Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with sockets

 
Barry Andrews
Ranch Hand
Posts: 523
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,
I am trying to learn about sockets, and I wrote 2 separate small test programs; SocketServer and SocketClient. What I am trying to do is get user input from console (a String) in the SocketClient class and send it to SocketServer. Then SocketServer is suppossed to send the same String back to SocketClient and SocketClient will display it. When I execute the programs, type some input and press Enter it does nothing. I need an extra pair of eyes to help me figure out what is wrong. It is probably something simple, but if you could please take a look.

SocketServer class:

SocketClient class:

I really appreciate the help!!!
Barry
 
Ryan Langley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Barry,
There appears to be a few problems with your code. Your main problem right now is that you are not flushing the PrintWriter streams on the client or the server. You can do this by either putting pw.flush() after you write the string to the stream, or you can set the autofush of the stream in the constructor, like this:
pw = new PrintWriter(theSock.getOutputStream(), true);
Your other problems deal mostly with over complicating the code, in my opinion.. I would get it working by just allowing it to send one message to the server and back, then exiting before attempting to get it to work with multiple messages.. You may also want to pick a higher port number.. 2000 may be a bit low and could conflict with other programs running on the same port.. Hope these suggestions help you out!
Cheers,
RL
 
Barry Andrews
Ranch Hand
Posts: 523
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan
Yes, you are right! I forgot to flush streams. That was my mistake...... Now about the port numbers. Is there any general rule of thumb about what port number to use so that I do not conflict with other processes that may be running? Thanks for you help!!!

Barry
 
karl koch
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
dont use well known port numbers (80 = http, 21 ftp ....) and dont use port numbers below 255. on some machines you wont get the rights to open a port with a number below 255.
otherwise it's up to you, just make sure it doesnt conflict with other stufff running on your machines.
karl
 
Ryan Langley
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
Actually, you definitely do not want to use any port between 0 and 1024. Those are reserved by the service that registers the port numbers (IANA). While the IANA can not control uses of these ports it does register or list uses of these ports as a convienence to the community. Usually anything above 7200 is pretty much safe. For instance, I see port 2000 registered to callbook applications. You can see the list of registered ports in the Assigned Numbers RFC.
Cheers,
RL
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic