public SimpleClient(string host, int port) { Contract.Requires(!string.IsNullOrEmpty(host)); Contract.Requires(0 < port && port < 65536); var address = host == "localhost" ? IPAddress.Loopback : IPAddress.Parse(host); connection = new TcpClientConnection(new IPEndPoint(address, port)); connection.SocketDisconnected += SocketDisconnected; connection.DataReceived += DataReceived; }
private void DataReceived(object sender, TcpClientConnection.DataReceivedEventArgs e) { try { Console.WriteLine("Data received: {0}", e.Data); var element = XElement.Parse(e.Data); Message message = messageConverter.ConvertFromXml(element); Console.WriteLine("Message received: {0}", message); } catch (Exception ex) { Console.WriteLine("Error processing data: {0}", ex); } }
private void SocketDisconnected(object sender, TcpClientConnection.SocketDisconnectedEventArgs e) { Console.WriteLine("Connection to server failed!"); if (e.Error != null) { Console.WriteLine("Error: {0}", e.Error); } }