Hello guys,
so I am implementing a
java server which communicates with a lot of mobile devices (phones mainly) over tcp protocol.
The idea is to save traffic and not to overload server very much when a lot of clients are connected so we have decided to do it as follows:
Server keeps a list of connections and pings (sends a short message) them every 5 seconds. If the ping does not succeed then server assumes that the connection is lost and just removes it from the list.
Every ping command is also an opportunity for a client to send some data to the server. So it works like this - server sends "P" to the client and awaits response. Client then responds - sends the contents that he wants server to receive. If client has nothing to send - it just responds by sending "N" to the server.
So this way server has many threads which handle all client connections. Server checks regularly if clients are alive and has some information that should be delivered to the server. After doing it -
thread sleeps for a specified time interval and later does the same thing again.
The problem is that there are more threads which access client connections and sends data to them - "ping thread" just runs in a background. And currently I am encountering some strange "SocketException: Connection Reset" exceptions when the number of clients reach 500 and ping interval is lower than 3 seconds.
I just wanted to consult with you guys who did some socket programming whether we could save some computation power and traffic by using this approach. Or should we just listen to the client socket connections all the time without Thread.sleep() ? How can we then detect if a connection has been lost without constantly pinging it?
I am asking this because so far I was only able to connect around 1000 clients and the server response time was really really slow.
Thanks.