Maximum buffer length on socket AF_UNIX

I would like to know: when programming in C using a socket (AF_UNIX), is there any limit (in bytes) when sending or receiving to or from a socket?

+4
source share
2 answers

You can change the read and write buffers for each individual socket connection using setsockopt ( SO_SNDBUF and SO_RCVBUF ).

The default and maximum sizes vary by platform.

Also, if you provide a larger custom buffer for each individual read, for example. with recv .

And if you use several recv series, you can read an infinite number of bytes on the connection, it will take infinitely long time.

+6
source

socket behavior is implementation dependent. In general, when you send (), there is no guarantee how many bytes will be loaded into the socket. Because the kernel controls this, it can be any number, usually in the range of 1500 or less. So, you need to check the return code () and keep pushing the data into the socket until you are done. This example assumes that you have already set the socket to non-blocking with:

  fcntl(s, F_SETFL, O_NONBLOCK); int sendall(int s, char *buf, int *len) { int total = 0; /* how many bytes we've sent */ int bytesleft = *len; /* how many we have left to send */ int n=0; int retries=0; struct timespec tp={0,500}; while(total < *len) { n = send(s, buf+total, bytesleft, 0); if (n == -1) { /* handle errors here, plus check for EWOULDBLOCK and then nanosleep() */ } total += n; bytesleft -= n; } 

To answer your question - there are no restrictions; you simply cannot send all your data with a single call to send ().

0
source

Source: https://habr.com/ru/post/1312956/


All Articles