• Post Reply Bookmark Topic Watch Topic
  • New Topic

creating a basic server using java sockets libraries  RSS feed

 
peter pham
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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:
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • 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).
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
narendra saran
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!