Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Linux - SocketException: Operation on non-blocking socket would block

Discussion in 'Scripting' started by gReMixx, Apr 22, 2016.

  1. gReMixx

    gReMixx

    Joined:
    Aug 26, 2015
    Posts:
    6
    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
    Code (csharp):
    1. SocketException: Operation on non-blocking socket would block
    in a Linux standalone build.

    Here's the code for it:
    Code (csharp):
    1.  
    2.         try
    3.         {
    4.             // Set the TcpListener on port 39999.
    5.             int port = 39999;
    6.             IPAddress localAddr = IPAddress.Parse("127.0.0.1");
    7.  
    8.             server = new TcpListener(localAddr, port);
    9.  
    10.             server.Server.ReceiveTimeout = 3000; // 3 second timeout
    11.  
    12.             // Start listening for client requests.
    13.             server.Start();
    14.  
    15.             // Buffer for reading data
    16.             byte[] bytes = new byte[10000000]; // 10 MB
    17.             string data = null;
    18.  
    19.             // Enter the listening loop.
    20.             while (alive)
    21.             {
    22.                 // Perform a blocking call to accept requests.
    23.                 // You could also user server.AcceptSocket() here.
    24.                 TcpClient client = server.AcceptTcpClient();
    25.                 client.Client.Blocking = true;
    26.  
    27.                 data = "";
    28.  
    29.                 // Get a stream object for reading and writing
    30.                 NetworkStream stream = client.GetStream();
    31.  
    32.                 int i;
    33.  
    34.                 // Loop to receive all the data sent by the client.
    35.                 while ((i = stream.Read(bytes, 0, bytes.Length)) != 0 && alive)
    36.                 {
    37.                     // Translate data bytes to a ASCII string.
    38.                     data += System.Text.Encoding.ASCII.GetString(bytes, 0, i);
    39.  
    40.                     // Process the data sent by the client.
    41.                    ...
    42.                    [code to parse json strings]
    43.                    ...
    44.                 }
    45.  
    46.                 // Shutdown and end connection
    47.                 stream.Close();
    48.                 client.Close();
    49.             }
    50.         }
    51.         catch (SocketException e)
    52.         {
    53.             Ex = e.Message;
    54.         }
    55.         finally
    56.         {
    57.             // Stop listening for new clients.
    58.             server.Stop();
    59.         }
    60.  
    Any ideas why it works in Windows but not Linux? Thanks!

    Edit: Unity version 5.3.4f1
     
  2. dustinkerstein

    dustinkerstein

    Joined:
    Jan 26, 2017
    Posts:
    16
    Hey! Did you ever solve this issue? I'm running into a similar one.