socket

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



åå
       socket - éä¿¡ã®ããã®ç«¯ç¹ (endpoint) ãä½æãã

æ¸å¼
       #include <sys/types.h> /* ã注æãåç§ */
       #include <sys/socket.h>

       int socket(int domain, int type, int protocol);

説æ
       socket() ã¯éä¿¡ã®ããã®ç«¯ç¹ (endpoint) ãä½æãã ãã£ã¹ã¯ãªãã¿ã¼ãè¿ãã

       domain å¼æ°ã¯éä¿¡ãè¡ãªããã¡ã¤ã³ãæå®ãã; ããã¯ã©ã® ãããã³ã«ãã¡ããªã¼
       (protocol family) ãéä¿¡ã«ä½¿ç¨ããããæå®ããã ãããã®ãã¡ããªã¼ã¯ <sys/socket.h>
       ã«å®ç¾©ããã¦ããã ç¾å¨ãç解ã§ãããã©ã¼ãããã¯ä»¥ä¸ã®éãã

       åå                  ç®ç                                    ããã¥ã¢ã«
       AF_UNIX, AF_LOCAL   ãã¼ã«ã«éä¿¡                            unix(7)
       AF_INET             IPv4 ã¤ã³ã¿ã¼ããããããã³ã«                ip(7)
       AF_INET6            IPv6 ã¤ã³ã¿ã¼ããããããã³ã«                ipv6(7)
       AF_IPX              IPX - Novell ãããã³ã«
       AF_NETLINK          ã«ã¼ãã«ã¦ã¼ã¶ã¼ã¤ã³ã¿ã¼ãã§ã¼ã¹ããã¤ã¹   netlink(7)
       AF_X25              ITU-T X.25 / ISO-8208 ãããã³ã«          x25(7)
       AF_AX25             ã¢ããã¥ã¢ç¡ç· AX.25 ãããã³ã«
       AF_ATMPVC           çã® ATM PVC ã«ã¢ã¯ã»ã¹ãã
       AF_APPLETALK        AppleTalk                              ddp(7)
       AF_PACKET           ä½ã¬ãã«ã®ãã±ããã¤ã³ã¿ã¼ãã§ã¼ã¹          packet(7)
       AF_ALG              ã«ã¼ãã«ã®æå· API ã¸ã®ã¤ã³ã¿ã¼ãã§ã¼ã¹

       ã½ã±ãã㯠type ã§æå®ãããåãæã¡ãããã¯éä¿¡æ¹å¼ (semantics) ãæå®ããã
       å®ç¾©ããã¦ããåã¯ç¾å¨ä»¥ä¸ã®éãã

       SOCK_STREAM     é åºæ§ã¨ä¿¡é ¼æ§ããããåæ¹åã®ãæ¥ç¶ããã ãã¤ãã¹ããªã¼ã  (byte
                       stream) ãæä¾ããã 帯åå¤ (out-of-band)
                       ãã¼ã¿è»¢éã¡ã«ããºã ããµãã¼ããããã

       SOCK_DGRAM      ãã¼ã¿ã°ã©ã
                       (ã³ãã¯ã·ã§ã³ã¬ã¹ãä¿¡é ¼æ§ç¡ããåºå®æ大é·ã¡ãã»ã¼ã¸)
                       ããµãã¼ãããã

       SOCK_SEQPACKET  åºå®æ大é·ã®ãã¼ã¿ã°ã©ã 転éãã¹ã«åºã¥ããé åºæ§ãä¿¡é ¼æ§ã®ãã
                       åæ¹åã®æ¥ç¶ã«åºã¥ããéä¿¡ãæä¾ãããåãåãå´ã§ã¯ããããã®å¥å
                       ã·ã¹ãã ã³ã¼ã«ã§ãã±ããå¨ä½ãèªã¿åããã¨ãè¦æ±ãããã

       SOCK_RAW        çã®ãããã¯ã¼ã¯ãããã³ã«ã¸ã®ã¢ã¯ã»ã¹ãæä¾ããã

       SOCK_RDM        ä¿¡é ¼æ§ã¯ããããé åºã¯ä¿è¨¼ããªããã¼ã¿ã°ã©ã 層ãæä¾ããã

       SOCK_PACKET     å»æ¢ããã¦ããæ°ããããã°ã©ã ã§ä½¿ç¨ãã¦ã¯ãããªãã packet(7)
                       ãåç§ãããã¨

       ãã種ã®ã½ã±ããåãå¨ã¦ã®ãããã³ã«ãã¡ããªã¼ã§å®è£ããã¦ããããã§ã¯ãªãã

       Linux 2.6.27 以éã§ã¯ã type å¼æ°ã¯äºã¤ç®ã®ç®çã«ã使ç¨ãããã
       ã½ã±ããã®åãæå®ããã®ã«å ãã¦ã 以ä¸ã®å¤ã®ããã¤ãããããåä½ã®è«çå (OR)
       ã§æå®ãããã¨ã§ã socket()  ã®æ¯èããå¤æ´ãããã¨ãã§ããã

       SOCK_NONBLOCK   æ°ããçæããããªã¼ãã³ãã¡ã¤ã«è¨è¿° (open file description) ã®
                       O_NONBLOCK ãã¡ã¤ã«ã¹ãã¼ã¿ã¹ãã©ã°ãã»ããããã
                       ãã®ãã©ã°ã使ããã¨ã§ã O_NONBLOCK ãã»ããããããã« fcntl(2)
                       ã追å ã§å¼ã³åºãå¿è¦ããªããªãã

       SOCK_CLOEXEC    æ°ãããã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã¼ã«å¯¾ã㦠close-on-exec
                       (FD_CLOEXEC)  ãã©ã°ãã»ããããã
                       ãã®ãã©ã°ãå½¹ã«ç«ã¤çç±ã«ã¤ãã¦ã¯ã open(2)  ã® O_CLOEXEC
                       ãã©ã°ã®èª¬æãåç§ã®ãã¨ã

       protocol ã¯ã½ã±ããã«ãã£ã¦ä½¿ç¨ãããåºæã®ãããã³ã«ãæå®ãããé常ããããã®
       ã½ã±ããã¯ãä¸ãããããããã³ã«ãã¡ããªã¼ã®ç¨®é¡ãã¨ã«ä¸ã¤ã®ãããã³ã«ã®ã¿ã ãµãã¼ãããã
       ãã®å ´å㯠protocol ã« 0 ãæå®ã§ããã ããããå¤ãã®ãããã³ã«ãåå¨ãã¦ããã¾ããªãã
       ãã®å ´åã«ã¯ãã®æ¹æ³ã«ããåºæã®ãããã³ã«ãæå®ããå¿è¦ãããã 使ç¨ããããã‐
       ãã³ã«çªå·ã¯éä¿¡ã®è¡ãªããã“éä¿¡ãã¡ã¤ã³”ã« åºæã§ãã; protocols(5)
       ãåç§ãããã¨ã ãããã³ã«åãã©ããã£ã¦ãããã³ã«çªå·ã«å¯¾å¿ããããã«ã¤ãã¦ã¯
       getprotoent(3)  ãåç§ãããã¨ã

       SOCK_STREAM åã®ã½ã±ããã¯å¨äºéãã¤ãã¹ããªã¼ã ã§ããã ãããã¯ã¬ã³ã¼ãå¢çãä¿åããªãã
       ã¹ããªã¼ã ã¯ãã½ã±ããããã¼ã¿ãéã£ããåãããããåã« æ¥ç¶ããã
       ç¶æã«ãªã£ã¦ãªããã°ãªããªããä»ã®ã½ã±ããã¸ã®æ¥ç¶ã¯ connect(2)
       ã³ã¼ã«ã«ãã£ã¦è¡ãªããããä¸åº¦æ¥ç¶ããããã¼ã¿ã¯ read(2)  㨠write(2)  ã³ã¼ã«ã
       send(2)  㨠recv(2)  ã³ã¼ã«ã®å¤ç¨®ã使ç¨ãã¦è»¢éã§ããã ã»ãã·ã§ã³ãå®äºããã
       close(2) ãè¡ãªãããã帯åå¤ãã¼ã¿ã®è»¢éã send(2)  ã«è¨è¿°ããã¦ããã åä¿¡ã
       recv(2)  ã«è¨è¿°ããã¦ããã

       SOCK_STREAM ãå®è£ããéä¿¡ãããã³ã«ã¯ãã¼ã¿ã«æ失ãéè¤ããªããã¨ãä¿è¨¼ããã
       ããç¸æã®ãããã³ã«ããããã¡ã¼ç©ºéãæ㤠ãã¼ã¿ã®æçãé©å½ãªæéã®ãã¡ã«è»¢éã§ããªããã°ã
       æ¥ç¶ã¯æãããã¨ã¿ãªãããã®ã½ã±ãã SO_KEEPALIVE ãæå¹ã«ãªã£ã¦ããå ´åããã‐
       ãã³ã«ç¬èªã®æ¹æ³ã§æ¥ç¶ã®ç¸æå´ãã¾ã  æå¹ã§ãããããã§ãã¯ããã ãããã‐
       ã»ã¹ããå£ããã¹ããªã¼ã ã§ãã¼ã¿ãéåä¿¡ãããã¨ããå ´åã«ã¯ SIGPIPE ã·ã°ãã«ãéããã;
       ããã¯é常ã®ãã®ã·ã°ãã«ãæ±ã£ã¦ããªãããã»ã¹ã çµäºãããã SOCK_SEQPACKET ã½ã±ããã¯
       SOCK_STREAM ã½ã±ããã¨åãã·ã¹ãã ã³ã¼ã«ã使ç¨ããã å¯ä¸ã®éã㯠read(2)
       ã³ã¼ã«ãè¦æ±ãããéã®ãã¼ã¿ã®ã¿ãè¿ããå°çãããã±ããã®æ®ãã®é¨åã
       æ¨ã¦ã¦ãã¾ããã¨ã§ãããåæ§ã«å¥ã£ã¦ãããã¼ã¿ã°ã©ã ã®å¨ã¦ã®ã¡ãã»ã¼ã¸å¢ç㯠ä¿å‐
       ãããã

       SOCK_DGRAM 㨠SOCK_RAW ã½ã±ãã㯠sendto(2)
       ã³ã¼ã«ã§æå®ãããç¸æã¸ãã¼ã¿ã°ã©ã ãéããã¨ã許ããã¦ããã ãã¼ã¿ã°ã©ã ã¯ä¸è¬ã«
       recvfrom(2)  ã§åãã¨ãã
       ãã®ã³ã¼ã«ã¯æ¬¡ã®ãã¼ã¿ã°ã©ã ãéä¿¡èã®ã¢ãã¬ã¹ã¨ä¸ç·ã«è¿ãã

       SOCK_PACKET ã¯å¤ãã½ã±ããåã§ãç(raw)ã®ãã±ãããããã¤ã¹ãã©ã¤ããã
       ç´æ¥åä¿¡ããããã®ãã®ã§ããã ä»ã¯ä»£ããã« packet(7)  ãç¨ãããã¨ã

       fcntl(2)  ã® F_SETOWN æä½ã使ã£ã¦ãã·ã°ãã« SIGURG ã SIGPIPE ãåãã¨ããã‐
       ã»ã¹ã°ã«ã¼ããæå®ã§ããã SIGURG ã·ã°ãã«ã¯å¸¯åå¤ãã¼ã¿ãå°çããæã«ã SIGPIPE
       ã·ã°ãã«ã¯ SOCK_STREAM æ¥ç¶ãäºæããåæãããæã«éãããã ã¾ãã F_SETOWN æä½ã¯ãI/O ã
       I/O ã¤ãã³ãã®éåæ (asynchronous) éç¥ã SIGIO ãçµç±ã§åãåãããã»ã¹ããã‐
       ã»ã¹ã°ã«ã¼ããè¨å®ããã®ã«ã使ç¨ã§ããã F_SETOWN ã使ç¨ãããã¨ã¯ FIOSETOWN
       ã¾ã㯠SIOCSPGRP ã®å¼æ°ã§ ioctl(2)  ã使ç¨ãããã¨ã¨ç価ã§ããã

       ãããã¯ã¼ã¯ããããã³ã«ã¢ã¸ã¥ã¼ã«ã«ã¨ã©ã¼ç¶æãä¼ããå ´å (ä¾ãã°ãIP ã® ICMP
       ã¡ãã»ã¼ã¸ã使ç¨ãã¦)ã«ã¯ãã½ã±ããã® ãã³ãã£ã³ã°ã¨ã©ã¼ãã©ã°ãè¨‐
       å®ãããã次ã«ãã®ã½ã±ãããæä½ãã æã«ãã³ãã£ã³ã°ããã¦ããã¨ã©ã¼ã³ã¼ããè¿ãããããã‐
       ãã³ã«ã«ãã£ã¦ã¯ ã¨ã©ã¼ã«ã¤ãã¦ã®ãã詳ããæå ±ãåãåãããã«ã½ã±ãããã¨ã®ã¨ã©ã¼ã‐
       ã¥ã¼ã åãåããã¨ãå¯è½ã§ããã ip(7)  ã® IP_RECVERR ãåç§ãããã¨ã

       ã½ã±ããã®æä½ã¯ã½ã±ããã¬ãã« options ã«ãã£ã¦å¶å¾¡ãããã ãããã®ãªãã·ã§ã³ã¯
       <sys/socket.h> ã«å®ç¾©ããã¦ããã setsockopt(2)  㨠getsockopt(2)
       é¢æ°ã¯ãããããªãã·ã§ã³ã®è¨å®ã¨åå¾ãè¡ãªãã

è¿ãå¤
       æåããå ´åãæ°ããã½ã±ããã®ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ã¼ãè¿ãã ã¨ã©ã¼ãçºçããå ´å㯠-1
       ãè¿ãã errno ãé©åã«è¨å®ããã

ã¨ã©ã¼
       EACCES æå®ãããã¿ã¤ãã¾ãã¯ãããã³ã«ã®ã½ã±ãããä½æãã許å¯ãä¸ãããã¦ããªãã

       EAFNOSUPPORT
              æå®ãããã¢ãã¬ã¹ãã¡ããªã¼ããµãã¼ãããã¦ããªãã

       EINVAL ç¥ããªããããã³ã«ãã¾ãã¯å©ç¨ã§ããªããããã³ã«ãã¡ããªã¼ã§ããã

       EINVAL type ã«ç¡å¹ãªãã©ã°ãæå®ããã¦ããã

       EMFILE ããã»ã¹ã®ãã¡ã¤ã«ãã¼ãã«ã溢ãã¦ããã

       ENFILE ãªã¼ãã³ããããã¡ã¤ã«ã®ç·æ°ãã·ã¹ãã å¨ä½ã®ä¸éã«éãã¦ããã

       ENOBUFS ã¾ã㯠ENOMEM
              ååãªã¡ã¢ãªã¼ããªããååãªè³æºã解æ¾ãããã¾ã§ã¯ã½ã±ããã
              ä½æãããã¨ã¯ã§ããªãã

       EPROTONOSUPPORT
              ãã®ãã¡ã¤ã³ã§ã¯æå®ããããããã³ã«ã¾ãã¯ãããã³ã«ã¿ã¤ãã ãµãã¼ãããã¦ããªãã

       ä¸ä½ã®ãããã³ã«ã¢ã¸ã¥ã¼ã«ããä»ã®ã¨ã©ã¼ãçæããããããããªãã

æºæ
       4.4BSD, POSIX.1-2001.

       ãã©ã° SOCK_NONBLOCK, SOCK_CLOEXEC 㯠Linux åºæã§ããã

       socket()  㯠4.2BSD ã§ç»å ´ãããä¸è¬ã«ã(System V ã®å¤ç¨®ãå«ãã¦)  BSD
       ã®ã½ã±ãã層ã®äºææ§ããµãã¼ããã¦ãã BSD 以å¤ã®ã·ã¹ãã ã¸ã®ã ã¾ãã¯ãBSD
       以å¤ã®ã·ã¹ãã ããã®ç§»æ¤ãã§ããã

注æ
       POSIX.1-2001 ã§ã¯ <sys/types.h> ã®ã¤ã³ã¯ã«ã¼ãã¯å¿é ã¨ããã¦ãããã Linux
       ã§ã¯ãã®ãããã¼ãã¡ã¤ã«ã¯å¿è¦ã§ã¯ãªãã ããããæ´å²çã«ã¯ãããã¤ãã®å®è£ (BSD ç³»)
       ã§ãã®ãããã¼ãã¡ã¤ã«ã
       å¿è¦ã§ããã移æ¤æ§ãå¿è¦ãªã¢ããªã±ã¼ã·ã§ã³ã§ã¯ãã®ãã¡ã¤ã«ã
       ã¤ã³ã¯ã«ã¼ãããã®ãè³¢æã§ãããã

       4.x BSD ã«ããã¦å®æ°ã使ç¨ããå ´åããããã³ã«ãã¡ããªã¼ã«ã¯ PF_UNIX, PF_INET ç‐
       ã使ç¨ãã¦ãããä¸æ¹ã§ã¢ãã¬ã¹ãã¡ããªã¼ã«ã¯ AF_UNIX, AF_INET çã使ç¨ããã¦ããã
       ããããªãã BSD ã®ããã¥ã¢ã«ã§ã¯ ãä¸è¬ã«ãããã³ã«ãã¡ããªã¼ã¯
       ã¢ãã¬ã¹ãã¡ããªã¼ã¨åããã®ã§ãããã ã¨ä¿è¨¼ãã¦ããã
       ãã以å¤ã®è¦æ ¼ã§ã¯å¨ã¦ã®å ´æ㧠AF_* ã使ç¨ããã¦ããã

       AF_ALG ãããã³ã«ç¨®å¥ã¯ Linux 2.6.38 ã§è¿½å ãããã
       ãã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ã®è©³ããæå ±ã¯ã ã«ã¼ãã«ã®ã½ã¼ã¹ãã¡ã¤ã«
       Documentation/crypto/crypto-API-userspace.txt ã«æ¸ããã¦ããã

ä¾
       socket()  ã®å©ç¨ä¾ã getaddrinfo(3)  ã«è¨è¼ããã¦ããã

é¢é£é ç®
       accept(2), bind(2), connect(2), fcntl(2), getpeername(2),
       getsockname(2), getsockopt(2), ioctl(2), listen(2), read(2), recv(2),
       select(2), send(2), shutdown(2), socketpair(2), write(2),
       getprotoent(3), ip(7), socket(7), tcp(7), udp(7), unix(7)

       “An Introductory 4.3BSD Interprocess Communication Tutorial” and “BSD
       Interprocess Communication Tutorial”, (UNIX Programmer's Supplementary
       Documents Volume 1. ã¨ãã¦åçããã)

ãã®ææ¸ã«ã¤ãã¦
       ãã® man ãã¼ã¸ã¯ Linux man-pages ããã¸ã§ã¯ãã®ãªãªã¼ã¹ 3.79 ã®ä¸é¨
       ã§ãããããã¸ã§ã¯ãã®èª¬æã¨ãã°å ±åã«é¢ããæå ±ã¯
       http://www.kernel.org/doc/man-pages/ ã«æ¸ããã¦ããã



Linux                             2015-02-01                         SOCKET(2)