R
R
Hello everybody.
I've got theoretical problems with the idea of MultiThreaded Servers.
I want my server to run as separate threads for the clients.
I'm not enough experienced with network programming and Java itself.
Well I have a while loop like this (I removed all try/catch
statements):
boolean doIt = true;
while (doIt) {
Socket client = null;
client = server.accept();
is = client.getInputStream();
os = client.getOutputStream();
/* server stuff here... in separate thread */
Work w = new Work(is, os);
w.start();
}
And now my questions:
1) Client socket is created as a local variable - is it created every
loop iteration? I mean what happens if I started a thread 'Work' and a
new client will show up? Socket client will be created as a new
object? Are previous streams inside work thread safe? New thread will
have its own streams?
(well maybe it's not about network programming but about Java itself)
I'm 99% sure that 'client' is a new object after each iteration but I
must be 100% sure.
2) The above example is taken from Java Network Programming. But is it
a better solution to start a new thread with client socket as a
parameter rather then passing its streams?
3) When I create ServerSocket the second parameter refers to backlog.
How can I check how many clients are waiting in a queue?
thanks in advance for any help
best regards
R
I've got theoretical problems with the idea of MultiThreaded Servers.
I want my server to run as separate threads for the clients.
I'm not enough experienced with network programming and Java itself.
Well I have a while loop like this (I removed all try/catch
statements):
boolean doIt = true;
while (doIt) {
Socket client = null;
client = server.accept();
is = client.getInputStream();
os = client.getOutputStream();
/* server stuff here... in separate thread */
Work w = new Work(is, os);
w.start();
}
And now my questions:
1) Client socket is created as a local variable - is it created every
loop iteration? I mean what happens if I started a thread 'Work' and a
new client will show up? Socket client will be created as a new
object? Are previous streams inside work thread safe? New thread will
have its own streams?
(well maybe it's not about network programming but about Java itself)
I'm 99% sure that 'client' is a new object after each iteration but I
must be 100% sure.
2) The above example is taken from Java Network Programming. But is it
a better solution to start a new thread with client socket as a
parameter rather then passing its streams?
3) When I create ServerSocket the second parameter refers to backlog.
How can I check how many clients are waiting in a queue?
thanks in advance for any help
best regards
R