• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
M Burke
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
dos.close();
s1out.close();
s1.close();
...to 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;
import java.net.*;
import java.io.*;

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");
try{
// 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);
t.start();
}
}
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(){
try{
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 = dis.read(requ);
//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();
dos.write(resp);
}else{
resp = "No data sent".getBytes();
dos.write(resp);
}
//Close the connection, but not the server socket
dos.close();
s1out.close();
s1.close();
}catch(IOException e){
System.err.println("Communication Error: " + e.getMessage());
}


}
}
[ December 27, 2004: Message edited by: M Burke ]
 
Joe Ess
Bartender
Pie
Posts: 9266
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
you li
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic