I开发者_StackOverflow 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?
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 are platform dependent.
Furthermore, if you provide a larger user-side buffer for each individual read e.g. with recv
.
And if you use several recv
s in sequence, you can read an infinite amount of bytes over a connection, it'll just take infinitely long.
sockets behavior is implementation is dependent. In general when you send() there is no guarantee how many bytes will get pushed onto the socket. Since the kernel controls this it can be any number, generally in the range of 1500 or less. So what you have to do is check the send() return code and keep pushing data onto the socket until you are done. This example assumes that you 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 - no there is no limit, you just cannot send all of your data with one send() call.
精彩评论