Forums Register Login

Help with sockets

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
Yes, of course, and I accept that blame. In fact, i covet that blame. As does this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1171 times.
Similar Threads
move file A to file B
small IO problem
FTP Socket Programming
Server and Client communication probs
How to send a message back from Server to Client?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 20:21:43.