public void SendReceiveTest() { var server = new TcpServer(); server.ErrorEvent += Server_ErrorEvent; server.DataReceivedEvent += Server_DataReceivedEvent; var epList = new List <TcpServerLocalEndpoint>(); var ipEp1 = (new TcpServerLocalEndpoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234), 1)); epList.Add(ipEp1); server.Start(epList); var client = new TcpClient(); client.DataReceivedEvent += Client_DataReceivedEvent; client.Start(ipEp1.EndPoint); Thread.Sleep(200); for (int i = 1; i < 10; i++) { var data = BitConverter.GetBytes(i); ServerDataReceivedEvent.Clear(); Assert.AreEqual(0, ServerDataReceivedEvent.Count); client.SendData(data); Thread.Sleep(100); Assert.AreEqual(1, ServerDataReceivedEvent.Count); Assert.AreEqual(data.Length, ServerDataReceivedEvent[0].Data.Length); for (int j = 0; j < data.Length; j++) { Assert.AreEqual(data[j], ServerDataReceivedEvent[0].Data[j]); Assert.AreEqual(client.ExchangeInterface.LocalEndPoint, ServerDataReceivedEvent[0].RemoteEndPoint); Assert.AreEqual(client.ExchangeInterface.RemoteEndPoint, ServerDataReceivedEvent[0].LocalEndPoint); } } for (int i = 1; i < 10; i++) { var data = BitConverter.GetBytes(i); ClientDataReceivedEvent.Clear(); Assert.AreEqual(0, ClientDataReceivedEvent.Count); server.Write(data, client.ExchangeInterface.RemoteEndPoint, client.ExchangeInterface.LocalEndPoint); Thread.Sleep(100); Assert.AreEqual(1, ClientDataReceivedEvent.Count); Assert.AreEqual(data.Length, ClientDataReceivedEvent[0].Data.Length); for (int j = 0; j < data.Length; j++) { Assert.AreEqual(data[j], ClientDataReceivedEvent[0].Data[j]); Assert.AreEqual(ipEp1.EndPoint, ClientDataReceivedEvent[0].RemoteEndPoint); } } server.Dispose(); }
public void DataTraceTest() { var server = new TcpServer(); server.ErrorEvent += Server_ErrorEvent; server.ReceiveDataTraceEvent += Server_ReceiveDataTraceEvent; server.SendDataTraceEvent += Server_SendDataTraceEvent; server.DataReceivedEvent += Server_DataReceivedEvent; var epList = new List <TcpServerLocalEndpoint>(); var ipEp1 = (new TcpServerLocalEndpoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 2345), 1)); epList.Add(ipEp1); server.Start(epList); var client = new TcpClient(); client.ConnectedEvent += Client_ConnectedEvent; client.DisconnectedEvent += Client_DisconnectedEvent; client.DataReceivedEvent += Client_DataReceivedEvent; client.ReceiveDataTraceEvent += Client_ReceiveDataTraceEvent; client.SendDataTraceEvent += Client_SendDataTraceEvent; ClientConnectedEventEventArgs = null; client.Start(ipEp1.EndPoint); var waitStart = DateTime.Now; while (ClientConnectedEventEventArgs == null) { var delta = DateTime.Now - waitStart; if (delta.TotalSeconds > 5) { Assert.Fail("ClientConnectedEventEventArgs Timeout"); } } client.DataTrace = false; server.DataTrace = false; for (int i = 1; i < 10; i++) { var data = BitConverter.GetBytes(i); ServerDataReceivedEvent.Clear(); ClientSendDataTraceEvent.Clear(); ServerReceiveDataTraceEvent.Clear(); Assert.AreEqual(0, ServerDataReceivedEvent.Count); Assert.AreEqual(0, ClientSendDataTraceEvent.Count); Assert.AreEqual(0, ServerReceiveDataTraceEvent.Count); client.SendData(data); Thread.Sleep(100); Assert.AreEqual(1, ServerDataReceivedEvent.Count); Assert.AreEqual(data.Length, ServerDataReceivedEvent[0].Data.Length); Assert.AreEqual(0, ClientSendDataTraceEvent.Count); Assert.AreEqual(0, ServerReceiveDataTraceEvent.Count); for (int j = 0; j < data.Length; j++) { Assert.AreEqual(data[j], ServerDataReceivedEvent[0].Data[j]); Assert.AreEqual(client.ExchangeInterface.LocalEndPoint, ServerDataReceivedEvent[0].RemoteEndPoint); Assert.AreEqual(client.ExchangeInterface.RemoteEndPoint, ServerDataReceivedEvent[0].LocalEndPoint); } } for (int i = 1; i < 10; i++) { var data = BitConverter.GetBytes(i); ClientDataReceivedEvent.Clear(); ServerSendDataTraceEvent.Clear(); ClientReceiveDataTraceEvent.Clear(); Assert.AreEqual(0, ClientDataReceivedEvent.Count); Assert.AreEqual(0, ServerSendDataTraceEvent.Count); Assert.AreEqual(0, ClientReceiveDataTraceEvent.Count); server.Write(data, client.ExchangeInterface.RemoteEndPoint, client.ExchangeInterface.LocalEndPoint); Thread.Sleep(100); Assert.AreEqual(1, ServerDataReceivedEvent.Count); Assert.AreEqual(data.Length, ServerDataReceivedEvent[0].Data.Length); Assert.AreEqual(0, ServerSendDataTraceEvent.Count); Assert.AreEqual(0, ClientReceiveDataTraceEvent.Count); for (int j = 0; j < data.Length; j++) { Assert.AreEqual(data[j], ClientDataReceivedEvent[0].Data[j]); Assert.AreEqual(ipEp1.EndPoint, ClientDataReceivedEvent[0].RemoteEndPoint); } } client.DataTrace = true; server.DataTrace = true; for (int i = 1; i < 10; i++) { var data = BitConverter.GetBytes(i); ServerDataReceivedEvent.Clear(); ClientSendDataTraceEvent.Clear(); ServerReceiveDataTraceEvent.Clear(); Assert.AreEqual(0, ServerDataReceivedEvent.Count); Assert.AreEqual(0, ClientSendDataTraceEvent.Count); Assert.AreEqual(0, ServerReceiveDataTraceEvent.Count); client.SendData(data); Thread.Sleep(100); Assert.AreEqual(1, ServerDataReceivedEvent.Count); Assert.AreEqual(data.Length, ServerDataReceivedEvent[0].Data.Length); Assert.AreEqual(1, ClientSendDataTraceEvent.Count); Assert.AreEqual(1, ServerReceiveDataTraceEvent.Count); for (int j = 0; j < data.Length; j++) { Assert.AreEqual(data[j], ServerDataReceivedEvent[0].Data[j]); Assert.AreEqual(client.ExchangeInterface.LocalEndPoint, ServerDataReceivedEvent[0].RemoteEndPoint); Assert.AreEqual(client.ExchangeInterface.RemoteEndPoint, ServerDataReceivedEvent[0].LocalEndPoint); Assert.AreEqual(data[j], ClientSendDataTraceEvent[0].Data[j]); Assert.AreEqual(data[j], ServerReceiveDataTraceEvent[0].Data[j]); } Assert.AreEqual(ipEp1.EndPoint, ClientSendDataTraceEvent[0].RemoteEndPoint); Assert.AreEqual(client.ExchangeInterface.LocalEndPoint, ServerReceiveDataTraceEvent[0].RemoteEndPoint); Assert.AreEqual(client.ExchangeInterface.RemoteEndPoint, ServerReceiveDataTraceEvent[0].LocalEndPoint); } for (int i = 1; i < 10; i++) { var data = BitConverter.GetBytes(i); ClientDataReceivedEvent.Clear(); ServerSendDataTraceEvent.Clear(); ClientReceiveDataTraceEvent.Clear(); Assert.AreEqual(0, ClientDataReceivedEvent.Count); Assert.AreEqual(0, ServerSendDataTraceEvent.Count); Assert.AreEqual(0, ClientReceiveDataTraceEvent.Count); server.Write(data, client.ExchangeInterface.RemoteEndPoint, client.ExchangeInterface.LocalEndPoint); Thread.Sleep(100); Assert.AreEqual(1, ServerDataReceivedEvent.Count); Assert.AreEqual(data.Length, ServerDataReceivedEvent[0].Data.Length); Assert.AreEqual(1, ServerSendDataTraceEvent.Count); Assert.AreEqual(1, ClientReceiveDataTraceEvent.Count); for (int j = 0; j < data.Length; j++) { Assert.AreEqual(data[j], ClientDataReceivedEvent[0].Data[j]); Assert.AreEqual(ipEp1.EndPoint, ClientDataReceivedEvent[0].RemoteEndPoint); Assert.AreEqual(data[j], ServerSendDataTraceEvent[0].Data[j]); Assert.AreEqual(data[j], ClientReceiveDataTraceEvent[0].Data[j]); } Assert.AreEqual(client.ExchangeInterface.LocalEndPoint, ServerReceiveDataTraceEvent[0].RemoteEndPoint); Assert.AreEqual(client.ExchangeInterface.RemoteEndPoint, ServerReceiveDataTraceEvent[0].LocalEndPoint); Assert.AreEqual(ipEp1.EndPoint, ClientReceiveDataTraceEvent[0].RemoteEndPoint); } server.Dispose(); }
public void SendReceiveTest() { var serverName = "TestServer"; using (X509Certificate serverCert = MakeCert(serverName)) { var server = new TcpServer(); server.DataReceivedEvent += Server_DataReceivedEvent; var epList = new List <TcpServerLocalEndpoint>(); var ipEp1 = (new TcpServerLocalEndpoint(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234), 1)); epList.Add(ipEp1); server.Start(epList, serverCert, false, SslProtocols.None, false, EncryptionPolicy.RequireEncryption); X509Certificate clientCert = MakeCert(serverName); var client = new TcpClient(); client.DataReceivedEvent += Client_DataReceivedEvent; client.Start(ipEp1.EndPoint, serverName, clientCert, SslProtocols.None, false, EncryptionPolicy.RequireEncryption); Thread.Sleep(200); for (int i = 1; i < 10; i++) { var data = BitConverter.GetBytes(i); ServerDataReceivedEvent.Clear(); Assert.AreEqual(0, ServerDataReceivedEvent.Count); client.SendData(data); var waitStart = DateTime.Now; while (ServerDataReceivedEvent.Count == 0) { var delta = DateTime.Now - waitStart; if (delta.TotalMilliseconds > 100) { Assert.Fail("Data not received"); } } Assert.AreEqual(1, ServerDataReceivedEvent.Count); Assert.AreEqual(data.Length, ServerDataReceivedEvent[0].Data.Length); for (int j = 0; j < data.Length; j++) { Assert.AreEqual(data[j], ServerDataReceivedEvent[0].Data[j]); Assert.AreEqual(client.ExchangeInterface.LocalEndPoint, ServerDataReceivedEvent[0].RemoteEndPoint); Assert.AreEqual(client.ExchangeInterface.RemoteEndPoint, ServerDataReceivedEvent[0].LocalEndPoint); } } for (int i = 1; i < 10; i++) { var data = BitConverter.GetBytes(i); ClientDataReceivedEvent.Clear(); Assert.AreEqual(0, ClientDataReceivedEvent.Count); server.Write(data, client.ExchangeInterface.RemoteEndPoint, client.ExchangeInterface.LocalEndPoint); var waitStart = DateTime.Now; while (ClientDataReceivedEvent.Count == 0) { var delta = DateTime.Now - waitStart; if (delta.TotalMilliseconds > 100) { Assert.Fail("Data not received"); } } Assert.AreEqual(1, ClientDataReceivedEvent.Count); Assert.AreEqual(data.Length, ClientDataReceivedEvent[0].Data.Length); for (int j = 0; j < data.Length; j++) { Assert.AreEqual(data[j], ClientDataReceivedEvent[0].Data[j]); Assert.AreEqual(ipEp1.EndPoint, ClientDataReceivedEvent[0].RemoteEndPoint); } } server.Dispose(); client.Dispose(); clientCert.Dispose(); } }