public void Receive() { SocketStateObject state = new SocketStateObject(); state.mClientSocket = mSocket; while (mSocket.Connected) { Thread.Sleep(10); try { if (mSocket.Available > 0) { state.mSb.Clear(); // Create the state object. ReadSocket(mSocket, state); } } catch (System.Net.Sockets.SocketException e) { Console.WriteLine("Socket Exception, quit"); return; } catch (Exception e) { Console.WriteLine("Can't receive data from the server: {0}"); return; } } }
public void Receive() { SocketStateObject state = new SocketStateObject(); state.mClientSocket = mConnectedClient; while (true) { if (mConnectedClient == null || mConnectedClient.Available <= 0) { System.Threading.Thread.Sleep(10); continue; } try { state.mSb.Clear(); ReadSocket(mConnectedClient, state); } catch (Exception e) { Console.WriteLine( "Can't receive data from the server: {0}", e.ToString()); return; } } }
private void ReadSocket(Socket pSocket, SocketStateObject pStateObject) { string message = string.Empty; if (pSocket == null) { return; } // Read data from the socket int bytesRead = 0; try { bytesRead = pSocket.Receive(pStateObject.mBuffer); } catch (Exception e) { Console.WriteLine("Can't read data from remote end "); pSocket.Close(); bytesRead = 0; return; } if (bytesRead > 0) { pStateObject.mSb.Append( Encoding.ASCII.GetString( pStateObject.mBuffer, 0, bytesRead)); if (pSocket.Available > 0) { ReadSocket(pSocket, pStateObject); } message = pStateObject.mSb.ToString(); Console.WriteLine( "Arrived {0} bytes from socket.\n Data: {1}", message.Length, message); // Process with the incoming data here // Data exchange, decoding DataEncoderImpl.Decapsulate(message); } }
private void ReadSocket(Socket pSocket, SocketStateObject pStateObject) { try { int bytesRead = pSocket.Receive(pStateObject.mBuffer); if (bytesRead > 0) { pStateObject.mSb.Append( Encoding.ASCII.GetString( pStateObject.mBuffer, 0, bytesRead)); if (pSocket.Available > 0) { ReadSocket(pSocket, pStateObject); } } if (pStateObject.mSb.Length > 1) { mMessage = pStateObject.mSb.ToString(); // Process with the incoming data here Console.WriteLine( "Arrived {0} bytes from socket.\n Data: {1}", mMessage.Length, mMessage); // Process with the incoming data here // Data exchange, decoding DataEncoderImpl.Decapsulate(mMessage); } } catch (Exception e) { Console.WriteLine("Can't receive data from the server."); } }