Re: Concurrency newbie




"Ney André de Mello Zunino" <zunino@xxxxxxxxxxx> wrote in message
news:e0gojm$7m5$1@xxxxxxxxxxxxxxxx
Hello.

I am working on an assignment for the Parallel Programming course I've
been taking. Among other things, it involves a server which should accept
only 4 client connections. After the fourth connection has been
established, I would like the server to stop listening for connections.
Only when one of the connections is broken should the server resume its
listening activity.

Now the question is how can I properly set things up so that the server
won't be busy-waiting? I thought of an approach using wait/notify but,
since I am still learning concurrent programming, I am afraid I might be
abusing the concepts.

while (true)
{
while (connections < 4)
{
client = server.accept();
// create client handling object
connections++;
}
wait(); // once there are 4 connections already, stand by until
// a notify()cation is issued
}

Does the code above have any chance of succeeding? Will a notify() call
from a different thread actually wake up the server? What does that wait()
call actually means? From what I've read, it means the server would be
giving up its monitor and allowing any other thread who might be trying to
get hold of that monitor to proceed. Looking at it that way makes me fear
my solution will lead to a dead server, since the server code is not
shared, i.e. there are no other threads wanting to get hold of that
monitor.

My post shows I am definitely confused about these concepts. What I am
basically trying to do is have the server wait on a given condition
(number of connections went down from 4) in order to resume working.
What's the proper way to go about it?

Thank you,

--
Ney André de Mello Zunino
Graduando em Ciência da Computação
Universidade Federal de Santa Catarina

First of all, what wait() does is make the current thread wait until ther
notify or notifyAll method is called for that same thread. So as long as you
notify it again at some point it is a valid (although questionable way) to
make your program only allow 4 connections. Secondly your code snippet is
incorrect for what you want, try :

-> entry point for your program
while(!shutdownRequested) {

Socket socket = serverSocket.accept();

if(activeClientCount < 4)
initMyBrandNewSocket(socket);
else
rejectSocket(socket);
}
-> exit point

public void rejectSocket(Socket s) {

writeMessage(s, "Sorry, server is full baby!");

s.close();
}

Like you can see above, how i'd personally do this is keep the server accept
loop active, always accept connections (even if connectionCount > 4). Then
send a "server reached maximum capacity" kind of message to clients that
cause your server to pass the 4 client mark, then close the socket of the
"illegal" client and wait for the next accept. This is a better and neater
approach to your problem.

Remon van Vliet


.



Relevant Pages

  • Re: Network intermittently dropping the connection to shared files on server
    ... what we were using with our SBS2000 server with no problems. ... It's a small Server plus 4 Client W/S set up in one office. ... All users that have current connections to the shared files are ... We have a small network < 5 clients connected to a new Dell ...
    (microsoft.public.windows.server.sbs)
  • Re: Embedded Software Engineers, needed in Seattle
    ... One of our client companies in Seattle, ... Create editor for NFN content and server application to import content ... Test server for applications relating to each of the aforementioned ... Experience with being part of a creativity workforce and programming ...
    (comp.arch.embedded)
  • Re: What doesnt lend itself to OO?
    ... >> proxy and instructs the server to constuct the real object. ... rather than client code. ... If 'clock' is instantiated in the server, ... > for the server interface at the OOA level. ...
    (comp.object)
  • Embedded Software Engineers, needed in Seattle
    ... One of our client companies in Seattle, ... Create editor for NFN content and server application to import content ... Test server for applications relating to each of the aforementioned ... Experience with being part of a creativity workforce and programming ...
    (comp.arch.embedded)
  • This is going straight to the pool room
    ... or not the client has privilege to do what they're trying to do, ... The server environment is this: ... 3GL User action Routines that Tier3 will execute on your behalf during the ... Routine Name: USER_INIT ...
    (comp.os.vms)