*
The moose likes Sockets and Internet Protocols and the fly likes Implementing java server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Implementing java server" Watch "Implementing java server" New topic
Author

Implementing java server

mag nesum
Greenhorn

Joined: Apr 20, 2009
Posts: 5
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.
mag nesum
Greenhorn

Joined: Apr 20, 2009
Posts: 5
If you did not get what I wanted to say then in short-

Can you briefly describe what would be a decent design for such a server.
Can I write to sockets from multiple threads at the same time or should I use synchronization?

Just share your ideas if you have some relative experience.
Doua Beri
Ranch Hand

Joined: Sep 24, 2009
Posts: 60
Hi. I was just about to create a new thread about a chat server with large number of threads when I saw this one.
I have to say I didn't encountered your problem and I think your implementation eats a lot of resources. you said that you send a message to all clients every 5 seconds, that means if you have 10k users you just send 10k messages/5s besides other messages that are being send (for example communication between the clients).
I 'm not sure how you implemented your communication protocol, but i use something like this:




Worked very well for me. You can also make your client to ping the server every x seconds and on the server side you can delete the client from the list if he didn't received any response in 2x seconds
mag nesum
Greenhorn

Joined: Apr 20, 2009
Posts: 5
Thank you for sharing your idea. It really sounds as a great one to try.
You are right about the performance - it is pretty slow even when I connect 1000 clients. I will try to implement it the way you described.
Thanks!

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implementing java server