wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes Server Hangs up after single communication with Client Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Server Hangs up after single communication with Client" Watch "Server Hangs up after single communication with Client" New topic
Author

Server Hangs up after single communication with Client

Jay Crawf
Greenhorn

Joined: Sep 15, 2013
Posts: 10
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?



Evoked from the main program as:
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2564
    
    9

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 JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
Jay Crawf
Greenhorn

Joined: Sep 15, 2013
Posts: 10
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.
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2564
    
    9

The problem is in the server class. But then without the client connection, it's kinda hard to test.
Dennis Grimbergen
Ranch Hand

Joined: Nov 04, 2009
Posts: 142

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
Jay Crawf
Greenhorn

Joined: Sep 15, 2013
Posts: 10
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.
 
jQuery in Action, 2nd edition
 
subject: Server Hangs up after single communication with Client