using System.Collections; using System.Collections.Generic; using UnityEngine; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; class ClientSocket { private byte[] buffers; private Socket clientSocket; private int port = 8081; private string host = "127.0.0.1"; //private string host = "39.107.246.58"; public ClientSocket() { this.buffers = new byte[1100]; } private Socket clientConnect(int port, string host) { IPAddress ip = IPAddress.Parse(host); IPEndPoint ipe = new IPEndPoint(ip, port);//把ip和端口转化为IPEndPoint实例 Socket c = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket c.NoDelay = true; Console.WriteLine("Conneting..."); try { c.Connect(ipe);//连接到服务器 ; return c; } catch (Exception e) { throw e; //Thread.Sleep(1000); //clientConnect(port, host); //return c; } return null; } public void Receive() { try { clientSocket = clientConnect(port, host); while (true)//循环接收发送信息 { // Thread.Sleep(16); int length = 1; int packageNumber = 0;//包序 int row = 0; while (length > 0) { clientSocket.Receive(buffers); } } } catch (Exception e) { } finally { clientSocket.Close(); } } public string byteToHexStr(byte[] bytes) { string returnStr = ""; if (bytes != null) { for (int i = 0; i < bytes.Length; i++) { returnStr += bytes[i].ToString("X2"); } } return returnStr; } }