我想在Unity项目中使用C#插件。该插件应充当服务器,它将从客户端获取值,以便我可以将这些值用于进一步处理。问题是服务器具有无限循环。无限循环会导致Unity挂起。如何处理呢?
编辑:我附上服务器程序的代码段。我认为有2点可能会引起问题。如代码中所述,无限循环和暂停程序的地方:
void networkCode() { // Data buffer for incoming data. byte[] bytes = new Byte[1024]; // Establish the local endpoint for the socket. // Dns.GetHostName returns the name of the // host running the application. IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 1755); // Create a TCP/IP socket. listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); // Bind the socket to the local endpoint and // listen for incoming connections. try { listener.Bind(localEndPoint); listener.Listen(10); // Start listening for connections. while (true) { // Program is suspended while waiting for an incoming connection. Debug.Log("HELLO"); //It works handler = listener.Accept(); Debug.Log("HELLO"); //It doesn't work data = null; // An incoming connection needs to be processed. while (true) { bytes = new byte[1024]; int bytesRec = handler.Receive(bytes); data += Encoding.ASCII.GetString(bytes, 0, bytesRec); if (data.IndexOf("<EOF>") > -1) { break; } System.Threading.Thread.Sleep(1); } System.Threading.Thread.Sleep(1); } } catch (Exception e) { Debug.Log(e.ToString()); } }
编辑:@Programmer的帮助后,C#插件已完成。但是Unity没有读取正确的值。我附加了Unity边码:
using UnityEngine; using System; using SyncServerDLL; //That's our library public class receiver : MonoBehaviour { SynchronousSocketListener obj; //That's object to call server methods // Use this for initialization void Start() { obj = new SynchronousSocketListener (); obj.startServer (); } // Update is called once per frame void Update() { Debug.Log (obj.data); } }
我已经在Visual Studio中彻底测试了SynchronousSocketListener类。在那里效果很好。
使用线程来做您的服务器侦听和读写操作。您可以将套接字和其他networkstream对象声明为公共对象,然后在线程函数中对其进行初始化。
Unity无法很好地与Threads中的while循环配合使用,有时可能会 冻结 ,但您可以通过在要读取或等待数据从套接字到达的地方添加循环来 解决 此问题。System.Threading.Thread.Sleep(1);``while
System.Threading.Thread.Sleep(1);``while
确保停止Thread in OnDisable()功能。请 不要 在新的线程函数访问统一API。只需在此执行套接字操作,然后将数据返回到 公共 变量即可。
OnDisable()
System.Threading.Thread SocketThread; volatile bool keepReading = false; // Use this for initialization void Start() { Application.runInBackground = true; startServer(); } void startServer() { SocketThread = new System.Threading.Thread(networkCode); SocketThread.IsBackground = true; SocketThread.Start(); } private string getIPAddress() { IPHostEntry host; string localIP = ""; host = Dns.GetHostEntry(Dns.GetHostName()); foreach (IPAddress ip in host.AddressList) { if (ip.AddressFamily == AddressFamily.InterNetwork) { localIP = ip.ToString(); } } return localIP; } Socket listener; Socket handler; void networkCode() { string data; // Data buffer for incoming data. byte[] bytes = new Byte[1024]; // host running the application. Debug.Log("Ip " + getIPAddress().ToString()); IPAddress[] ipArray = Dns.GetHostAddresses(getIPAddress()); IPEndPoint localEndPoint = new IPEndPoint(ipArray[0], 1755); // Create a TCP/IP socket. listener = new Socket(ipArray[0].AddressFamily, SocketType.Stream, ProtocolType.Tcp); // Bind the socket to the local endpoint and // listen for incoming connections. try { listener.Bind(localEndPoint); listener.Listen(10); // Start listening for connections. while (true) { keepReading = true; // Program is suspended while waiting for an incoming connection. Debug.Log("Waiting for Connection"); //It works handler = listener.Accept(); Debug.Log("Client Connected"); //It doesn't work data = null; // An incoming connection needs to be processed. while (keepReading) { bytes = new byte[1024]; int bytesRec = handler.Receive(bytes); Debug.Log("Received from Server"); if (bytesRec <= 0) { keepReading = false; handler.Disconnect(true); break; } data += Encoding.ASCII.GetString(bytes, 0, bytesRec); if (data.IndexOf("<EOF>") > -1) { break; } System.Threading.Thread.Sleep(1); } System.Threading.Thread.Sleep(1); } } catch (Exception e) { Debug.Log(e.ToString()); } } void stopServer() { keepReading = false; //stop thread if (SocketThread != null) { SocketThread.Abort(); } if (handler != null && handler.Connected) { handler.Disconnect(false); Debug.Log("Disconnected!"); } } void OnDisable() { stopServer(); }