andrea
andrea

Reputation: 381

Waiting send if the packet is not sent to the other endsystem? C send()

I'm using send on Linux std socket to write a packet over network. Send call, buffers data and "always" return values greater than 0. Send Pass the problem to the operating system and the lower level. How can i stop the send call and wait for the delivery of the packet to the other endsystem? Waiting for the TCP ACK or something like that?

if (send(broker->socket, packet, sizeof(packet), 0) < sizeof(packet)){
 return -3;
}   

The test will like: start to send packet,remove the ethernet,reattach.
Thanks, sorry for my bad english.

Upvotes: 0

Views: 99

Answers (1)

cnicutar
cnicutar

Reputation: 182684

There's no real robust way to find out if the remote peer acked your data. In fact, even if the remote TCP acks your data that doesn't mean the remote process read it. The simplest method is to implement a sort of application-layer ACK where the peer sends back a byte signaling "ok, got it".

  • You call send and it returns almost immediately
  • At this point the kernel starts working, trying to push the data
  • You call recv which blocks
  • The remote side receives the data and sends some data back, acknowledging it
  • Your recv unblocks

At this point you can be certain the remote side received your data.

Upvotes: 1

Related Questions