protected virtual void Dispose(bool disposing) { if (disposing) { if (Wtcp != null) { Wtcp.Dispose(); } } }
static void Main(string[] args) { Task.Run(() => { WatsonTcpClient client = new WatsonTcpClient("10.1.2.3", 1234); // NonExistant Server client.ServerConnected += HandleServerConnected; client.ServerDisconnected += HandleServerDisconnected; client.MessageReceived += HandleMessageReceived; try { Console.WriteLine("Starting Client"); client.Start(); } catch (Exception ex) { Console.WriteLine("Exception: {0}", ex.Message); client.Dispose(); } }); Console.WriteLine("Waiting on NullReferenceException"); Thread.Sleep(10000); }
public void Disconnect() { if (IsConnected()) { _forceDisconnect = true; _watsonTcpClient.Dispose(); } }
private void Events_MessageReceived(object sender, MessageReceivedEventArgs e) { CommandResponseReceived(e); if (tcpClient.Connected) { tcpClient.Disconnect(); tcpClient.Dispose(); } }
protected virtual void Dispose(bool disposing) { if (disposing) { if (_WtcpClient != null) { _WtcpClient.Dispose(); } } }
static void Main(string[] args) { try { _Client = new WatsonTcpClient("127.0.0.1", 9000); _Client.Events.MessageReceived += MessageReceived; _Client.Start(); _Client.Send("Hello!"); _Client.Dispose(); } catch (Exception e) { Console.WriteLine(e.ToString()); } }
private void close_connection() { if (client == null) { return; } try { reset = false; client.Events.MessageReceived -= Events_MessageReceived; client.Dispose(); } catch { } client = null; }
protected virtual void Dispose(bool disposing) { if (_Disposed) { return; } if (disposing) { if (_TcpClient != null) { _TcpClient.Dispose(); } } _Disposed = true; }
public void Stop() { logger.Warn("Sent Stop to APIConnect"); if (cts != null) { // Cancel just in case cts.Cancel(); logger.Warn(cts.IsCancellationRequested); } // Dispose of connection if still up if (client.IsConnected()) { client.Dispose(); } tcs.SetResult(true); }
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 void Form1_FormClosing(object sender, FormClosingEventArgs e) { client.Dispose(); }
static void Main(string[] args) { Console.Write("Server IP : "); serverIp = Console.ReadLine(); Console.Write("Server Port : "); serverPort = Convert.ToInt32(Console.ReadLine()); WatsonTcpClient client = new WatsonTcpClient(serverIp, serverPort, ServerConnected, ServerDisconnected, MessageReceived, true); bool runForever = true; while (runForever) { Console.Write("Command [? for help]: "); string userInput = Console.ReadLine(); 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(" sendasync send message to server asynchronously"); Console.WriteLine(" status show if client connected"); Console.WriteLine(" dispose dispose of the connection"); Console.WriteLine(" connect connect to the server if not connected"); Console.WriteLine(" reconnect disconnect if connected, then reconnect"); break; case "q": runForever = false; break; case "cls": Console.Clear(); break; case "send": Console.Write("Data: "); userInput = Console.ReadLine(); if (String.IsNullOrEmpty(userInput)) { break; } client.Send(Encoding.UTF8.GetBytes(userInput)); break; case "sendasync": Console.Write("Data: "); userInput = Console.ReadLine(); if (String.IsNullOrEmpty(userInput)) { break; } client.SendAsync(Encoding.UTF8.GetBytes(userInput)); break; case "status": if (client == null) { Console.WriteLine("Connected: False (null)"); } else { Console.WriteLine("Connected: " + client.IsConnected()); } break; case "dispose": client.Dispose(); break; case "connect": if (client != null && client.IsConnected()) { Console.WriteLine("Already connected"); } else { client = new WatsonTcpClient(serverIp, serverPort, ServerConnected, ServerDisconnected, MessageReceived, true); } break; case "reconnect": if (client != null) { client.Dispose(); } client = new WatsonTcpClient(serverIp, serverPort, ServerConnected, ServerDisconnected, MessageReceived, true); break; default: break; } } }
static void Main(string[] args) { server = new WatsonTcpServer("127.0.0.1", 9000); server.Events.ClientConnected += ServerClientConnected; server.Events.ClientDisconnected += ServerClientDisconnected; server.Events.MessageReceived += ServerMessageReceived; // server.StreamReceived = ServerStreamReceived; // server.Debug = true; server.Start(); client = new WatsonTcpClient("127.0.0.1", 9000); client.Events.ServerConnected += ServerConnected; client.Events.ServerDisconnected += ServerDisconnected; client.Events.MessageReceived += MessageReceived; // client.Events.StreamReceived = StreamReceived; // client.Debug = true; client.Start(); int msgSize = (1024 * 128); Console.Write("Message size (default 128KB): "); string userInput = Console.ReadLine(); if (!String.IsNullOrEmpty(userInput)) { msgSize = Convert.ToInt32(userInput); } int msgCount = 4; Console.Write("Message count (default 4): "); userInput = Console.ReadLine(); if (!String.IsNullOrEmpty(userInput)) { msgCount = Convert.ToInt32(userInput); } Console.WriteLine(""); Console.WriteLine("---"); Console.WriteLine("Sending messages from client to server..."); for (int i = 0; i < msgCount; i++) { string randomString = RandomString(msgSize); string md5 = Md5(randomString); Console.WriteLine("Client sending " + msgSize + " bytes: MD5 " + md5); client.Send(Encoding.UTF8.GetBytes(randomString)); } Console.WriteLine(""); Console.WriteLine("---"); string ipPort = server.ListClients().ToList()[0]; Console.WriteLine("Sending messages from server to client " + ipPort + "..."); for (int i = 0; i < msgCount; i++) { string randomString = RandomString(msgSize); string md5 = Md5(randomString); Console.WriteLine("Server sending " + msgSize + " bytes: MD5 " + md5); server.Send(ipPort, Encoding.UTF8.GetBytes(randomString)); } Console.WriteLine(""); Console.WriteLine("---"); Console.WriteLine("Press ENTER to exit"); Console.ReadLine(); server.Dispose(); client.Dispose(); }
static void Main(string[] args) { serverIp = Common.InputString("Server IP:", "127.0.0.1", false); serverPort = Common.InputInteger("Server port:", 9000, true, false); useSsl = Common.InputBoolean("Use SSL:", false); InitializeClient(); bool runForever = true; while (runForever) { Console.Write("Command [? for help]: "); string userInput = Console.ReadLine(); 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(" sendasync send message to server asynchronously"); Console.WriteLine(" status show if client connected"); Console.WriteLine(" dispose dispose of the connection"); Console.WriteLine(" connect connect to the server if not connected"); Console.WriteLine(" reconnect disconnect if connected, then reconnect"); Console.WriteLine(" psk set the preshared key"); Console.WriteLine(" auth authenticate using the preshared key"); Console.WriteLine(" debug enable/disable debug (currently " + client.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; } client.Send(Encoding.UTF8.GetBytes(userInput)); break; case "sendasync": Console.Write("Data: "); userInput = Console.ReadLine(); if (String.IsNullOrEmpty(userInput)) { break; } bool success = client.SendAsync(Encoding.UTF8.GetBytes(userInput)).Result; 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": if (client != null && client.Connected) { Console.WriteLine("Already connected"); } else { client = new WatsonTcpClient(serverIp, serverPort); client.ServerConnected = ServerConnected; client.ServerDisconnected = ServerDisconnected; client.MessageReceived = MessageReceived; client.Start(); } break; case "reconnect": if (client != null) { client.Dispose(); } client = new WatsonTcpClient(serverIp, serverPort); client.ServerConnected = ServerConnected; client.ServerDisconnected = ServerDisconnected; client.MessageReceived = MessageReceived; client.Start(); break; case "psk": presharedKey = Common.InputString("Preshared key:", "1234567812345678", false); break; case "auth": client.Authenticate(presharedKey); break; case "debug": client.Debug = !client.Debug; Console.WriteLine("Debug set to: " + client.Debug); break; default: break; } } }
private static void ClientToServer() { _Random = new Random((int)DateTime.Now.Ticks); _DataLargeBytes = InitByteArray(_DataLargeSize, 0x00); _DataLargeMd5 = BytesToHex(Md5(_DataLargeBytes)); _DataSmallBytes = InitByteArray(_DataSmallSize, 0x00); _DataSmallMd5 = BytesToHex(Md5(_DataSmallBytes)); Console.WriteLine("Large Data MD5: " + _DataLargeMd5); Console.WriteLine("Small Data MD5: " + _DataSmallMd5); Console.WriteLine("Starting in 3 seconds..."); _Server = new WatsonTcpServer(null, _ServerPort); _Server.Events.ClientConnected += ServerClientConnected; _Server.Events.ClientDisconnected += ServerClientDisconnected; if (!_UseStreams) { _Server.Events.MessageReceived += ServerMsgReceived; } else { _Server.Events.StreamReceived += ServerStreamReceived; } _Server.Callbacks.SyncRequestReceived = ServerSyncRequestReceived; _Server.Settings.MaxProxiedStreamSize = _MaxProxiedStreamSize; _Server.Settings.Logger = Console.WriteLine; _Server.Settings.DebugMessages = _Debug; _Server.Start(); Thread.Sleep(2000); _Client = new WatsonTcpClient("localhost", _ServerPort); _Client.Events.ServerConnected += ClientServerConnected; _Client.Events.ServerDisconnected += ClientServerDisconnected; if (!_UseStreams) { _Client.Events.MessageReceived += ClientMsgReceived; } else { _Client.Events.StreamReceived += ClientStreamReceived; } _Client.Callbacks.SyncRequestReceived = ClientSyncRequestReceived; _Client.Settings.MaxProxiedStreamSize = _MaxProxiedStreamSize; _Client.Settings.Logger = Console.WriteLine; _Client.Settings.DebugMessages = _Debug; _Client.Start(); Thread.Sleep(2000); Console.WriteLine("Press ENTER to exit"); for (int i = 0; i < _ClientThreads; i++) { Console.WriteLine("Starting client thread " + i); Task.Run(() => ClientTask()); } Console.WriteLine("Press ENTER after completion to view statistics"); Console.ReadLine(); _Client.Stop(); _Server.Stop(); _Server.Dispose(); _Client.Dispose(); Console.WriteLine("Success: " + _Success); Console.WriteLine("Failure: " + _Failure); }
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; } } }
private void ClientForm_FormClosing(object sender, FormClosingEventArgs e) { _Client.Dispose(); }
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 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 connection"); Console.WriteLine(" connect connect to the server if not connected"); Console.WriteLine(" reconnect disconnect if connected, then reconnect"); 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(" comp set the compression type, currently: " + client.Compression.ToString()); Console.WriteLine(" debug enable/disable debug (currently " + client.DebugMessages + ")"); 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 md": userInput = InputString("Data:", null, false); metadata = InputDictionary(); if (!client.Send(metadata, Encoding.UTF8.GetBytes(userInput))) { 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(metadata, "Hello!")) { 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(metadata, Encoding.UTF8.GetBytes(userInput)).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": if (client != null && client.Connected) { Console.WriteLine("Already connected"); } else { client = new WatsonTcpClient(serverIp, serverPort); client.ServerConnected += ServerConnected; client.ServerDisconnected += ServerDisconnected; client.MessageReceived += MessageReceived; client.Start(); } break; case "reconnect": ConnectClient(); break; case "psk": presharedKey = InputString("Preshared key:", "1234567812345678", false); break; case "auth": client.Authenticate(presharedKey); break; case "stats": Console.WriteLine(client.Stats.ToString()); break; case "stats reset": client.Stats.Reset(); break; case "comp": client.Compression = (CompressionType)(Enum.Parse(typeof(CompressionType), InputString("Compression [None|Default|Gzip]:", "None", false))); break; case "debug": client.DebugMessages = !client.DebugMessages; Console.WriteLine("Debug set to: " + client.DebugMessages); break; default: break; } } }