wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes Help with sockets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Help with sockets" Watch "Help with sockets" New topic
Author

Help with sockets

Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

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

Joined: May 10, 2001
Posts: 46
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


Cheers,<P>RL
Barry Andrews
Ranch Hand

Joined: Sep 05, 2000
Posts: 523

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

Joined: May 25, 2001
Posts: 388
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

Joined: May 10, 2001
Posts: 46
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
 
wood burning stoves
 
subject: Help with sockets
 
Similar Threads
FTP Socket Programming
move file A to file B
small IO problem
Server and Client communication probs
How to send a message back from Server to Client?