private void buttonSendMessage_Click(object sender, EventArgs e) { try { // Create the message to send Messages.StringMessage message = new Messages.StringMessage(); message.Message = textBoxMessage.Text; // Serialize the message to a binary array byte[] binaryMessage = Messages.Util.Serialize(message); // Send the message; the state is used by ClientSocket_WriteCompleted to display an output to the log string description = "<string message: " + message.Message + ">"; ClientSocket.WriteAsync(binaryMessage, description); textBoxLog.AppendText("Sending message " + description + Environment.NewLine); } catch (Exception ex) { ResetSocket(); textBoxLog.AppendText("Error sending message to socket: [" + ex.GetType().Name + "] " + ex.Message + Environment.NewLine); } finally { RefreshDisplay(); } }
private void ChildSocket_PacketArrived(ServerChildTcpSocket socket, AsyncResultEventArgs <byte[]> e) { try { // Check for errors if (e.Error != null) { textBoxLog.AppendText("Client socket error during Read from " + socket.RemoteEndPoint.ToString() + ": [" + e.Error.GetType().Name + "] " + e.Error.Message + Environment.NewLine); ResetChildSocket(socket); } else if (e.Result == null) { // PacketArrived completes with a null packet when the other side gracefully closes the connection textBoxLog.AppendText("Socket graceful close detected from " + socket.RemoteEndPoint.ToString() + Environment.NewLine); // Close the socket and remove it from the list ResetChildSocket(socket); } else { // At this point, we know we actually got a message. // Deserialize the message object message = Messages.Util.Deserialize(e.Result); // Handle the message Messages.StringMessage stringMessage = message as Messages.StringMessage; if (stringMessage != null) { textBoxLog.AppendText("Socket read got a string message from " + socket.RemoteEndPoint.ToString() + ": " + stringMessage.Message + Environment.NewLine); return; } Messages.ComplexMessage complexMessage = message as Messages.ComplexMessage; if (complexMessage != null) { textBoxLog.AppendText("Socket read got a complex message from " + socket.RemoteEndPoint.ToString() + ": (UniqueID = " + complexMessage.UniqueID.ToString() + ", Time = " + complexMessage.Time.ToString() + ", Message = " + complexMessage.Message + ")" + Environment.NewLine); return; } textBoxLog.AppendText("Socket read got an unknown message from " + socket.RemoteEndPoint.ToString() + " of type " + message.GetType().Name + Environment.NewLine); } } catch (Exception ex) { textBoxLog.AppendText("Error reading from socket " + socket.RemoteEndPoint.ToString() + ": [" + ex.GetType().Name + "] " + ex.Message + Environment.NewLine); ResetChildSocket(socket); } finally { RefreshDisplay(); } }
private void buttonSendMessage_Click(object sender, EventArgs e) { // This function sends a simple (string) message to all connected clients Messages.StringMessage message = new Messages.StringMessage(); message.Message = textBoxMessage.Text; string description = "<string message: " + message.Message + ">"; // Serialize it to a binary array byte[] binaryObject = Messages.Util.Serialize(message); // Keep a list of all errors for child sockets Dictionary <ServerChildTcpSocket, Exception> SocketErrors = new Dictionary <ServerChildTcpSocket, Exception>(); // Start a send on each child socket foreach (KeyValuePair <ServerChildTcpSocket, ChildSocketContext> childSocket in ChildSockets) { // Ignore sockets that are disconnecting if (childSocket.Value.State != ChildSocketState.Connected) { continue; } try { textBoxLog.AppendText("Sending to " + childSocket.Key.RemoteEndPoint.ToString() + ": " + description + Environment.NewLine); SocketPacketProtocol.WritePacketAsync(childSocket.Key, binaryObject, description); } catch (Exception ex) { // Make a note of the error to handle later SocketErrors.Add(childSocket.Key, ex); } } // Handle all errors. This is done outside the enumeration loop because the child socket // error recovery will remove the socket from the list of child sockets. foreach (KeyValuePair <ServerChildTcpSocket, Exception> error in SocketErrors) { textBoxLog.AppendText("Child Socket error sending message to " + error.Key.RemoteEndPoint.ToString() + ": [" + error.Value.GetType().Name + "] " + error.Value.Message + Environment.NewLine); ResetChildSocket(error.Key); } // In case there were any errors, the display may need to be updated RefreshDisplay(); }