I was given an assignment to code a simple console based chat program in java with multiple users using threads and sockets. The way I have chosen to implement this is to have the client side program first query the server via a hardcoded port # for an open port for which to connect to the server. Then with the fresh port # in hand the client closes that socket, freeing it up for another client to query the server, and opens up a new socket with the dedicated port.

My problem is the client never makes the second connection. I have verified that the server is "listening" to the right port number and that the client is attempting the connect to the right port number. But I get a connection error each time. Is there a rule about sockets I dont know? Can you not initiate more than 1 in any thread? If so, should I be spinning a thread off the client to initiate the first contact? I can supply the code if necessary.

Also (this is the multithreaded part) the server is designed to dish out the port numbers then assign each number to a new socket and pass it to a new thread to handle. But when it passes the socket off, the server no longer is connected to it. So how do I get the messages sent from each client to each other client concurrently?

Once again I can provide code if necessary.