Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Server Hangs up after single communication with Client

 
Jay Crawf
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3398
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Jay Crawf
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3398
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is in the server class. But then without the client connection, it's kinda hard to test.
 
Dennis Grimbergen
Ranch Hand
Posts: 159
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jay Crawf
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic