private static void RunChatClient(SocketMessaging.TcpClient client) { client.SetMode(MessageMode.DelimiterBound); client.ReceivedMessage += (sender, e) => Console.Write(client.ReceiveMessageString()); Console.WriteLine("Write something in the chat."); Console.WriteLine("Press Enter on an empty line to disconnect."); Console.WriteLine(""); Console.WriteLine("Communication log:"); Console.WriteLine("=================="); string message; do { message = Console.ReadLine(); if (message == "") { break; } client.Send(message); } while (client.IsConnected); if (!client.IsConnected) { Console.WriteLine("Forcefully disconnected by server."); } else { client.Close(); Console.WriteLine("User disconnected."); } }
/// <summary> /// Opens the connection to the server and begins listening for events /// </summary> private void InitiateConnection() { AddRemoteStatusMessage($"Connecting to {remoteEndpoint.Address}:{remoteEndpoint.Port}..."); try { client = TcpClient.Connect(remoteEndpoint.Address, remoteEndpoint.Port); client.ReceivedMessage += Client_ReceivedMessage; client.Disconnected += Client_Disconnected; client.SetMode(MessageMode.PrefixedLength); } catch (Exception ex) { AddRemoteStatusMessage($"ERROR: Connection to host failed: {ex.Message}"); } }