Flush a socket in C++

nrofis picture nrofis · Oct 10, 2012 · Viewed 21k times · Source

I tried to flush a socket after calling to send function in c++.
I used winsock2.h library.

I need to send the data immediatly after the send message, but I can not find any function like flash function.

I am trying to send messages to a device and it expect receiving messages one by one.
I mean that if I sending two messages in the sender like "MessageOne" and "MessageTwo", the receiver received "MessageOneMessageTwo" that is not seperate, and the device not recognize the commands.

So how can I do that?

Answer

Remus Rusanu picture Remus Rusanu · Oct 10, 2012

There is nothing you can do on the send side to make the receive side receive messages 'one by one'. Is entirely the receive side responsibility to properly reconstruct the sent frames ('messages'). Receive code must know the message length somehow (entirely protocol specific) and receive as much data as appropriate to construct an entire frame (usually achieved by posting recv with a specified length and specified that is interested only on the entire buffer, eg. MSG_WAITALL flag). I find it very hard to believe your device does not know how to handle this, and if that's indeed the case there is literally nothing you can do. I find it somehow more likely that you do not understand the device/protocol requirements and you're asking the wrong question.