LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-23-2005, 04:11 PM   #1
pjz
Member
 
Registered: Sep 2003
Posts: 71

Rep: Reputation: 15
Question How do I flush a C++ socket?


Hi-

I'm working with sockets in C++, but not using the iostream library to access
the sockets. Both sender and receiver reside on Linux workstations, and I'm
using g++ to compile things. I'm working off RedHat 9.0.

When I send on my socket, it doesn't necessarily flush a message before I
send the next one. This results in parts (or all) of more than one message
being concatenated together. I can code around this by parsing and
reassembling them myself on the receiving end, but it'd be really great if
someone out there would tell me how to flush a socket once the
write() operation is complete. Then I could count on each message being
discrete, and the parsing would be simplified.

Anyone have any ideas out there?
pjz
 
Old 02-24-2005, 08:21 PM   #2
ringerxyz
LQ Newbie
 
Registered: Nov 2004
Location: China
Distribution: red-flag
Posts: 16

Rep: Reputation: 0
hi piz,
I guess you are using tcp socket,aren't you?
you should take the tcp connection as a stream,so at one end you send some bytes,say 2 bytes,then at the another end of connection ,you just simply recv 2 bytes,there is no need to think about the single packet,
btw,the is a socket option you can use :TCP_NODELAY ,but i think you should avoid to use it until you are going to program some 'telnet' 'rlogin' like programs.
goog luck
 
Old 02-25-2005, 08:58 AM   #3
pjz
Member
 
Registered: Sep 2003
Posts: 71

Original Poster
Rep: Reputation: 15
Thanks. I have variable length records to exchange over the socket.
The solution I'm working with now involves using recv(sockt,buf,sizeof,MSG_PEEK) to see what's in the socket first. I then
look for the message terminator, add up the length of the message to
that point, then use recv(sockt,buf,sizeof,0) to pop just the right number of characters out of the buffer. That's a pretty effective solution. I wish I new
how to flush the buffer without needing something like fd << flush, but
it just doesn't seem to be out there.

Thanks again for the suggestion. This is a great place to post, the community here is very responsive

pjz
 
Old 02-25-2005, 09:01 AM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Unfortunately, with TCP based communication, you generally have to use your own tricks to preserve packet boundaries. There are a number of ways you can do this. One way is to have a field in your packet that specifies the length of the packet so that you know exactly how many bytes to read in for any given packet.
 
Old 02-25-2005, 10:26 AM   #5
pjz
Member
 
Registered: Sep 2003
Posts: 71

Original Poster
Rep: Reputation: 15
Yeah, I thought of that. It would work, but would still require a MSG_PEEK,
then a parse, to retrieve the message length, so that the correct number
of characters could be removed from the socket. It would be a little cleaner
to implement on the receiving end, because the field with the count would be in a known place and have a known length. One wouldn't have to iterate the
message to accumulate the count. On the sending end, it would be just a
matter of using strlen() or something like it to get the length for passage in
the message. The code I'm developing is for an event driven simulator
application, and isn't intended to be "production quality", but "model quality".
I think if I was developing for distribution, including the message length in
the sent message would be the way to go.
 
Old 02-25-2005, 10:55 AM   #6
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
You wouldn't necessarily need to peek. If your count value is stored in the first bytes, just read those bytes from the socket to get the length, then read the number of bytes necessary for the rest of the packet.

You might want to restrict this to a reasonable maximum, and also use some sort of timeout mechanism to make sure you don't get stuck in a loop where you are expecting 50000 bytes, but only ever get sent 4, though.

If you want to get a feel for how other TCP protocols work with regards to packet boundaries, you might want to take a look at some RFCs for established protocols.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ARP flush abneru SUSE / openSUSE 3 01-27-2012 12:57 PM
cannot read data at server socket, though client socket sends it jacques83 Linux - Networking 0 11-15-2005 01:58 PM
Unable to connect to UNIX socket /tmp/.esd/socket error while using grip dr_zayus69 Linux - Software 4 08-23-2005 07:28 PM
flush() before use... Elric of Grans Programming 1 07-12-2005 07:54 PM
C flush() question ...... purpleburple Programming 3 11-15-2002 09:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:31 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration