MultiThreaded Servers - theoretical problems

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
 
S

Steve Horsley

R said:
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.

This is not a problem. When you accept the next connection,
the variable 'client' changes to refer to the latest socket,
bt that doesn't instantly vapourise the previous socket. The
previous socket can continue to exist and to be used by the
previous instatniation of a Work object (which similarly
will not be vapourised just because you create another one).

The thing to remember here is that both w and client are
references to objects, not objects themselves. As such, they
can be altered to refer to different objects without causing
instant death to the original. If this was the last
reachable reference to the object though, then the Garbage
Collector can come and eat it.

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?

I don't think it much matters. Personally, I would probably
pass the socket, but that's mainly to reduce the number of
parameters passed. You thread might want the socket though,
so it can do things like get the IP address of the far end
(Socket.getInetAddress()) or set the socket timeout
(Socket.setSoTimeout()). These cannot be done if only the
streams are passed.

3) When I create ServerSocket the second parameter refers to backlog.
How can I check how many clients are waiting in a queue?

I'm not aware of any way to find the current backlog. Just
keep calling accept(), and if there are no more callers,
accept will block.

Steve
 
E

Esmond Pitt

Steve said:
I'm not aware of any way to find the current backlog. Just keep calling
accept(), and if there are no more callers, accept will block.

There *is no way* to find either the maximum backlog (as specified when
listening) or the current backlog of connected clients which haven't
been accept()-ed yet. Why do you think you want to know?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top