unix sockets: how to send really big data with one "send" call? unix sockets: how to send really big data with one "send" call? unix unix

unix sockets: how to send really big data with one "send" call?


The send system call is supposed to be fast, because the program may have other things useful things to do. Certainly you do not want to wait for the data to be sent out and the other computer to send a reply - that would lead to terrible throughput.

So, all send really does is queues some data for sending and returns control to the program. The kernel could copy the entire message into kernel memory, but this would consume a lot of kernel memory (not good).

Instead, the kernel only queues as much of the message as is reasonable. It is the program's responsibility to re-attempt sending of the remaining data.

In your case, use a loop to send the data that did not get sent the first time.

while(length > 0) {    bytesSent = send(sd, s, length);    if (bytesSent == 0)        break; //socket probably closed    else if (bytesSent < 0)        break; //handle errors appropriately    s += bytesSent;    length -= bytesSent;}

At the receiving end you will likely need to do the same thing.


Your initial send() call is wrong. You need to pass send() the address of the data, i.e.:

bytesSend = send(sd, &length, sizeof(int))

Also, this runs into some classical risks, with endianness, size of int on various platforms, et cetera.