accept: Invalid argument

accept函数出错

看看 man 2 accept:

NAME
       accept, accept4 - accept a connection on a socket

SYNOPSIS
       #include <sys/types.h>          /* See NOTES */
       #include <sys/socket.h>

       int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

       #define _GNU_SOURCE             /* See feature_test_macros(7) */
       #include <sys/socket.h>

       int accept4(int sockfd, struct sockaddr *addr,
                   socklen_t *addrlen, int flags);

DESCRIPTION
       The  accept()  system  call is used with connection-based socket types (SOCK_STREAM, SOCK_SEQPACKET).  It extracts the first connection request on the queue of pending
       connections for the listening socket, sockfd, creates a new connected socket, and returns a new file descriptor referring to that socket.  The newly created socket  is
       not in the listening state.  The original socket sockfd is unaffected by this call.

       The argument sockfd is a socket that has been created with socket(2), bound to a local address with bind(2), and is listening for connections after a listen(2).

       The  argument  addr  is a pointer to a sockaddr structure.  This structure is filled in with the address of the peer socket, as known to the communications layer.  The
       exact format of the address returned addr is determined by the socket's address family (see socket(2) and the respective protocol man pages).  When addr is NULL, noth‐
       ing is filled in; in this case, addrlen is not used, and should also be NULL.

       The  addrlen  argument  is a value-result argument: the caller must initialize it to contain the size (in bytes) of the structure pointed to by addr; on return it will
       contain the actual size of the peer address.

       The returned address is truncated if the buffer provided is too small; in this case, addrlen will return a value greater than was supplied to the call.

       If no pending connections are present on the queue, and the socket is not marked as nonblocking, accept() blocks the caller until a  connection  is  present.   If  the
       socket is marked nonblocking and no pending connections are present on the queue, accept() fails with the error EAGAIN or EWOULDBLOCK.



其中有这么一句:

       The  addrlen  argument  is a value-result argument: the caller must initialize it to contain the size (in bytes) of the structure pointed to by addr; on return it will
       contain the actual size of the peer address.
参数addrlen必须先初始化!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值