static void Main(string[] args) { // Create and start a TCP listener on port 8080 var localAddress = IPAddress.Parse(ServerIp); var tcpListener = new TcpListener(localAddress, port); TcpClient clientSocket; tcpListener.Start(); Console.WriteLine("Server started..."); int counter = 0; while (true) { counter += 1; clientSocket = tcpListener.AcceptTcpClient(); Console.WriteLine($"Client No: {counter} started."); HandleClient client = new HandleClient(); client.StartClient(clientSocket, Convert.ToString(counter)); } clientSocket.Close(); tcpListener.Stop(); Console.ReadLine(); }
static void Main(string[] args) { TcpListener serverSocket = new TcpListener(IPAddress.Any, 8080); TcpClient clientSocket = default(TcpClient); int counter = 0; serverSocket.Start(); Console.WriteLine("Server Started..."); while (true) { counter += 1; clientSocket = serverSocket.AcceptTcpClient(); byte[] bytesFrom = new byte[1024]; string dataFromClient = null; NetworkStream networkStream = clientSocket.GetStream(); networkStream.Read(bytesFrom, 0, 1024); dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom); Console.Write(dataFromClient); try { clientsList.Add(dataFromClient, clientSocket); } catch { Console.WriteLine("Client already added"); } HandleClient client = new HandleClient(); client.StartClient(clientSocket, dataFromClient, clientsList); } }
static void Main(string[] args) { TcpListener ServerSocket = new TcpListener(IPAddress.Parse("127.0.0.1"), 1000); TcpClient ClientSocket = default(TcpClient); ServerSocket.Start(); Console.WriteLine("Server Started ..."); while (true) { try { ClientSocket = ServerSocket.AcceptTcpClient(); byte[] BytesFrom = new byte[10025]; string DataFromClient = null; NetworkStream NetworkStream = ClientSocket.GetStream(); NetworkStream.Read(BytesFrom, 0, 2000); DataFromClient = System.Text.Encoding.ASCII.GetString(BytesFrom); DataFromClient = DataFromClient.Substring(0, DataFromClient.IndexOf("$")); ClientsList.Add(DataFromClient, ClientSocket); Guid GuidName = Guid.Parse(DataFromClient); Console.WriteLine(GuidName + " has just joined application "); HandleClient Client = new HandleClient(); Client.StartClient(ClientSocket, DataFromClient, ClientsList); } catch { ClientSocket.Close(); ServerSocket.Stop(); Console.WriteLine("exit"); Console.ReadLine(); } } }