// прослушивание входящих подключений protected internal void Listen() { try { tcpListener = new TcpListener(IPAddress.Any, 11000); tcpListener.Start(); Console.WriteLine("Server started on port 11000"); while (true) { TcpClient tcpClient = tcpListener.AcceptTcpClient(); ClientObject clientObject = new ClientObject(tcpClient, this); Task clientTask = Task.Run(() => clientObject.Process()); } } catch (Exception ex) { Console.WriteLine(ex.Message); Disconnect(); } }
protected internal void Listen() { try { tcpListener = new TcpListener(IPAddress.Any, port); tcpListener.Start(); Console.WriteLine("Server is run. Wait for connections...\n"); while (true) { TcpClient tcpClient = tcpListener.AcceptTcpClient(); ClientObject clientObject = new ClientObject(tcpClient, this, new DbHelper()); Thread clientThread = new Thread(async() => await clientObject.Process()); clientThread.Start(); } } catch (Exception ex) { Console.WriteLine(ex.Message); Disconnect(); } }
private async void StartServer() { //textBox1.Text += ("Ожидание подключений..."); IPHostEntry ipHost = Dns.GetHostEntry("localhost"); IPAddress ipAddr = ipHost.AddressList[0]; const int port = 8888; TcpListener listener = new TcpListener(IPAddress.Parse("127.0.0.1"), port); try { listener.Start(); textBox1.Text += ("Ожидание подключений..."); while (true) { TcpClient client = await listener.AcceptTcpClientAsync(); ClientObject clientObject = new ClientObject(client); //создаем новый поток для обслуживания нового клиента Thread clientThread = new Thread(() => clientObject.Process(textBox1)); textBox1.Text += "\r\n New client started!"; clientThread.Start(); } } catch (Exception ex) { Console.WriteLine(ex.Message); } finally { if (listener != null) { listener.Stop(); } } }