I have a TCPListener running in Unity that accepts TCPClients in a blocking fashion in a separate thread. It works perfect in a Windows standalone build, but throws a SocketException: Operation on non-blocking socket would block
in a Linux standalone build.
Here’s the code for it:
try
{
// Set the TcpListener on port 39999.
int port = 39999;
IPAddress localAddr = IPAddress.Parse("127.0.0.1");
server = new TcpListener(localAddr, port);
server.Server.ReceiveTimeout = 3000; // 3 second timeout
// Start listening for client requests.
server.Start();
// Buffer for reading data
byte[] bytes = new byte[10000000]; // 10 MB
string data = null;
// Enter the listening loop.
while (alive)
{
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient client = server.AcceptTcpClient();
client.Client.Blocking = true;
data = "";
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0 && alive)
{
// Translate data bytes to a ASCII string.
data += System.Text.Encoding.ASCII.GetString(bytes, 0, i);
// Process the data sent by the client.
...
[code to parse json strings]
...
}
// Shutdown and end connection
stream.Close();
client.Close();
}
}
catch (SocketException e)
{
Ex = e.Message;
}
finally
{
// Stop listening for new clients.
server.Stop();
}
Any ideas why it works in Windows but not Linux? Thanks!
Edit: Unity version 5.3.4f1