clientTCP.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7. using System.IO;
  8. using System.Threading;
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. using UnityEngine.Networking;
  12. public class clientTCP : MonoBehaviour
  13. {
  14. private string staInfo = "NULL"; //状态信息
  15. private string inputIp = "192.168.50.201"; //输入ip地址
  16. private string inputPort = "18180"; //输入端口号
  17. public string Message = "NULL"; //发送的消息
  18. private int recTimes = 0; //接收到信息的次数
  19. private string recMes = "NULL"; //接收到的消息
  20. private Socket socketSend; //客户端套接字,用来链接远端服务器
  21. private bool clickSend = false; //是否点击发送按钮
  22. private bool isConnect = true;
  23. private float times = 0;
  24. private string ipPath;
  25. public Queue<String> queueRecMsg;
  26. void Start()
  27. {
  28. /*
  29. ipPath = Application.persistentDataPath + "/Ipconfig/" + "ip.txt";
  30. Debug.Log(ipPath);
  31. StartCoroutine( SettingIP());*/
  32. // ClickConnect();
  33. queueRecMsg = new Queue<string>();
  34. }
  35. public float dmaxx = 1;
  36. public float dmaxy = 1;
  37. float maxx;
  38. float minx;
  39. float maxy;
  40. float miny;
  41. float xx;
  42. float yy;
  43. void Update()
  44. {
  45. if (Input.GetKeyDown(KeyCode.A))
  46. {
  47. SendMessage();
  48. }
  49. if (Input.GetKeyDown(KeyCode.S))
  50. {
  51. SendMessage2();
  52. }
  53. if (Input.GetKeyDown(KeyCode.D))
  54. {
  55. SendMessage3();
  56. }
  57. Debug.Log("x的长度为:minx," + minx + "_maxx" + maxx);
  58. Debug.Log("y的长度为:miny," + miny + "_maxy" + maxy);
  59. Debug.Log("当前长度:x," + xx + "_y" + yy);
  60. //float ctx = xx / maxx;
  61. //float cty = yy / maxy;
  62. float fx = 0;
  63. float fy = 0;
  64. if(minx<0)
  65. {
  66. fx = minx;
  67. }
  68. if(miny<0)
  69. {
  70. fy = miny;
  71. }
  72. float zctx = Mathf.Abs(minx) + maxx;
  73. float zcty = Mathf.Abs(miny) + maxy;
  74. float curx = Mathf.Abs(fx) - Mathf.Abs(xx);//xx + minx;
  75. if (xx > 0)
  76. {
  77. curx = xx + Mathf.Abs(fx);
  78. }
  79. else
  80. {
  81. }
  82. if (maxx < 0)
  83. {
  84. curx = Mathf.Abs(xx) + maxx;
  85. }
  86. float cury = Mathf.Abs(yy);//xx + minx;
  87. if (yy > 0)
  88. {
  89. cury = yy + Mathf.Abs(fy);
  90. }
  91. if(maxy<0)
  92. {
  93. cury = Mathf.Abs(yy)+ maxy;
  94. }
  95. float ctx = curx / zctx;
  96. float cty = cury / zcty;
  97. if(minx!=0)
  98. img.transform.localPosition = new Vector3(0,(-(cty* dmaxy - dmaxy))/2f, (dmaxx * ctx - dmaxx / 2f));
  99. }
  100. IEnumerator SettingIP()
  101. {
  102. yield return new WaitForSeconds(0.2f);
  103. if (File.Exists(ipPath))
  104. {
  105. var webRequest = UnityWebRequest.Get(ipPath);
  106. yield return webRequest.SendWebRequest();
  107. if (webRequest.error != null)
  108. {
  109. Debug.Log("通信中");
  110. yield return webRequest;
  111. }
  112. var downloadHandler = webRequest.downloadHandler;
  113. if (!downloadHandler.isDone)
  114. {
  115. Debug.Log("下载中");
  116. yield return downloadHandler;
  117. }
  118. else
  119. {
  120. Debug.Log("下载成功");
  121. Debug.Log(downloadHandler.data);
  122. // if (!File.Exists(Application.dataPath + "/Resources/YourVideoName.mp4"))
  123. inputIp = System.Text.Encoding.Default.GetString(downloadHandler.data);
  124. // ClickConnect();
  125. }
  126. }
  127. else
  128. {
  129. Directory.CreateDirectory(Application.persistentDataPath + "/Ipconfig");
  130. FileInfo fileInfo = new FileInfo(ipPath);
  131. //设置Log文件输出地址
  132. FileStream FileWriter = fileInfo.Open(FileMode.Create, FileAccess.ReadWrite, FileShare.Read);
  133. UTF8Encoding encoding = new UTF8Encoding();
  134. FileWriter.Write(encoding.GetBytes("192.168.50.211"), 0, encoding.GetByteCount("192.168.50.211"));
  135. FileWriter.Close();
  136. Application.Quit();
  137. }
  138. }
  139. //建立链接
  140. public void ClickConnect(string ipurl)
  141. {
  142. return;
  143. Debug.Log("ClickConnect");
  144. isConnect = true;
  145. try
  146. {
  147. int _port = 8000;//Convert.ToInt32(inputPort); //获取端口号
  148. string _ip = ipurl;//"192.168.1.109"; //获取ip地址
  149. Debug.Log("ClickConnect1 " + _ip + _port);
  150. //创建客户端Socket,获得远程ip和端口号
  151. socketSend = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  152. IPAddress ip = IPAddress.Parse(_ip);
  153. IPEndPoint point = new IPEndPoint(ip, _port);
  154. Debug.Log("ClickConnect2");
  155. socketSend.Connect(point);
  156. Debug.Log("连接成功 , " + " ip = " + ip + " port = " + _port);
  157. staInfo = ip + ":" + _port + " 连接成功";
  158. Thread r_thread = new Thread(Received); //开启新的线程,不停的接收服务器发来的消息
  159. r_thread.IsBackground = true;
  160. r_thread.Start();
  161. Thread s_thread = new Thread(SendMessage); //开启新的线程,不停的给服务器发送消息
  162. s_thread.IsBackground = true;
  163. s_thread.Start();
  164. }
  165. catch (Exception e)
  166. {
  167. Debug.LogError(e.ToString());
  168. Debug.Log("IP或者端口号错误......");
  169. staInfo = "IP或者端口号错误......";
  170. isConnect = false;
  171. }
  172. }
  173. public GameObject img;
  174. byte[] cx = new byte[2];
  175. /// <summary>
  176. /// 接收服务端返回的消息
  177. /// </summary>
  178. void Received()
  179. {
  180. while (true)
  181. {
  182. try
  183. {
  184. byte[] buffer = new byte[23];
  185. //实际接收到的有效字节数
  186. int len = socketSend.Receive(buffer);
  187. if (len == 0)
  188. {
  189. // isConnect = false;
  190. break;
  191. }
  192. // Debug.Log("curx==:" + buffer[3]);
  193. switch (buffer[3])
  194. {
  195. case 0x00:
  196. break;
  197. case 0x01:
  198. break;
  199. case 0x02:
  200. break;
  201. case 0x03:
  202. float af = 0;
  203. cx[0] = buffer[4];
  204. cx[1] = buffer[5];
  205. // Debug.Log("curx4==:" + buffer[4]);
  206. // Debug.Log("curx5==:" + buffer[5]);
  207. af = byteArrayToshort(cx, 0);
  208. // Debug.Log("minx==:" + af);
  209. minx = af;
  210. cx[0] = buffer[6];
  211. cx[1] = buffer[7];
  212. af = byteArrayToshort(cx, 0);
  213. maxx = af;
  214. // Debug.Log("maxx==:" + af);
  215. cx[0] = buffer[8];
  216. cx[1] = buffer[9];
  217. af = byteArrayToshort(cx, 0);
  218. Debug.Log("curx==:" + af);
  219. xx = af;
  220. byte[] cy = new byte[2];
  221. cy[0] = buffer[10];
  222. cy[1] = buffer[11];
  223. af = byteArrayToshort(cy, 0);
  224. miny = af;
  225. // Debug.Log("miny==:" + af);
  226. cy[0] = buffer[12];
  227. cy[1] = buffer[13];
  228. af = byteArrayToshort(cy, 0);
  229. maxy = af;
  230. // Debug.Log("maxy==:" + af);
  231. cy[0] = buffer[14];
  232. cy[1] = buffer[15];
  233. af = byteArrayToshort(cy, 0);
  234. Debug.Log("cury==:" + af);
  235. yy = af;
  236. break;
  237. }
  238. /*
  239. Debug.Log("客户端接收到的数据 : " );
  240. recMes = Encoding.UTF8.GetString(buffer, 0, len);
  241. Debug.Log("客户端接收到的数据 : " + recMes);
  242. recTimes++;
  243. staInfo = "接收到一次数据,接收次数为 :" + recTimes;
  244. // Debug.Log("接收次数为:" + recTimes);
  245. */
  246. // queueRecMsg.Enqueue(recMes);
  247. }
  248. catch { }
  249. }
  250. }
  251. public void SendData()
  252. {
  253. }
  254. /// <summary>
  255. /// 向服务器发送消息
  256. /// </summary>
  257. /// <param name="sender"></param>
  258. /// <param name="e"></param>
  259. void SendMessage()
  260. {
  261. Debug.Log("发送数据");
  262. byte[] buffer = new byte[5];
  263. buffer[0] = 0x3F;
  264. buffer[1] = 0x02;
  265. buffer[2] = 0x05;
  266. buffer[3] = 0x03;
  267. buffer[4] = 0x3F;
  268. socketSend.Send(buffer);
  269. }
  270. /// <summary>
  271. /// 向服务器发送消息
  272. /// </summary>
  273. /// <param name="sender"></param>
  274. /// <param name="e"></param>
  275. void SendMessage2()
  276. {
  277. Debug.Log("发送数据");
  278. byte[] buffer = new byte[4];
  279. buffer[0] = 0x3F;
  280. buffer[1] = 0x01;
  281. buffer[2] = 0x01;
  282. buffer[3] = 0x3F;
  283. socketSend.Send(buffer);
  284. }
  285. /// <summary>
  286. /// 向服务器发送消息
  287. /// </summary>
  288. /// <param name="sender"></param>
  289. /// <param name="e"></param>
  290. void SendMessage3()
  291. {
  292. Debug.Log("发送数据");
  293. byte[] buffer = new byte[5];
  294. buffer[0] = 0x3F;
  295. buffer[1] = 0x02;
  296. buffer[2] = 0x05;
  297. buffer[2] = 0x00;
  298. buffer[3] = 0x3F;
  299. socketSend.Send(buffer);
  300. }
  301. private short byteArrayToshort(byte[] b, int offset)
  302. {
  303. // throw new NotImplementedException();
  304. short s = 0;
  305. short s0 = (short)(b[1] & 0xff);// 最低位
  306. short s1 = (short)(b[0] & 0xff);
  307. s1 <<= 8;
  308. s = (short)(s0 | s1);
  309. return s;
  310. }
  311. private void OnDisable()
  312. {
  313. Debug.Log("begin OnDisable()");
  314. if (socketSend.Connected)
  315. {
  316. try
  317. {
  318. socketSend.Shutdown(SocketShutdown.Both); //禁用Socket的发送和接收功能
  319. socketSend.Close(); //关闭Socket连接并释放所有相关资源
  320. }
  321. catch (Exception e)
  322. {
  323. print(e.Message);
  324. }
  325. }
  326. Debug.Log("end OnDisable()");
  327. }
  328. //用户界面
  329. //void OnGUI()
  330. //{
  331. // //GUI.color = Color.black;
  332. // //GUI.Label(new Rect(65, 10, 60, 20), "状态信息");
  333. // //GUI.Label(new Rect(135, 10, 80, 60), staInfo);
  334. // //GUI.Label(new Rect(65, 70, 50, 20), "服务器ip地址");
  335. // //inputIp = GUI.TextField(new Rect(125, 70, 100, 20), inputIp, 20);
  336. // //GUI.Label(new Rect(65, 110, 50, 20), "服务器端口");
  337. // //inputPort = GUI.TextField(new Rect(125, 110, 100, 20), inputPort, 20);
  338. // //GUI.Label(new Rect(65, 150, 80, 20), "接收到消息:");
  339. // //GUI.Label(new Rect(155, 150, 80, 20), recMes);
  340. // //GUI.Label(new Rect(65, 190, 80, 20), "发送的消息:");
  341. // //Message = GUI.TextField(new Rect(155, 190, 100, 20), Message, 20);
  342. // //if (GUI.Button(new Rect(65, 270, 300, 100), "发送信息"))
  343. // //{
  344. // // clickSend = true;
  345. // //}
  346. //}
  347. }