• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Socket.accept stuck in loop

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question too difficult for “beginning”. Moving thread.
reply
    Bookmark Topic Watch Topic
  • New Topic