/// <summary> /// Callback function to accpect an incoming connection /// </summary> /// <param name="ar"></param> void AcceptCallback(IAsyncResult ar) { // Signal the main thread to continue. allDone.Set(); // Get the socket that handles the client request. Socket listener = (Socket)ar.AsyncState; ConnectionSocket = listener.EndAccept(ar); // Create the state object. TcpStateObject state = new TcpStateObject(); state.workSocket = ConnectionSocket; ConnectionSocket.BeginReceive(state.buffer, 0, TcpStateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); }
/// <summary> /// Called when a new server is connected to this client /// </summary> /// <param name="ar"></param> void ConnectCallback(IAsyncResult ar) { try { // Retrieve the socket from the state object. Socket client = (Socket)ar.AsyncState; // Complete the connection. client.EndConnect(ar); // Create the state object. TcpStateObject state = new TcpStateObject(); state.workSocket = client; // Begin receiving the data from the remote device. client.BeginReceive(state.buffer, 0, TcpStateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); } catch (Exception e) { Console.WriteLine(e.ToString()); } }
/// <summary> /// Read a Callback send from client /// </summary> /// <param name="ar"></param> void ReadCallback(IAsyncResult ar) { // Retrieve the state object and the handler socket // from the asynchronous state object. TcpStateObject state = (TcpStateObject)ar.AsyncState; Socket handler = state.workSocket; // Read data from the client socket. int bytesRead = handler.EndReceive(ar); Console.WriteLine("Callback read on server: " + bytesRead); if (bytesRead > 0) { // There might be more data, so store the data received so far. state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead)); //flag and trigger the package event // All the data has been read from the client. //trigger new message event if (PackageReceived != null) { PackageReceived(Encoding.ASCII.GetBytes(state.sb.ToString())); } //keep read is allowed so if (!TerminateFlag) { //establish new message and read more TcpStateObject newState = new TcpStateObject(); newState.workSocket = handler; try { handler.BeginReceive(newState.buffer, 0, TcpStateObject.BufferSize, 0, new AsyncCallback(ReadCallback), newState); } catch (SocketException) { } } } }
/// <summary> /// Async send a callback to client /// </summary> /// <param name="ar"></param> private void SendCallback(IAsyncResult ar) { try { // Retrieve the socket from the state object. Socket handler = (Socket)ar.AsyncState; // Complete sending the data to the remote device. int bytesSent = handler.EndSend(ar); //keep read is allowed so if (!TerminateFlag) { //establish new message and read more TcpStateObject newState = new TcpStateObject(); newState.workSocket = handler; handler.BeginReceive(newState.buffer, 0, TcpStateObject.BufferSize, 0, new AsyncCallback(ReadCallback), newState); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
/// <summary> /// Read a Callback send from client /// </summary> /// <param name="ar"></param> void ReadCallback(IAsyncResult ar) { try { String content = String.Empty; // Retrieve the state object and the client socket // from the asynchronous state object. TcpStateObject state = (TcpStateObject)ar.AsyncState; Socket client = state.workSocket; // Read data from the remote device. int bytesRead = client.EndReceive(ar); if (bytesRead > 0) { // There might be more data, so store the data received so far. state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead)); // Check for end-of-file tag. If it is not there, read // more data. content = state.sb.ToString(); // All the data has been read from the client. //trigger new message event if (PackageReceived != null) { PackageReceived(Encoding.ASCII.GetBytes(content)); } //establish new message and read more TcpStateObject newState = new TcpStateObject(); newState.workSocket = client; client.BeginReceive(state.buffer, 0, TcpStateObject.BufferSize, 0, new AsyncCallback(ReadCallback), state); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }