Hello people,
Would need a few ideas and remarks on what I have to do.
I have a server and I have clients (yay we got both) (both are scripted by me)
The server is posing me some problems, here it is the schematics of how it's working :
Server starts and start a TcpListener on port 13000 for instance.
Client 1 ask for connect on that port
Server responds with another port number (13001)
Client 1 closes acutal link
Server opens listener on 13001 on a new thread. Client is going to talk on 13001.
Everyone is happy they can talk about whatever-i-dont-care
Client 2 do a request on 13000 and server responds port 13002
Yay they also communicate
Now here's the onion : when Client disconnects/drop out/fly to the moon
I don't know how to abort a given thread and remove it from list
Here are the 3 classes :
1st is the server listening on 13000
Second are the listener for each client :Code:static class MyTcpServer { static public List<Thread> threads; static public void MainServer() { int portcounter = 13001; try { // Set the TcpListener on port 13000. Int32 port = 13000; IPAddress localAddr = IPAddress.Parse("127.0.0.1"); // TcpListener server = new TcpListener(port); TcpListener server = new TcpListener(localAddr, port); // Start listening for client requests. server.Start(); // Buffer for reading data Byte[] bytes = new Byte[256]; String data = null; // Enter the listening loop. while (true) { Console.Write("Waiting for a connection... "); TcpClient client = server.AcceptTcpClient(); Console.WriteLine("Connected!"); data = null; NetworkStream stream = client.GetStream(); int i; while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { // Translate data bytes to a ASCII string. data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); Console.WriteLine(String.Format("Received: {0}", data)); if (data.Equals("CONNECT_REQUEST")) { byte[] msg = System.Text.Encoding.ASCII.GetBytes(portcounter.ToString()); stream.Write(msg, 0, msg.Length); Console.WriteLine(String.Format("Sent: {0}", msg)); MyTcpServers m = new MyTcpServers(portcounter); Thread t = new Thread(new ThreadStart(m.StartServer)); threads.Add(t); portcounter++; } else { byte[] msg = System.Text.Encoding.ASCII.GetBytes("CONNECTION_ERROR"); stream.Write(msg, 0, msg.Length); Console.WriteLine(String.Format("Sent: {0}", data)); } } client.Close(); } } catch (SocketException e) { Console.WriteLine("SocketException: {0}", e); } Console.WriteLine("\nHit enter to continue..."); Console.Read(); } static public void StartServers() { while (true) { if (threads.Count != 0) { foreach (Thread t in MyTcpServer.threads) { if (!t.IsAlive) { t.Start(); } } } Console.WriteLine(threads.Count.ToString() + " active servers."); Thread.Sleep(250); } } }
and the main that is.... main ^^Code:class MyTcpServers { private Int32 port; private TcpListener server; public int waiting_for_connect=0; public MyTcpServers(int _port) { port = _port; } public void StartServer() { try { // Set the TcpListener on port 13000. IPAddress localAddr = IPAddress.Parse("127.0.0.1"); // TcpListener server = new TcpListener(port); server = new TcpListener(localAddr, port); // Start listening for client requests. server.Start(); // Buffer for reading data Byte[] bytes = new Byte[256]; String data = null; // Enter the listening loop. while (true) { waiting_for_connect = 0; Console.Write("Waiting for a connection on port " + port.ToString()); TcpClient client = server.AcceptTcpClient(); Console.WriteLine("Connected!"); data = null; NetworkStream stream = client.GetStream(); int i; while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { waiting_for_connect = -1; // Translate data bytes to a ASCII string. data = System.Text.Encoding.ASCII.GetString(bytes, 0, i); Console.WriteLine(String.Format("Received: {0}", data)); byte[] msg = System.Text.Encoding.ASCII.GetBytes("ACK"); stream.Write(msg, 0, msg.Length); Console.WriteLine(String.Format("Sent: {0}", msg)); } client.Close(); } } catch (SocketException) { server.Stop(); Console.Write("Stopping listening on port " + port.ToString()); //Console.WriteLine("SocketException: {0}", e); } } }
Yeah I know I'm still a full-100%-newbie in programmingCode:class MainApp2 { static void Main() { MyTcpServer.threads = new List<Thread>(); Console.WriteLine("Starting servers"); Thread t1 = new Thread(new ThreadStart(MyTcpServer.MainServer)); Thread t2 = new Thread(new ThreadStart(MyTcpServer.StartServers)); t1.Start(); t2.Start(); while (true) ; } }
Clients behaviors are this way : they start connecteing after each other (3 clients) and starts talking continously for 10 seconds after that they close one after another. Problem is that my console outputs still say "3 active servers" meaning the 3 threads are not dead while they are supposed to be dead. Anyone can point me the direction where I could aim the double-barrel-shootgun ?
Of course I never see this : Stopping listening on port " + port.ToString());



Reply With Quote

Bookmarks