C
Chris Berg
My client/server application is using plain sockets, in the
traditional way:
ServerSocket ss=new ServerSocket(port);
while (true){
new Handler(ss.accept());
}
......
class Handler{
Socket s;
public Handler(Socket s){
this.s=s;
new Thread(this).start();
}
public void run(){
while(true){
read a command from s's inputstream;
interpret it;
write a reply to s's outputstream;
if (user logs off) break;
}
}
}
Of course, there is more to it - setting the socket timeout, etc., but
I'm sure you get the picture.
I worry about how my server will react to many simultaneous clients,
each with not too much activity, but there may be a lot of concurrent
threads (thousands), consuming many system ressources.
Now, I'f noticed the new java.nio classes that seem to handle multiple
clients in a more efficient way. Only, I'm concerned that I will have
to completely re-write the program logic to make it fit into the
scheme. As everything runs on the same thread, I must program it in a
more event-handler-oriented manner, where I need to save a state
vector for every client, which would roughly correspond to the current
program location of each of the old threads. Or is there maybe a
smarter way to do that?
Also, some of the command-handling code can be blocking (ie.
Thread.sleep() or disk file I/O). This means that I'll have to leave
the handler and go back to the Selector and service incoming commands
before having finished current commands. What a mess! It must end up
with some ugly code!??
Chris
traditional way:
ServerSocket ss=new ServerSocket(port);
while (true){
new Handler(ss.accept());
}
......
class Handler{
Socket s;
public Handler(Socket s){
this.s=s;
new Thread(this).start();
}
public void run(){
while(true){
read a command from s's inputstream;
interpret it;
write a reply to s's outputstream;
if (user logs off) break;
}
}
}
Of course, there is more to it - setting the socket timeout, etc., but
I'm sure you get the picture.
I worry about how my server will react to many simultaneous clients,
each with not too much activity, but there may be a lot of concurrent
threads (thousands), consuming many system ressources.
Now, I'f noticed the new java.nio classes that seem to handle multiple
clients in a more efficient way. Only, I'm concerned that I will have
to completely re-write the program logic to make it fit into the
scheme. As everything runs on the same thread, I must program it in a
more event-handler-oriented manner, where I need to save a state
vector for every client, which would roughly correspond to the current
program location of each of the old threads. Or is there maybe a
smarter way to do that?
Also, some of the command-handling code can be blocking (ie.
Thread.sleep() or disk file I/O). This means that I'll have to leave
the handler and go back to the Selector and service incoming commands
before having finished current commands. What a mess! It must end up
with some ugly code!??
Chris