private void HandleClientComm(object client) { TcpClient tcpClient = (TcpClient)client; clientConnectionList.Add(tcpClient); if (tcpClient.Connected == false) { return; } NetworkStream clientStream = tcpClient.GetStream(); //BinaryReader binaryReader = new BinaryReader(clientStream); int bytesRead; byte[] recievedCommand = new byte[10]; while (true) { bytesRead = 0; try { //blocks until a client sends a message if (tcpClient.Connected == true) { byte[] packetLength = new byte[2]; //read the packet length from the stream bytesRead = clientStream.Read(packetLength, 0, 2); //create a buffer of a size to fit the pending packet length recievedCommand = new byte[BitConverter.ToUInt16(packetLength, 0)]; for (int i = 0; i < 2; i++) { recievedCommand[i] = packetLength[i]; } //read the remaining data from the stream and write it to the recieved command array. bytesRead += clientStream.Read(recievedCommand, 2, recievedCommand.Length - 2); } } catch { // a socket error has occured System.Diagnostics.Debug.WriteLine("Socket error has occured in TCPServer.HandleClientComm"); break; } if (bytesRead == 0) { // the client has disconnected from the server System.Diagnostics.Debug.WriteLine("The client has disconnected from the server"); break; } //else message has been successfully recieved //write line to debug window System.Diagnostics.Debug.WriteLine("Command received: "); foreach (byte singleByte in recievedCommand) { System.Diagnostics.Debug.WriteLine((int)singleByte); } OptitrackCommandParser_Server commandParser = new OptitrackCommandParser_Server(); commandParser.handleCommand(recievedCommand); } }
private void HandleClientComm(object client) { TcpClient tcpClient = (TcpClient)client; clientConnectionList.Add(tcpClient); if (tcpClient.Connected == false) { return; } NetworkStream clientStream = tcpClient.GetStream(); //BinaryReader binaryReader = new BinaryReader(clientStream); int bytesRead; byte[] recievedCommand = new byte[10]; while (true) { bytesRead = 0; try { //blocks until a client sends a message if (tcpClient.Connected == true) { byte[] packetLength = new byte[2]; //read the packet length from the stream bytesRead = clientStream.Read(packetLength, 0, 2); //create a buffer of a size to fit the pending packet length recievedCommand = new byte[BitConverter.ToUInt16(packetLength, 0)]; for (int i = 0; i < 2; i++) { recievedCommand[i] = packetLength[i]; } //read the remaining data from the stream and write it to the recieved command array. bytesRead += clientStream.Read(recievedCommand, 2, recievedCommand.Length - 2); } } catch { // a socket error has occured System.Diagnostics.Debug.WriteLine("Socket error has occured in TCPServer.HandleClientComm"); break; } if (bytesRead == 0) { // the client has disconnected from the server System.Diagnostics.Debug.WriteLine("The client has disconnected from the server"); break; } //else message has been successfully recieved //write line to debug window System.Diagnostics.Debug.WriteLine("Command received: "); foreach (byte singleByte in recievedCommand) { System.Diagnostics.Debug.WriteLine((int)singleByte); } OptitrackCommandParser_Server commandParser = new OptitrackCommandParser_Server(); commandParser.handleCommand(recievedCommand); } }