accept 中文man頁面
NAME 名稱
accept - 在一個套接字上接收一個連接
SYNOPSIS 概述
#include <sys/types.h>
#include <sys/socket.h>
int accept(int s, struct sockaddr *addr, socklen_t *addrlen);
DESCRIPTION 描述
accept 函數(shù)用于基于連接的套接字 (SOCK_STREAM, SOCK_SEQPACKET 和 SOCK_RDM). 它從未完成連接隊列中取出第一個連接請求,創(chuàng)建一個和參數(shù) s 屬性相同的連接套接字,并為這個套接字分配一個文件描述符, 然后以這個描述符返回.新創(chuàng)建的描述符不再處于傾聽狀態(tài).原套接字 s 不受此調(diào)用的影響.注意任意一個文件描述符標志 (任何可以被 fcntl以參數(shù) F_SETFL 設(shè)置的值,比如非阻塞式或者異步狀態(tài))不會被 accept. 所繼承.
參數(shù) s 是以 socket(2) 創(chuàng)建,用 bind(2) 綁定到一個本地地址,并且在調(diào)用了 listen(2). 之后正在偵聽一個連接的套接字. 參數(shù) addr 是一個指向結(jié)構(gòu)sockaddr的指針.這個結(jié)構(gòu)體以連接實體地址填充. 所謂的連接實體,就是眾所周知的網(wǎng)絡(luò)層.參數(shù) addr 所傳遞的真正的地址格式依賴于所使用的套接字族. (參見 socket(2) 和各協(xié)議自己的手冊頁). addrlen 是一個實時參數(shù): 它的大小應(yīng)該能夠足以容納參數(shù) addr 所指向的結(jié)構(gòu)體;在函數(shù)返回時此參數(shù)將以字節(jié)數(shù)表示出返回地址的實際長度.若 addr 使用NULL作為參數(shù),addrlen將也被置為NULL.
如果隊列中沒有未完成連接套接字,并且套接字沒有標記為非阻塞式, accept 將阻塞直到一個連接到達.如果一個套接字被標記為非阻塞式而隊列中沒有未完成連接套接字, accept 將返回EAGAIN.
使用 select(2) 或者 poll(2). 可以在一個套接字上有連接到來時產(chǎn)生事件.當嘗試一個新的連接時套接字讀就緒,這樣我們就可以調(diào)用 accept 為這個連接獲得一個新的套接字.此外,你還可以設(shè)置套接字在喚醒時接收到信號 SIGIO; 細節(jié)請參見 socket(7)
對于那些需要顯式確認的協(xié)議,比如 DECNet, accept 可以看作僅僅從隊列中取出下一個連接而不做確認.當在這個新的文件描述符上進行普通讀寫操作時暗示了確認,當關(guān)閉這個新的套接字時暗示了拒絕.目前在Linux上只有DECNet有這樣的含義.
NOTES 注意
當接收到一個 SIGIO 信號或者 select(2) 或 poll(2) 返回讀就緒并不總是意味著有新連接在等待,因為連接可能在調(diào)用 accept 之前已經(jīng)被異步網(wǎng)絡(luò)錯誤或者其他線程所移除.如果發(fā)生這種情況, 那么調(diào)用將阻塞并等待下一個連接的到來.為確保 accept 永遠不會阻塞,傳遞的套接字 s 需要置 O_NONBLOCK 標志(參見 socket(7)).
RETURN VALUE 返回值
此調(diào)用在發(fā)生錯誤時返回-1.若成功則返回一個非負整數(shù)標識這個連接套接字.
ERROR HANDLING 錯誤處理
Linux accept 將一個待處理網(wǎng)絡(luò)錯誤代碼通過 accept 傳遞給新套接字 . 這種處理方式有別于其他的BSD套接字實現(xiàn).為可靠操作,應(yīng)用程序必須在調(diào)用 accept 之后能夠檢測這些為協(xié)議定義的網(wǎng)絡(luò)錯誤,并且以重試解決,就象 EAGAIN 一樣.對于TCP/IP這些網(wǎng)絡(luò)錯誤是 ENETDOWN, EPROTO, ENOPROTOOPT, EHOSTDOWN, ENONET, EHOSTUNREACH, EOPNOTSUPP, 以及 ENETUNREACH.
ERRORS 錯誤
- EAGAIN或者EWOULDBLOCK
- 套接字被標記為非阻塞,且當前沒有可接收的連接.
- EBADF
- 描述符非法.
- ENOTSOCK
- 描述符指向一個文件,而不是一個套接字.
- EOPNOTSUPP
- 作為參數(shù)的套接字不是 SOCK_STREAM. 類型
- EFAULT
- 參數(shù) addr 不在用戶可寫地址空間之內(nèi).
- EPERM
- 防火墻規(guī)則禁止連接.
- ENOBUFS,ENOMEM
- 沒有足夠內(nèi)存. 這個錯誤一般來說意味著內(nèi)存分配受套接字緩沖區(qū)所限, 而不是沒有系統(tǒng)內(nèi)存.
另外,新套接字和協(xié)議中定義的網(wǎng)絡(luò)錯誤也可能被返回. 不同的Linux內(nèi)核也可能返回下列錯誤 EMFILE, EINVAL, ENOSR, ENOBUFS, EPERM, ECONNABORTED, ESOCKTNOSUPPORT, EPROTONOSUPPORT, ETIMEDOUT, ERESTARTSYS.
CONFORMING TO 兼容于
SVr4,4.4BSD( accept 函數(shù)首次出現(xiàn)于BSD 4.2). BSD手冊頁文檔定義了五個可能的錯誤返回值 (EBADF, ENOTSOCK, EOPNOTSUPP, EWOULDBLOCK, EFAULT). SUSv2文檔的定義是EAGAIN, EBADF, ECONNABORTED, EFAULT, EINTR, EINVAL, EMFILE, ENFILE, ENOBUFS, ENOMEM, ENOSR, ENOTSOCK, EOPNOTSUPP, EPROTO, EWOULDBLOCK.
Linux accept不繼承象 O_NONBLOCK 這樣的套接字標志. 這一點有別于其他的BSD套接字實現(xiàn). 因此,程序應(yīng)該在accept所返回的套接字上設(shè)置所有需要的標志.
NOTE 注意
函數(shù) accept 的第三個參數(shù)原來被聲明為'int *'(在libc4和libc5以及其他很多系統(tǒng)中, 比如BSD 4.*,SunOS 4, SGI);POSIX 1003.1g草案試圖將其改變?yōu)?`size_t *',SunOS 5就是這么做的. 后來的POSIX草案和Single Unix Specification以及glibc2使用了 `socklen_t *'. Quoting Linus Torvalds: 引自Linus Torvalds (譯注:這個家伙就是Linux的創(chuàng)始人,所以我保留了他老人家的原文, 僅將原文大意附后): I fails: only italicizes a single line _Any_ sane library _must_ have "socklen_t" be the same size as int. Anything else breaks any BSD socket layer stuff. POSIX initially _did_ make it a size_t, and I (and hopefully others, but obviously not too many) complained to them very loudly indeed. Making it a size_t is completely broken, exactly because size_t very seldom is the same size as "int" on 64-bit architectures, for example. And it _has_ to be the same size as "int" because that's what the BSD socket interface is. Anyway, the POSIX people eventually got a clue, and created "socklen_t". They shouldn't have touched it in the first place, but once they did they felt it had to have a named type for some unfathomable reason (probably somebody didn't like losing face over having done the original stupid thing, so they silently just renamed their blunder).
數(shù)據(jù)類型"socklen_t"和int應(yīng)該具有相同的長度.否則就會破壞 BSD套接字層的填充.POSIX開始的時候用的是size_t, Linus Torvalds(他希望有更多的人,但顯然不是很多) 努力向他們解釋使用size_t是完全錯誤的,因為在64位結(jié)構(gòu)中 size_t和int的長度是不一樣的,而這個參數(shù)(也就是accept函數(shù)的第三參數(shù))的長度必須和int一致,因為這是BSD套接字接口標準.最終POSIX的那幫家伙找到了解決的辦法,那就是創(chuàng)造了一個新的類型"socklen_t".Linux Torvalds說這是由于他們發(fā)現(xiàn)了自己的錯誤但又不好意思向大家伙兒承認,所以另外創(chuàng)造了一個新的數(shù)據(jù)類型.
SEE ALSO 參見
bind(2), connect(2), listen(2), select(2), socket(2)
#p#
NAME
accept - accept a connection on a socket
SYNOPSIS
#include <sys/types.h>
#include <sys/socket.h>
int accept(int s, struct sockaddr *addr, socklen_t *addrlen);
DESCRIPTION
The accept function is used with connection-based socket types (SOCK_STREAM, SOCK_SEQPACKET and SOCK_RDM). It extracts the first connection request on the queue of pending connections, creates a new connected socket with mostly the same properties as s, and allocates a new file descriptor for the socket, which is returned. The newly created socket is no longer in the listening state. The original socket s is unaffected by this call. Note that any per file descriptor flags (everything that can be set with the F_SETFL fcntl, like non blocking or async state) are not inherited across an accept.
The argument s 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 connecting entity, as known to the communications layer. The exact format of the address passed in the addr parameter is determined by the socket's family (see socket(2) and the respective protocol man pages). The addrlen argument is a value-result parameter: it should initially contain the size of the structure pointed to by addr; on return it will contain the actual length (in bytes) of the address returned. When addr is NULL nothing is filled in.
If no pending connections are present on the queue, and the socket is not marked as non-blocking, accept blocks the caller until a connection is present. If the socket is marked non-blocking and no pending connections are present on the queue, accept returns EAGAIN.
In order to be notified of incoming connections on a socket, you can use select(2) or poll(2). A readable event will be delivered when a new connection is attempted and you may then call accept to get a socket for that connection. Alternatively, you can set the socket to deliver SIGIO when activity occurs on a socket; see socket(7) for details.
For certain protocols which require an explicit confirmation, such as DECNet, accept can be thought of as merely dequeuing the next connection request and not implying confirmation. Confirmation can be implied by a normal read or write on the new file descriptor, and rejection can be implied by closing the new socket. Currently only DECNet has these semantics on Linux.
NOTES
There may not always be a connection waiting after a SIGIO is delivered or select(2) or poll(2) return a readability event because the connection might have been removed by an asynchronous network error or another thread before accept is called. If this happens then the call will block waiting for the next connection to arrive. To ensure that accept never blocks, the passed socket s needs to have the O_NONBLOCK flag set (see socket(7)).
RETURN VALUE
The call returns -1 on error. If it succeeds, it returns a non-negative integer that is a descriptor for the accepted socket.
ERROR HANDLING
Linux accept passes already-pending network errors on the new socket as an error code from accept. This behaviour differs from other BSD socket implementations. For reliable operation the application should detect the network errors defined for the protocol after accept and treat them like EAGAIN by retrying. In case of TCP/IP these are ENETDOWN, EPROTO, ENOPROTOOPT, EHOSTDOWN, ENONET, EHOSTUNREACH, EOPNOTSUPP, and ENETUNREACH.
ERRORS
accept shall fail if:
- EAGAIN or EWOULDBLOCK
- The socket is marked non-blocking and no connections are present to be accepted.
- EBADF
- The descriptor is invalid.
- ENOTSOCK
- The descriptor references a file, not a socket.
- EOPNOTSUPP
- The referenced socket is not of type SOCK_STREAM.
- EINTR
- The system call was interrupted by a signal that was caught before a valid connection arrived.
- ECONNABORTED
- A connection has been aborted.
- EINVAL
- Socket is not listening for connections.
- EMFILE
- The per-process limit of open file descriptors has been reached.
- ENFILE
- The system maximum for file descriptors has been reached.
accept may fail if:
- EFAULT
- The addr parameter is not in a writable part of the user address space.
- ENOBUFS, ENOMEM
- Not enough free memory. This often means that the memory allocation is limited by the socket buffer limits, not by the system memory.
- EPROTO
- Protocol error.
Linux accept may fail if:
- EPERM
- Firewall rules forbid connection.
In addition, network errors for the new socket and as defined for the protocol may be returned. Various Linux kernels can return other errors such as ENOSR, ESOCKTNOSUPPORT, EPROTONOSUPPORT, ETIMEDOUT. The value ERESTARTSYS may be seen during a trace.
CONFORMING TO
SVr4, 4.4BSD (the accept function first appeared in BSD 4.2). The BSD man page documents five possible error returns (EBADF, ENOTSOCK, EOPNOTSUPP, EWOULDBLOCK, EFAULT). SUSv3 documents errors EAGAIN, EBADF, ECONNABORTED, EINTR, EINVAL, EMFILE, ENFILE, ENOBUFS, ENOMEM, ENOTSOCK, EOPNOTSUPP, EPROTO, EWOULDBLOCK. In addition, SUSv2 documents EFAULT and ENOSR.
Linux accept does _not_ inherit socket flags like O_NONBLOCK. This behaviour differs from other BSD socket implementations. Portable programs should not rely on this behaviour and always set all required flags on the socket returned from accept.
NOTE
The third argument of accept was originally declared as an `int *' (and is that under libc4 and libc5 and on many other systems like BSD 4.*, SunOS 4, SGI); a POSIX 1003.1g draft standard wanted to change it into a `size_t *', and that is what it is for SunOS 5. Later POSIX drafts have `socklen_t *', and so do the Single Unix Specification and glibc2. Quoting Linus Torvalds: _Any_ sane library _must_ have "socklen_t" be the same size as int. Anything else breaks any BSD socket layer stuff. POSIX initially _did_ make it a size_t, and I (and hopefully others, but obviously not too many) complained to them very loudly indeed. Making it a size_t is completely broken, exactly because size_t very seldom is the same size as "int" on 64-bit architectures, for example. And it _has_ to be the same size as "int" because that's what the BSD socket interface is. Anyway, the POSIX people eventually got a clue, and created "socklen_t". They shouldn't have touched it in the first place, but once they did they felt it had to have a named type for some unfathomable reason (probably somebody didn't like losing face over having done the original stupid thing, so they silently just renamed their blunder).
SEE ALSO
bind(2), connect(2), listen(2), select(2), socket(2)