File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Sockets and Internet Protocols and the fly likes Need some advice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Need some advice" Watch "Need some advice" New topic

Need some advice

mag nesum

Joined: Apr 20, 2009
Posts: 5

I am developing a connection part of the project which will deal with various services provided to mobile devices. I need the server to be able to handle thousands of clients simultaneously.

In order to make servers life easier I am thinking weather it would be possible to make everything like this:

1. Server listens for clients
2. Client connects to the server
3. Server then adds client's Socket to the list (hashtable) of active/connected clients along with clients ID and no longer listens on that Socket (Client is listening on it's socket everytime)
4. When server has some information that needs to be sent to the client he looks up the corresponding socket (using the client ID) and sends the data.

To make sure that the clients are online server sends a ping every 10 minutes or so... If the client does not respond then server removes it from the list...
The need here is to keep the communication rate between server and clients as low as possible...

Now while I was trying to implement this idea I encountered the following difficulties:
While the server has its PrintWriter open (used to send messages to the client) the client blocks on the BufferedReader.readLine() method. However as soon as I close the PrintWriter on the server side the client is not blocking anymore- it then reads a string that is null. And since I have an infinite loop the thread keeps reading null and uses 50% of the cpu. I have made the following correction but I am not sure if it is a good idea...
String s = bufferedReader.readLine();

if(s == null)

So can anyone suggest me a good strategy for the described scenario?
Is there a method to establish a connection with a client. Close that socket and put it to the list and when need arises temporarily open that socket, send some information and close it again to keep the communication between the endpoints to it's minimum.
The client could listen to it's socket every time...

Hope you could understand my English...
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Not sure what's going, but since you mention readLine and PrintWriter, you should read Don't println to a Socket.
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

You should also not continue to listen to a closed stream. It can't be reopened so there's never going to be more data.

[How To Ask Questions On JavaRanch]
mag nesum

Joined: Apr 20, 2009
Posts: 5
One more question:
closing input or output stream of a socket closes the socket itself. Is there an option to avoid it???
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

There's several options:
1. use one connection per request. This is how HTTP works. The Java TCP library actually keeps a persistent connection under the covers, so this isn't as much a performance hit as it sounds.
2. use a persistent connection and create a protocol which delineates the start and end of messages and data. FTP is an example of this kind of protocol.
3. Use RMI. RMI (remote method invocation) wraps the ugly details of dealing with sockets in a clean object-oriented interface. This lets you focus on solving the problem you are working on rather than trying to figure out low-level socket protocols.
I agree. Here's the link:
subject: Need some advice
It's not a secret anymore!