// Invoke the ResponseReceived event protected void OnResponseReceived(ResponseReceivedEventArgs e) { if (ResponseReceived != null) { ResponseReceived(this, e); } }
private void ProcessReceive(SocketAsyncEventArgs e) { if (e.SocketError == SocketError.Success) { // Received data from server string dataFromServer = Encoding.UTF8.GetString(e.Buffer, 0, e.BytesTransferred); Socket sock = e.UserToken as Socket; sock.Shutdown(SocketShutdown.Send); sock.Close(); clientDone.Set(); // Respond to the client in the UI thread to tell him that data was received System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { ResponseReceivedEventArgs args = new ResponseReceivedEventArgs(); args.response = dataFromServer; OnResponseReceived(args); }); } else { clientDone.Set(); throw new SocketException((int)e.SocketError); } }
void handler_ResponseReceived(object sender, ResponseReceivedEventArgs e) { previousPos = currentPos; string[] data = e.response.Split('|'); int.TryParse(data[1], out currentPos); if (currentPos != previousPos) { targetIndex = currentPos; } }
// Called when a SendAsync operation completes private void ProcessSend(SocketAsyncEventArgs e) { if (e.SocketError == SocketError.Success) { //Read data sent from the server Socket sock = e.UserToken as Socket; sock.ReceiveAsync(e); } else { clientDone.Set(); ResponseReceivedEventArgs args = new ResponseReceivedEventArgs(); args.response = e.SocketError.ToString(); args.isError = true; OnResponseReceived(args); } }
private void ProcessConnect(SocketAsyncEventArgs e) { if (e.SocketError == SocketError.Success) { // Successfully connected to the server // Send data to the server byte[] buffer = Encoding.UTF8.GetBytes(dataIn); e.SetBuffer(buffer, 0, buffer.Length); Socket sock = e.UserToken as Socket; sock.SendAsync(e); } else { clientDone.Set(); ResponseReceivedEventArgs args = new ResponseReceivedEventArgs(); args.response = e.SocketError.ToString(); args.isError = true; OnResponseReceived(args); } }