This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes How do I get my server to accept multiple requests from  one client connection? 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 "How do I get my server to accept multiple requests from  one client connection?" Watch "How do I get my server to accept multiple requests from  one client connection?" New topic

How do I get my server to accept multiple requests from one client connection?

M Burke
Ranch Hand

Joined: Jun 25, 2004
Posts: 395
My server works fine if it handles a single TCP connection, then a single command string. But if I do not close the connection and send another command, the accept() ignores it.

So if I comment out this...
s1.close(); keep the connection open, the server will not accept any more connections from the same client.

How can I get my server to accept a single connection from a client, VB and C++ in this case, and process multiple requests from the same connection?

package BenServer;

public class Ben {

public static void main(String[] args)
ServerSocket s = null;
Socket s2;

Integer i = new Integer(args[0]);
int z = i.intValue();
try {
s = new ServerSocket(z);
} catch (IOException e) {
// ignore

System.out.println("***Starting Ben Server on port " + z + "***" + "\r\n");
// Run the listen/accept loop forever
while (true)
// Wait here and listen for a connection
s2 = s.accept();
//create thread and go back to listening
ProcessDoc jps = new ProcessDoc(s2);
Thread t = new Thread(jps);
catch(Exception e)
System.err.println("General Error: " + e.getMessage());
}//end main

class ProcessDoc implements Runnable{

public ProcessDoc(Socket s) {s1 = s;}

Socket s1;
InputStream s1In;
DataInputStream dis;
OutputStream s1out;
DataOutputStream dos;
String sresp;
byte[]requ = new byte[4000];
byte[]resp = new byte[4000];

static int called = 0;

public void run(){
s1In = s1.getInputStream();
dis = new DataInputStream(s1In);
//Get a communication stream associated with the socket
s1out = s1.getOutputStream();
dos = new DataOutputStream (s1out);
//Get request from client
int num =;
//Get postal Info
if(num > 0){
String trequ = new String(requ);
sresp = trequ;
sresp = sresp.trim();

System.out.println("\nRequest from client: " + sresp);
called = called + 1;
sresp = sresp + "Added by Ben " + called + " times ";
System.out.println("\n" + sresp);

//send response back to client
resp = sresp.getBytes();
resp = "No data sent".getBytes();
//Close the connection, but not the server socket
}catch(IOException e){
System.err.println("Communication Error: " + e.getMessage());

[ December 27, 2004: Message edited by: M Burke ]
Joe Ess

Joined: Oct 29, 2001
Posts: 9137

You didn't give us the client code so I must guess. Put a couple of System.out.println's around where the client reads the response from the server the first time and then attempts to make its second connection to the server (test this those three lines on the server commented out). I'll bet the client doesn't even attempt a second connection.

[How To Ask Questions On JavaRanch]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Your run() method is coded to read one request and send one response and exit. It would need to have a loop of some kind:

I haven't tried this, but I think one tricky bit will be knowing when you have read a complete request and it's time to process it. You might put the length of the request in the first "n" bytes, or put a "fence" at the end of the request. Did that make sense?

BTW: For grins see if you can make the code almost exactly those 10 lines, plus a few for brackets. A line like "get request" would turn into a method call: request = getRequest() and so on. Let us know what you come up with!
[ December 28, 2004: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
you li

Joined: Jan 17, 2005
Posts: 3

this is the code of my first tryout server in java i can with the proper client send "ik" and get "ben", send "jij" and get "bent".(im dutch) it works fine and i can send and receive as many time i like. maybe its of some use.

i do however have another question. how do i make it possible for my server to accept more than one client simultanious?
[ January 17, 2005: Message edited by: youri lima ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Looks like you're having the right kind of fun with sockets. To handle multiple clients, move the accept() outside the runnable.

This is a very common setup. I'd probably put the request handler in a separate class, but what you have works.

"while running" and your "while true" would both run forever. You might have your message reader recognize another value to close one client connection, and another to change running to false and shut down the program. How your handler gets access to the running variable is an interesting problem I'll leave to you.

BTW: The common Java convention - that will help others read your code - is to capitalize class names, eg MyServer

Let us see what you wind up with!
I agree. Here's the link:
subject: How do I get my server to accept multiple requests from one client connection?
It's not a secret anymore!