Advertisement

Question about TCP sockets

Started by November 25, 2002 07:18 PM
3 comments, last by antareus 22 years, 2 months ago
I''ve used them quite a bit, but on my final ''industrial strength'' project for my networking class, I''m not 100% clear on how much I should cover for TCP''s stream capability. Lets say I send the string "USER matt PASS matt" (strikingly like FTP I know). Now when I send it, I loop over send() repeatedly until the whole string has been sent. Also, it might not actually get sent THAT instant since TCP may hold data and send it a bit later in order to maximize thoroughput. I''m okay with that. When I go to receive it on the other end do I have to worry that I will only get part of it, such as just receiving "USE" from the string sent before? The problem is I am transmitting short text strings and if I only get 3 chars instead of four its pretty darn fatal. Also, that is all that is being transmitted, they are small packets.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
Have each command end in a newline, and on the receiving end, wait until you get a full line before parsing the command. I can''t remember if data sticks together when you send() and recv(), but it appears possible that such a string may be split if it somehow ends on the boundary between two packets.

quote:
Now when I send it, I loop over send() repeatedly until the whole string has been sent.
You should only have to send() the data once. No need to loop on send().

Advertisement
For non-blocking sockets, you might have to loop on send() if the information you''re trying to send is too large to fit in the transport buffers. It shouldn''t really be a problem unless you''re sending something as large as a file.

Since TCP is a streaming protocol, you may have to call recv() several times to get the entire string. I would suggest using some sort of terminating character on the end of your string. Then you can keep calling recv() until you''ve got the terminator. Just be careful not to discard anything that might come after it.
whazzup ant
daerid@gmail.com
Thanks for the responses. It looks like I''ll make a small class to handle the command processing like I thought I would. That and I wasted about two hours trying to figure out how to use stringstreams.

Hey dae, saw your trace functionality was featured yesterday at gamedev.net.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis

This topic is closed to new replies.

Advertisement