0
votes

i've tried to build a socket server in php, but found out that because of the lack of multithreading capability in php, if the server is loaded with connection (lets say even a small amount of 300 connection), the server will be very slow.

i've switched to java, and built a multithreading socket server, and tried to overloaded it with 100 connection in 10 seconds, and i must say it took it bravely.. my own pc (which made the overload) became real slow during the process but server looked like he was ok.

since i'm building a server which should run lots of connections simultaneously, i'm trying to make efficient, and not to leave any ghost connection open.

there for i'm implementing a "Keep Alive" method and my question is as follow: if i have 600 open connections opened in my server, and obviously it takes the server time to run between all threads to check keep alive and even to get the keep alive message from the client - what should be the appropriate time to check if a keep alive message was sent. i've thought somewhere around 2 minutes - so the server want be to busy, and from the other hand i don't want my clients to send data too often.. any suggestions ?

1
Are you using blocking sockets with each thread per socket? I would recommend switching to java.nio for your case since you can get away with single thread doing the job for you and that should improve your application's performance drastically.Buchi
You mean in my server side? Can you give me some more information please? For now im accepting a socket in my main function and starts a new thread to handle itAsaf Nevo
i've read about SocketChannel object and ServerSocketChannel object. if i understand right, it has better performance because a new thread is opening only where there is a new task to do (read, write etc..) so basically most of the time less threads will run simultaneously than in my code which each connection have a thread regardless if it has task to perfrom or not.. is this correct ?Asaf Nevo
Yes, server side you can use java.nio and a single thread can do the operation (read, write, etc as you said) for you. Go through tutorials like ibm.com/developerworks/java/tutorials/j-nio for more understanding please.Buchi

1 Answers

0
votes

600 open connections. Assuming that a keep-alive is sent every 2 minutes, 120 seconds, that means about 5 threads becoming ready per second to send a keep-alive. Assuming that all the connections are up, the five threads wil beome ready again shortly after to process the echo from the peer. So, that's about ten threads/sec need to run to support the keep-alives.

That's an insignificant loading.

If the clients are busy, then those clients will not be sent keep-alives, so the loading for keep-alives drops as clients become usefully busy.

You sound like you should be OK.