private void HandleClientComm(object client) { TcpClient tcpClient = (TcpClient)client; NetworkStream clientStream = tcpClient.GetStream(); byte[] message = new byte[4096]; int bytesRead; while (true) { bytesRead = 0; try { //blocks until a client sends a message bytesRead = clientStream.Read(message, 0, 4096); } catch { //a socket error has occured break; } if (bytesRead == 0) { //the client has disconnected from the server break; } //message has successfully been received ASCIIEncoding encoder = new ASCIIEncoding(); String message_string = encoder.GetString(message, 0, bytesRead); CallResponse call_response = new CallResponse(message_string, clientStream); queue.add(call_response); } tcpClient.Close(); }
private void HandleClientComm(object client) { //Debug.Log ("Called HandleClientComm, applicationRunning is: " + applicationRunning.ToString()); FileLogger.Log("New TCP Client accepted."); TcpClient tcpClient = (TcpClient)client; NetworkStream clientStream = tcpClient.GetStream(); byte[] message = new byte[4096]; int bytesRead; while (applicationRunning) { bytesRead = 0; String message_string = ""; try { do { FileLogger.Log("About to block, waiting for Java."); //blocks until a client sends a message bytesRead = clientStream.Read(message, 0, 4096); if (bytesRead == 0) { throw new Exception(""); } FileLogger.Log("Unblocked. Got bytes from Java."); ASCIIEncoding encoder = new ASCIIEncoding(); message_string += encoder.GetString(message, 0, bytesRead); } while (!message_string.EndsWith("\n")); } catch (Exception e) { FileLogger.Log("A socket error occured or there was not data while waiting for Java: " + e.Message); //a socket error has occured break; } /*if (bytesRead == 0) * { * FileLogger.Log("The Java client disconnected"); * * //the client has disconnected from the server * break; * }*/ //message has successfully been received //ASCIIEncoding encoder = new ASCIIEncoding(); //String message_string = encoder.GetString(message, 0, bytesRead); //} //ends while loop FileLogger.Log("Unity got response from Java: " + message_string); CallResponse call_response = new CallResponse(message_string, clientStream); queue.add(call_response); } FileLogger.Log("Closing TCP connection."); //Debug.Log ("Closing TCP connection, applicationRunning is: " + applicationRunning.ToString()); tcpClient.Close(); }