This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I've been working on a simple client / server setup, and it does exactly what I need for the first client to connect. The server then hangs up forever, clients get a simple connection refused message afterwards until the server is restarted. I want the server to respond whenever a new client tries to connect, without having to restart the server each time. I've made the server listener Runnable, and placed it on its own thread (I think). What am I missing?
Where are you closing your server socket (line 36)? Inside the while loop or outside? In fact the close() stuff should be done in a finally block. The reading file stuff should have its own try/catch block
K. Tsang wrote:Where are you closing your server socket (line 36)? Inside the while loop or outside? In fact the close() stuff should be done in a finally block. The reading file stuff should have its own try/catch block
The close is outside the loop. I can throw a whole bunch more blocks in there if needed... will that allow the server to respond to more than one connection request?
I'm trying to debug my client, but it's a pain having to constantly restart the server app to do that.
Your server only accepts one client and then blocks until the client does something.
If you want more clients to connect, then you should place line 16 within a while loop.
Lines 22-31 from your code then moves to the Client class.
Then your Server class just accepts new client connections and spawns a new Thread for each of these clients, which can run concurrently. If you don't expect thousands of client connections, then this may be a suitable solution.
SCJP, SCWCD, SCJD
Joined: Sep 15, 2013
Cool, got it running in a loop now. I've only used it with one client connection at a time so far, but ultimately I'm only expecting a handful of connections at a time.