private void Events_MessageReceived(object sender, MessageReceivedEventArgs e) { CommandResponseReceived(e); if (tcpClient.Connected) { tcpClient.Disconnect(); tcpClient.Dispose(); } }
static void Main(string[] args) { _Client = new WatsonTcpClient("127.0.0.1", 9000); _Client.Events.MessageReceived += (s, e) => { Console.WriteLine(e.IpPort + ": " + Encoding.UTF8.GetString(e.Data)); }; while (true) { try { _Client.Connect(); Console.WriteLine(DateTime.UtcNow.ToString() + " Connected"); Task.Delay(_Random.Next(1000, 3000)).Wait(); _Client.Disconnect(); } catch (Exception e) { Console.WriteLine(DateTime.UtcNow.ToString() + " Failed: " + e.ToString()); } } }
private static void Main(string[] args) { InitializeClient(); bool runForever = true; Dictionary <object, object> metadata; bool success; while (runForever) { string userInput = InputString("Command [? for help]:", null, false); switch (userInput) { case "?": Console.WriteLine("Available commands:"); Console.WriteLine(" ? help (this menu)"); Console.WriteLine(" q quit"); Console.WriteLine(" cls clear screen"); Console.WriteLine(" send send message to server"); Console.WriteLine(" send offset send message to server with offset"); Console.WriteLine(" send md send message with metadata to server"); Console.WriteLine(" sendasync send message to server asynchronously"); Console.WriteLine(" sendasync md send message with metadata to server asynchronously"); Console.WriteLine(" sendandwait send message and wait for a response"); Console.WriteLine(" sendempty send empty message with metadata"); Console.WriteLine(" sendandwait empty send empty message with metadata and wait for a response"); Console.WriteLine(" status show if client connected"); Console.WriteLine(" dispose dispose of the client"); Console.WriteLine(" connect connect to the server"); Console.WriteLine(" disconnect disconnect from the server"); Console.WriteLine(" psk set the preshared key"); Console.WriteLine(" auth authenticate using the preshared key"); Console.WriteLine(" stats display client statistics"); Console.WriteLine(" stats reset reset statistics other than start time and uptime"); Console.WriteLine(" debug enable/disable debug"); break; case "q": runForever = false; break; case "cls": Console.Clear(); break; case "send": userInput = InputString("Data:", null, false); if (!_Client.Send(Encoding.UTF8.GetBytes(userInput))) { Console.WriteLine("Failed"); } break; case "send offset": userInput = InputString("Data:", null, false); int offset = InputInteger("Offset:", 0, true, true); if (!_Client.Send(Encoding.UTF8.GetBytes(userInput), null, offset)) { Console.WriteLine("Failed"); } break; case "send md": userInput = InputString("Data:", null, false); metadata = InputDictionary(); if (!_Client.Send(Encoding.UTF8.GetBytes(userInput), metadata)) { Console.WriteLine("Failed"); } break; case "send md large": metadata = new Dictionary <object, object>(); for (int i = 0; i < 100000; i++) { metadata.Add(i, i); } if (!_Client.Send("Hello!", metadata)) { Console.WriteLine("Failed"); } break; case "sendasync": userInput = InputString("Data:", null, false); success = _Client.SendAsync(Encoding.UTF8.GetBytes(userInput)).Result; if (!success) { Console.WriteLine("Failed"); } break; case "sendasync md": userInput = InputString("Data:", null, false); metadata = InputDictionary(); success = _Client.SendAsync(Encoding.UTF8.GetBytes(userInput), metadata).Result; if (!success) { Console.WriteLine("Failed"); } break; case "sendandwait": SendAndWait(); break; case "sendempty": metadata = InputDictionary(); success = _Client.Send("", metadata); if (!success) { Console.WriteLine("Failed"); } break; case "sendandwait empty": SendAndWaitEmpty(); break; case "status": if (_Client == null) { Console.WriteLine("Connected: False (null)"); } else { Console.WriteLine("Connected: " + _Client.Connected); } break; case "dispose": _Client.Dispose(); break; case "connect": _Client.Connect(); break; case "disconnect": _Client.Disconnect(); break; case "psk": _PresharedKey = InputString("Preshared key:", "1234567812345678", false); break; case "auth": _Client.Authenticate(_PresharedKey); break; case "stats": Console.WriteLine(_Client.Statistics.ToString()); break; case "stats reset": _Client.Statistics.Reset(); break; case "debug": _Client.Settings.DebugMessages = !_Client.Settings.DebugMessages; Console.WriteLine("Debug set to: " + _Client.Settings.DebugMessages); break; default: break; } } }
private static void Main(string[] args) { serverIp = InputString("Server IP:", "127.0.0.1", false); serverPort = InputInteger("Server port:", 9000, true, false); useSsl = InputBoolean("Use SSL:", false); InitializeClient(); bool runForever = true; Dictionary <object, object> metadata; bool success; while (runForever) { Console.Write("Command [? for help]: "); string userInput = Console.ReadLine(); byte[] data = null; MemoryStream ms = null; if (String.IsNullOrEmpty(userInput)) { continue; } switch (userInput) { case "?": Console.WriteLine("Available commands:"); Console.WriteLine(" ? help (this menu)"); Console.WriteLine(" q quit"); Console.WriteLine(" cls clear screen"); Console.WriteLine(" send send message to server"); Console.WriteLine(" send md send message with metadata to server"); Console.WriteLine(" sendasync send message to server asynchronously"); Console.WriteLine(" sendasync md send message with metadata to server asynchronously"); Console.WriteLine(" sendandwait send message and wait for a response"); Console.WriteLine(" status show if client connected"); Console.WriteLine(" dispose dispose of the client"); Console.WriteLine(" connect connect to the server"); Console.WriteLine(" disconnect disconnect from the server"); Console.WriteLine(" psk set the preshared key"); Console.WriteLine(" auth authenticate using the preshared key"); Console.WriteLine(" debug enable/disable debug"); break; case "q": runForever = false; break; case "cls": Console.Clear(); break; case "send": Console.Write("Data: "); userInput = Console.ReadLine(); if (String.IsNullOrEmpty(userInput)) { break; } data = Encoding.UTF8.GetBytes(userInput); ms = new MemoryStream(data); success = client.Send(data.Length, ms); Console.WriteLine(success); break; case "send md": metadata = InputDictionary(); Console.Write("Data: "); userInput = Console.ReadLine(); if (String.IsNullOrEmpty(userInput)) { break; } data = Encoding.UTF8.GetBytes(userInput); ms = new MemoryStream(data); success = client.Send(data.Length, ms, metadata); Console.WriteLine(success); break; case "sendasync": Console.Write("Data: "); userInput = Console.ReadLine(); if (String.IsNullOrEmpty(userInput)) { break; } data = Encoding.UTF8.GetBytes(userInput); ms = new MemoryStream(data); success = client.SendAsync(data.Length, ms).Result; Console.WriteLine(success); break; case "sendasync md": metadata = InputDictionary(); Console.Write("Data: "); userInput = Console.ReadLine(); if (String.IsNullOrEmpty(userInput)) { break; } data = Encoding.UTF8.GetBytes(userInput); ms = new MemoryStream(data); success = client.SendAsync(data.Length, ms, metadata).Result; Console.WriteLine(success); break; case "sendandwait": SendAndWait(); break; case "status": if (client == null) { Console.WriteLine("Connected: False (null)"); } else { Console.WriteLine("Connected: " + client.Connected); } break; case "dispose": client.Dispose(); break; case "connect": client.Connect(); break; case "disconnect": client.Disconnect(); break; case "psk": presharedKey = InputString("Preshared key:", "1234567812345678", false); break; case "auth": client.Authenticate(presharedKey); break; case "debug": client.Settings.DebugMessages = !client.Settings.DebugMessages; Console.WriteLine("Debug set to: " + client.Settings.DebugMessages); break; default: break; } } }
public void Disconnect() { UIHandler.StopColorfulBarAnimation(); clientH.Disconnect(); }
public static void Disconnect() { client.Disconnect(); }
public void LeaveServer() { Users.Clear(); _client.Disconnect(); }