• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Socket.accept stuck in loop

 
Rob Brew
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all.

I'm at a loss as i've studied the official Oracle documentation on how to handle sockets and this problem still remains:



The Core class (of which the above method is call from) never exits therefore the next command in the main function cannot execute.

"There" never gets printed.

Should i set up initNetworking as a thread?

Thread.sleep() needed.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, yeah. The line "serverSocket.accept()" is indeed waiting until a client connects. And then once that happens, it's going to accept the client and wait for the next one.

So if you don't want that code to block something else, then yes, you should run it in its own thread.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Brew wrote:
Should i set up initNetworking as a thread?


AS a thread? No. But IN its own thread, yes.

If you do


Obviously, you'll never get to the "other stuff" part. It doesn't matter if the "stuff" part is socket calls or something else.

Also, you're closing the ServerSocket at every pass through the while loop. This means that only your first accept() will work, and all subsequent ones will fail.

Always close in a finally block.


 
Campbell Ritchie
Sheriff
Pie
Posts: 50278
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question too difficult for “beginning”. Moving thread.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic