简体   繁体   English

AF_UNIX套接字中的最大缓冲区长度

[英]Maximum length of buffer in AF_UNIX socket

我想知道:当使用套接字(AF_UNIX)在C编程时,在发送或接收套接字时是否有任何限制(以字节为单位)?

You can change the read and write buffers for each individual socket connection using setsockopt ( SO_SNDBUF and SO_RCVBUF ). 您可以使用setsockoptSO_SNDBUFSO_RCVBUF )更改每个插槽连接的读写缓冲区。

The default and maximum sizes are platform dependent. 默认大小和最大大小取决于平台。

Furthermore, if you provide a larger user-side buffer for each individual read eg with recv . 此外,如果您为每个单独的读取提供更大的用户端缓冲区,例如使用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. 如果你按顺序使用几个recv ,你可以通过连接读取无限量的字节,它只需要无限长。

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. 由于内核控制它,它可以是任何数字,通常在1500或更小的范围内。 So what you have to do is check the send() return code and keep pushing data onto the socket until you are done. 因此,您需要做的是检查send()返回代码并继续将数据推送到套接字上,直到完成为止。 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. 要回答您的问题 - 没有限制,您只能通过一次send()调用发送所有数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM