• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

creating a basic server using java sockets libraries

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm going through this tutorial: http://docs.oracle.com/javase/tutorial/networking/sockets/clientServer.html and http://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html

I'm building a server that just accepts client messages, so basically a subset of the first link's example. I am developing in eclipse. I ran this project and the program hangs when the server socket tries to accept a connection; why?



and here is my test program, the client which sends messages to the server:
 
Rancher
Posts: 43024
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using Readers and Writers with sockets is problematic. Make sure you're not running into this problem: Don't println to a Socket (which can happen on all OS, not just OS X).
 
Saloon Keeper
Posts: 13074
281
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, you have a deadlock. Your program is waiting on itself.

In the constructor of your server, you're accepting client sockets. Bad idea. accept() is a blocking operation, and will wait until a client tries to connect. So your constructor waits for a client to connect, but this never happens because the constructor doesn't finish running.

You should use handlers in separate threads to deal with incoming connections.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your code never reaches line no. 17, as you are start listening in constructor itself.You constructor does not completes.
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic