multi threaded tcp listeners possible
-
helu.... i am writing code for a server which listens on a port 1000; and i start the listener as a thread. but the problem is that when the client connections increase then the server is unable to handle the requests and errors n exceoptions occur. i think the single thread is unable to handle that many conections. so is it possible to start multiple threads on the same port 1000; thanks
-
helu.... i am writing code for a server which listens on a port 1000; and i start the listener as a thread. but the problem is that when the client connections increase then the server is unable to handle the requests and errors n exceoptions occur. i think the single thread is unable to handle that many conections. so is it possible to start multiple threads on the same port 1000; thanks
No, you'd need more than 1 TCP socket on port 1000 (you can use a socket across multiple threads, but you shouldn't Listen() on multiple threads with the same socket) which is clearly illegal. However, you could handle the request on an other thread if you're not already doing that.
-
No, you'd need more than 1 TCP socket on port 1000 (you can use a socket across multiple threads, but you shouldn't Listen() on multiple threads with the same socket) which is clearly illegal. However, you could handle the request on an other thread if you're not already doing that.
thanks.. but "handle the request on an other thread" how can i do that is ti like statring a new thread when any new connection is made to the server
-
thanks.. but "handle the request on an other thread" how can i do that is ti like statring a new thread when any new connection is made to the server
There are several ways, starting a new thread wouldn't be the best of them (too much overhead), but you could use the threadpool Or, you could let the current thread handle the request but Before that, give the socket to an other thread to listen for more connections. As long as the listening for more connections doesn't happen on the same thread as the handling of the existing connections doesn't happen on the same thread. I'd probably loop the listener on its own thread and add the handling to the threadpool queue.
-
helu.... i am writing code for a server which listens on a port 1000; and i start the listener as a thread. but the problem is that when the client connections increase then the server is unable to handle the requests and errors n exceoptions occur. i think the single thread is unable to handle that many conections. so is it possible to start multiple threads on the same port 1000; thanks
The AcceptSocket method will return a TcpSocket object moved to a different port so you can begin the accept cycle again. No code has to be written to change the port. You can then queue the Socket objects and parse them in a separate thread. Or you can use Asynchronous sockets. In no case will you need more than two threads to handle the connections. Read the documentation here, http://msdn.microsoft.com/en-us/library/system.net.sockets.tcplistener.acceptsocket.aspx[^] and to verify that the ports are indeed automatically assigned for you run the command line method netstat.
Need custom software developed? I do C# development and consulting all over the United States. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane
-
thanks.. but "handle the request on an other thread" how can i do that is ti like statring a new thread when any new connection is made to the server
Hey Max, Harold is dead on - you cannot run multiple listeners you MUST have only one listener, however you can then run separate threads to actually convert the socket into a connection... I eventually used: http://www.codeproject.com/KB/IP/Generic_TCP_IP_server.aspx?fid=431256&select=2489157&fr=26#xx0xx[^] The code is easy to understand and very powerful - i've added encryption/authentication as i went through the code - took a couple of days and ive been trying to get a solid TCP stack working with async methods for a few weeks! However i did eventually used the async method for the listening socket, everything else is similar to the code in the link On the 'new connection' i pass the async socket result to a connection event in a separate thread - as the code above looks at - minimising the time taken to process new socket before your listener can go back and wait for the next connection. HAving said all that i haven't load tested it just yet :) Good luck, C
-
thanks.. but "handle the request on an other thread" how can i do that is ti like statring a new thread when any new connection is made to the server
Every time accept() returns successfully you will have a new client. You should start a new thread as pass the client socket to that thread so it can process indepently of the main thread. incidentally the only thing your main thread should do is open the socket, accept new connections, dispatch new threads, and look for shutdown. Move your action to worker threads and you will see a notable improvement in perfomance. However be advised you will need to take care of critical sections, and thread synchronization.
-
Every time accept() returns successfully you will have a new client. You should start a new thread as pass the client socket to that thread so it can process indepently of the main thread. incidentally the only thing your main thread should do is open the socket, accept new connections, dispatch new threads, and look for shutdown. Move your action to worker threads and you will see a notable improvement in perfomance. However be advised you will need to take care of critical sections, and thread synchronization.
thank you very mch...