Flush socket in C ++

I tried to clear the socket after calling the send function in C ++.
I used the winsock2.h library.

I need to send data immediately after sending a message, but I can not find any function, for example, the flash function.

I am trying to send messages to a device and expect to receive messages one by one.
I mean, if I send two messages in the sender, such as "MessageOne" and "MessageTwo", the recipient received a "MessageOneMessageTwo", which is not separate, and the device does not recognize the commands.

So how can I do this?

0
c ++ sockets network-programming winsock2
source share
5 answers

There is nothing you can do on the sending side to receive one-by-one receive messages. He is fully responsible for the responsibility for the correct reconstruction of the sent frames ("messages"). The receive code should know the length of the message somehow (completely protocol specific) and receive as much data as possible to build the whole frame (usually this is achieved by placing recv with the specified length and the specified one, which only interests the entire buffer, for example. MSG_WAITALL ). It’s very difficult for me to believe that your device does not know how to handle this, and if this is true, then literally nothing you can do. I find it somehow more likely that you do not understand the device / protocol requirements and you are asking the wrong question.

+6
source share

There is no flash functionality for sockets. If you need to send two messages at once, just send them. If this is a TCP socket, then they will arrive in the correct order (the order you sent them to).

This pattern is actually not uncommon; First send a message header, followed by a separate message data submission.

+1
source share

As far as I know, this is not really a flash. the send function returns the number of bytes, so you can iterate over the loop until all bytes are sent.

Edit: To add to what I read, you want from other users. The only way I know to increase the "internal buffer" (redness is what winsock itself does) is setockopt using the so_sendbuf option.

Article relating to it:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms740476%28v=vs.85%29.aspx

0
source share

The exact answer to the question about your batch scheme with an example from the Winsock FAQ

0
source share

set socket options to NDELAY on the send side

-one
source share

All Articles