recv

RECV(2)                    Linux Programmer's Manual                   RECV(2)



åå
       recv, recvfrom, recvmsg - ã½ã±ããããã¡ãã»ã¼ã¸ãåãåã

æ¸å¼
       #include <sys/types.h>
       #include <sys/socket.h>


       int recv(int s, void *buf, int len, unsigned int flags);

       int recvfrom(int s, void *buf, int len, unsigned int flags struct
       sockaddr *from, int *fromlen);

       int recvmsg(int s, struct msghdr *msg, unsigned int flags);

説æ
       recvfrom 㨠recvmsg ã¯ã½ã±ããããã¡ãã»ã¼ã¸ãåãåãã®ã«ä½¿ç¨ããã½ã±ãããæ¥ç¶æå
       (connection-oriened)ã§ãã£ã¦ããªãã¦ãã½ã±ããã®ãã¼ã¿ãåä¿¡ãã ãã¨ãã§ããã

       from ã NULL ã§ãªããã½ã±ãããæ¥ç¶æåã§ãªãå ´åã from
       ã«ã¡ãã»ã¼ã¸ã®éä¿¡åã®ã¢ãã¬ã¹ãå¥ããããã fromlen
       ã¯å¥åºå両ç¨ã®ãã©ã¡ã¼ã¿ã¼ã§ãæå㯠from
       ã«å²ãå½ã¦ããããã¡ã¼ã®å¤§ãããå¥ãã¦ãããè¿ã£ã¦ããæã«ã¯ from
       ã«å®éã«å¥ãã¢ãã¬ã¹ã®å¤§ããã«ä¿®æ£ããã¦è¿ãããã

       recv ã³ã¼ã«ã¯é常 æ¥ç¶ããã(connected) ã½ã±ãã( connect(2)
       ãåç§)ã«ã¤ãã¦ã®ã¿ä½¿ç¨ããã from ãã©ã¡ã¼ã¿ã¼ã NULL ã® recvfrom ã¨ç価ã§ããã
       åé·ã§ãããããå°æ¥ã®ãªãªã¼ã¹ã§ã¯ãµãã¼ããããªããããããªãã

       æåããå ´åã«ã¯ä¸ã¤ã®ã«ã¼ãã³ã¯ã¡ãã»ã¼ã¸ã®é·ããè¿ãã
       ã¡ãã»ã¼ã¸é·éãã¦ä¸ãããããããã¡ã¼ã«ç´ã¾ããªãã£ãå ´åã«ã¯
       ã¡ãã»ã¼ã¸ãåä¿¡ããã½ã±ããã®ç¨®é¡ã«ãã£ã¦ã¯ä½åã®ãã¤ãã¯æ¨ã¦ããã ãããããªãã(
       socket(2) ãåç§)ã

       ã½ã±ããã«åãåãã¡ãã»ã¼ã¸ãåå¨ããªãã£ãå ´åãåä¿¡ç¨ã®ã³ã¼ã«ã¯
       ã¡ãã»ã¼ã¸ãå°çããã¾ã§å¾ã¤ããã ãã½ã±ãããéåæ¢(nonblocking) ã«è¨å®ããã¦ããå ´å(
       fcntl(2) ãåç§)㯠-1 ãè¿ãå¤é¨å¤æ° errno ã« EWOULDBLOCK ãè¨å®ãããã

       åä¿¡ã³ã¼ã«ã¯é常ãè¦æ±ããæ大é以ä¸ã§åä¿¡ãããã¼ã¿ãè¿ãã
       è¦æ±éãã£ã±ãã¾ã§åä¿¡ããã¾ã§å¾ã¤ãã¨ã¯ãªãããã®åä½ã¯ getsockopt(2)
       ã«è¨è¿°ãããã½ã±ãã層ã®ãªãã·ã§ã³ SO_RCVLOWAT 㨠SO_RCVTIMEO ã®å½±é¿ãåããã


       select(2) ã³ã¼ã«ã¯ãã¤ãã¼ã¿ãå±ããã決å®ããããã«ä½¿ç¨ã§ããã

       recv ã³ã¼ã«ã® flags å¼ãæ°ã¯ä»¥ä¸ã®å¤ã®ä¸ã¤ä»¥ä¸ã® or ã§ãã:


       MSG_OOB 帯åå¤(out-of-band)ãã¼ã¿ãå¦çãã

       MSG_PEEK
               å¥ã£ã¦æ¥ãã¡ãã»ã¼ã¸ã調ã¹ã

       MSG_WAITALL
               è¦æ±ãå®å¨ã«æºããã¨ã©ã¼ãèµ·ããã¾ã§å¾ã¤

               MSG_OOB ãã©ã°ã¯é常ã®ãã¼ã¿ã»ã¹ããªã¼ã ã§åä¿¡ã§ããªã帯åå¤(out-of-
               band)ãã¼ã¿ã åä¿¡ãããã¨ãè¦æ±ãããé常ã®ãã¼ã¿ã»ã‐
               ã¥ã¼ã®åé ã«ééãã¼ã¿ãç½®ã ãããã³ã«ãåå¨ãããããã§ãã®ãããªãã‐
               ãã³ã«ã§ã¯ãã®ãã©ã°ã¯ 使ç¨ã§ããªãã MSG_PEEK ãã©ã°ã¯åä¿¡ã‐
               ã¥ã¼ã®æåã®ãã¼ã¿ããã¥ã¼ãããã¼ã¿ãåé¤ããã«è¿ãã
               ããã«ãã£ã¦ç¶ãåä¿¡ã³ã¼ã«ã§åããã¼ã¿ãè¿ããã¨ãã§ããã MSG_WAITALL
               ãã©ã°ã¯è¦æ±ããéãå®å¨ã«æºãã¾ã§æä½ãåæ¢(block)ãããã¨ãè¦æ±ããã
               ããããªãããã·ã°ãã«ãåä¿¡ããããã¨ã©ã¼ãåæ(disconnect)ãèµ·ã£ãã
               次ã«åä¿¡ãããã¼ã¿ãç°ãåã ã£ãå ´åã«ã¯ãè¦æ±ããéããå°ãªããã¼ã¿ã
               è¿ããã¨ãããã

               recvmsg ã³ã¼ã«ã¯ç´æ¥ä¸ãããã©ã¡ã¼ã¿ã¼ã®æ°ãæ¸ããããã« msghdr
               æ§é ä½ã使ç¨ããããã®æ§é ä½ã¯ sys/socket.h
               ã§ä»¥ä¸ã®ããã«å®ç¾©ããã¦ãã :

               struct msghdr {
                    caddr_t   msg_name; /* optional address */
                    u_int     msg_namelen;   /* size of address */
                    struct    iovec *msg_iov;     /* scatter/gather array */
                    u_int     msg_iovlen;    /* # elements in msg_iov */
                    caddr_t   msg_control;   /* ancillary data, see below */
                    u_int     msg_controllen; /* ancillary data buffer len */
                    int  msg_flags;     /* flags on received message */
               };

       ãã㧠msg_name 㨠msg_namelen
       ã¯ã½ã±ãããæ¥ç¶ããã¦ããªãå ´åã«ãç¸æã®ã¢ãã¬ã¹ãæ ¼ç´ãããã ååãå¿è¦ãªãå ´åã«ã¯
       msg_name ã« NULL ãå¥ãããã¨ãã§ããã msg_iov 㨠msg_iovlen 㯠readv(2)
       ã«è¨è¿°ããã¦ãããããªéä¿¡/éä¿¡ç¨ã®ãã¯ã¿ã¼ãæå®ããã msg_control, ã¯
       msg_controllen ã®é·ããæã¡ãä»ã®ã¡ãã»ã¼ã¸é¢é£ã®ãããã³ã«ãå¶å¾¡ãããããã¡ã¼ã
       ä»ã®è£å©çãªãã¼ã¿ã®ããã®ãããã¡ã¼ã¸ã®ãã¤ã³ã¿ã¼ã§ããã ã¡ãã»ã¼ã¸ã®å½¢å¼ã¯:

               struct cmsghdr {
                    u_int     cmsg_len; /* data byte count, including hdr */
                    int  cmsg_level;    /* originating protocol */
                    int  cmsg_type;     /* protocol-specific type */
               /* followed by
                    u_char    cmsg_data[]; */
               };

       ä¾ãã°ããã®æ§é ä½ãã XNS/SPP ã ISO ã«ããã¦ãã¼ã¿ã»ã¹ããªã¼ã ã®
       å¤åãç¥ããã¨ãã§ããã accept ã³ã¼ã«ã®ç´å¾ã« recvmsg
       ããã¼ã¿ã»ãããã¡ã¼ããªãã§å¼ã¶ãã¨ã«ããã ãã®æ§é ä½ã«ãã¦ã¼ã¶ã¼æ¥ç¶è¦æ±(user-
       connection-request)ãã åå¾ãããã¨ãã§ããã

       ãªã¼ãã³ããããã¡ã¤ã«ã»ãã£ã¹ã¯ãªãã¿ã¼ã¯è£å©çãªãã¼ã¿ã¨ã㦠AF_UNIX
       ãã¡ã¤ã³ã»ã½ã±ããã«æ¸¡ããã cmsg_level ã« SOL_SOCKET ãè¨å®ããã cmsg_type ã«
       SCM_RIGHTS ãè¨å®ãããã

       msg_flags ãã£ã¼ã«ãã¯åä¿¡ããã¡ãã»ã¼ã¸ãåç§ããããã«è¿å´æã«è¨å®ãããã MSG_EOR
       ã¯ã¬ã³ã¼ãã®çµã(end-of-record)ã示ãããã®ãã¼ã¿ã¯å®å¨ãªã¬ã³ã¼ãã
       è¿ãããã¨ã示ã(ä¸è¬çã«ã¯ SOCK_SEQPACKET åã®ã½ã±ããã§ä½¿ç¨ããã)ã MSG_TRUNC
       ã¯ããã¼ã¿ã°ã©ã ãä¸ãããããããã¡ã¼ãã大ããã£ãããã«
       ãã¼ã¿ã°ã©ã ã®ã¯ã¿åºããé¨åãæ¨ã¦ããããã¨ã示ãã MSG_CTRUNC
       ã¯è£å©çãªãã¼ã¿ã®ããã®ãããã¡ã¼ãä¸è¶³ããããã«å¶å¾¡ãã¼ã¿ã®ä¸é¨ã
       æ¨ã¦ãã¦ããã示ãã ééãã¼ã¿ã帯åå¤ãã¼ã¿ãåä¿¡ãããã¨ã示ãããã« MSG_OOB ãè¿ãããã


è¿ãå¤
       ãããã®ã³ã¼ã«ã¯åä¿¡ãããã¤ãæ°ãè¿ããã¨ã©ã¼ã®å ´å㯠-1 ãè¿ãããã

ã¨ã©ã¼
       EBADF   å¼ãæ° s ãç¡å¹ãªãã£ã¹ã¯ãªãã¿ã¼ã§ããã

       ENOTCONN
               ã½ã±ããã«æ¥ç¶æåãããã³ã«ãå²ãå½ã¦ããã¦ããã ã¾ã æ¥ç¶ããã¦ããªãã(
               connect(2) 㨠accept(2) ãåç§ãããã¨)ã

       ENOTSOCK
               å¼ãæ° s ãã½ã±ãããåç§ãã¦ããªãã

       EWOULDBLOCK
               ã½ã±ãããéåæ¢(non-blocking)ã«è¨å®ããã¦ããåä¿¡æä½ãåæ¢ãããã
               åä¿¡ã«æéåã(timeout)ãè¨å®ããããããã¼ã¿ãåä¿¡ããåã« æéåãã«ãªã£ãã

       EINTR   ãã¼ã¿ãåä¿¡ããåã«ã·ã°ãã«(signal)ãééããã¦å²ãè¾¼ã¾ããã

       EFAULT  åä¿¡ãããã¡ã¼ã¸ã®ãã¤ã³ã¿ã¼ãããã»ã¹ã®ã¢ãã¬ã¹ç©ºéå¤ãæãã¦ããã

æºæ
       4.4BSD (ãããã®é¢æ°ã¯ 4.2BSD ã§ç¾ããã)ã

é¢é£é ç®
       fcntl(2), read(2), select(2), getsockopt(2), socket(2)



BSD Man Page                     24 July 1993                          RECV(2)