public void StartServer() { TcpServer = new TcpServer(); TcpServer.OnStart += () => AddLogLine("SERVER STARTED", "Server"); TcpServer.OnCommandReceived += (connectionId, command) => { AddLogLine("COMMAND RECEIVED " + command, "Server"); if (command.StartsWith("HELLO")) { TcpServer.SendCommand(connectionId, "HELLO CLIENT"); TcpServer.WaitForCommand(connectionId); } else if (command.StartsWith("FILE")) { TcpServer.WaitForFile(connectionId, int.Parse(command.Split(':')[1])); } else if (command.StartsWith("GETFILE")) { TcpServer.SendFile(connectionId, new byte[] { 72, 69, 76, 76, 79, 32, 83, 62, 67, 72, 69, 76, 76, 79, 32, 83, 62, 67, 72, 69, 76, 76, 79, 32, 83, 62, 67 }); AddLogLine("FILE SEND STARTED", "Server"); } else if (command.StartsWith("QUIT")) { TcpServer.CloseConnection(connectionId); AddLogLine("CONNECTION CLOSED", "Server"); } else { TcpServer.WaitForCommand(connectionId); } }; TcpServer.OnCommandSent += (command, bytesSent) => AddLogLine("COMMAND SENT " + command, "Server"); TcpServer.OnClientDisconnected += (connectionId, endPoint) => AddLogLine("DISCONNECTED OF CLIENT", "Server"); TcpServer.OnFileReceived += (connectionId, fileData) => { File.WriteAllBytes(ApplicationPaths.CurrentPath + @"Server.txt", fileData); AddLogLine("FILE RECEIVED", "Server"); TcpServer.SendCommand(connectionId, "MESSAGE:FILE RECEIVED IN SERVER"); TcpServer.WaitForCommand(connectionId); }; TcpServer.OnFileSent += (connectionId, fileData) => { AddLogLine("FILE SENT", "Server"); TcpServer.WaitForCommand(connectionId); }; TcpServer.OnClientConnected += connectionId => AddLogLine("CONNECTED WITH CLIENT", "Server"); TcpServer.Start(); }
public void TestTwoServerSamePort() { var randomPort = 56000 + new Random().Next(1, 999); var tcpServer1 = new TcpServer(); tcpServer1.Start(randomPort, IPAddress.Loopback); AssertIsTrue(() => tcpServer1.Running); var tcpServer2 = new TcpServer(); tcpServer2.OnErrorOcurred += delegate { }; tcpServer2.Start(randomPort, IPAddress.Loopback); AssertIsTrue(() => !tcpServer2.Running); var tcpClient1 = new TcpClient(); tcpClient1.Connect(IPAddress.Loopback, randomPort); tcpClient1.Disconnect(); tcpServer1.Stop(); }