• 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

Help with sockets

 
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic