成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

accept 中文man頁面

系統
accept 函數用于基于連接的套接字 (SOCK_STREAM, SOCK_SEQPACKET 和 SOCK_RDM). 它從未完成連接隊列中取出第一個連接請求,創建一個和參數 s 屬性相同的連接套接字,并為這個套接字分配一個文件描述符, 然后以這個描述符返回.新創建的描述符不再處于傾聽狀態.原套接字 s 不受此調用的影響.注意任意一個文件描述符標志 (任何可以被 fcntl以參數 F_SETFL 設置的值,比如非阻塞式或者異步狀態)不會被 accept. 所繼承.

NAME 名稱

accept - 在一個套接字上接收一個連接  

SYNOPSIS 概述

#include <sys/types.h>
#include <sys/socket.h>

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

DESCRIPTION 描述

accept 函數用于基于連接的套接字 (SOCK_STREAM, SOCK_SEQPACKETSOCK_RDM). 它從未完成連接隊列中取出第一個連接請求,創建一個和參數 s 屬性相同的連接套接字,并為這個套接字分配一個文件描述符, 然后以這個描述符返回.新創建的描述符不再處于傾聽狀態.原套接字 s 不受此調用的影響.注意任意一個文件描述符標志 (任何可以被 fcntl以參數 F_SETFL 設置的值,比如非阻塞式或者異步狀態)不會被 accept. 所繼承.

參數 s 是以 socket(2) 創建,用 bind(2) 綁定到一個本地地址,并且在調用了 listen(2). 之后正在偵聽一個連接的套接字. 參數 addr 是一個指向結構sockaddr的指針.這個結構體以連接實體地址填充. 所謂的連接實體,就是眾所周知的網絡層.參數 addr 所傳遞的真正的地址格式依賴于所使用的套接字族. (參見 socket(2) 和各協議自己的手冊頁). addrlen 是一個實時參數: 它的大小應該能夠足以容納參數 addr 所指向的結構體;在函數返回時此參數將以字節數表示出返回地址的實際長度.若 addr 使用NULL作為參數,addrlen將也被置為NULL.

如果隊列中沒有未完成連接套接字,并且套接字沒有標記為非阻塞式, accept 將阻塞直到一個連接到達.如果一個套接字被標記為非阻塞式而隊列中沒有未完成連接套接字, accept 將返回EAGAIN.

使用 select(2) 或者 poll(2). 可以在一個套接字上有連接到來時產生事件.當嘗試一個新的連接時套接字讀就緒,這樣我們就可以調用 accept 為這個連接獲得一個新的套接字.此外,你還可以設置套接字在喚醒時接收到信號 SIGIO; 細節請參見 socket(7)

對于那些需要顯式確認的協議,比如 DECNet, accept 可以看作僅僅從隊列中取出下一個連接而不做確認.當在這個新的文件描述符上進行普通讀寫操作時暗示了確認,當關閉這個新的套接字時暗示了拒絕.目前在Linux上只有DECNet有這樣的含義.  

NOTES 注意

當接收到一個 SIGIO 信號或者 select(2) 或 poll(2) 返回讀就緒并不總是意味著有新連接在等待,因為連接可能在調用 accept 之前已經被異步網絡錯誤或者其他線程所移除.如果發生這種情況, 那么調用將阻塞并等待下一個連接的到來.為確保 accept 永遠不會阻塞,傳遞的套接字 s 需要置 O_NONBLOCK 標志(參見 socket(7)).  

RETURN VALUE 返回值

此調用在發生錯誤時返回-1.若成功則返回一個非負整數標識這個連接套接字.  

ERROR HANDLING 錯誤處理

Linux accept 將一個待處理網絡錯誤代碼通過 accept 傳遞給新套接字 . 這種處理方式有別于其他的BSD套接字實現.為可靠操作,應用程序必須在調用 accept 之后能夠檢測這些為協議定義的網絡錯誤,并且以重試解決,就象 EAGAIN 一樣.對于TCP/IP這些網絡錯誤是 ENETDOWN, EPROTO, ENOPROTOOPT, EHOSTDOWN, ENONET, EHOSTUNREACH, EOPNOTSUPP, 以及 ENETUNREACH.  

ERRORS 錯誤

EAGAIN或者EWOULDBLOCK
套接字被標記為非阻塞,且當前沒有可接收的連接.
EBADF
描述符非法.
ENOTSOCK
描述符指向一個文件,而不是一個套接字.
EOPNOTSUPP
作為參數的套接字不是 SOCK_STREAM. 類型
EFAULT
參數 addr 不在用戶可寫地址空間之內.
EPERM
防火墻規則禁止連接.
ENOBUFS,ENOMEM
沒有足夠內存. 這個錯誤一般來說意味著內存分配受套接字緩沖區所限, 而不是沒有系統內存.

另外,新套接字和協議中定義的網絡錯誤也可能被返回. 不同的Linux內核也可能返回下列錯誤 EMFILE, EINVAL, ENOSR, ENOBUFS, EPERM, ECONNABORTED, ESOCKTNOSUPPORT, EPROTONOSUPPORT, ETIMEDOUT, ERESTARTSYS.  

CONFORMING TO 兼容于

SVr4,4.4BSD( accept 函數首次出現于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套接字實現. 因此,程序應該在accept所返回的套接字上設置所有需要的標志.  

NOTE 注意

函數 accept 的第三個參數原來被聲明為'int *'(在libc4和libc5以及其他很多系統中, 比如BSD 4.*,SunOS 4, SGI);POSIX 1003.1g草案試圖將其改變為 `size_t *',SunOS 5就是這么做的. 后來的POSIX草案和Single Unix Specification以及glibc2使用了 `socklen_t *'. Quoting Linus Torvalds: 引自Linus Torvalds (譯注:這個家伙就是Linux的創始人,所以我保留了他老人家的原文, 僅將原文大意附后): 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).

數據類型"socklen_t"和int應該具有相同的長度.否則就會破壞 BSD套接字層的填充.POSIX開始的時候用的是size_t, Linus Torvalds(他希望有更多的人,但顯然不是很多) 努力向他們解釋使用size_t是完全錯誤的,因為在64位結構中 size_t和int的長度是不一樣的,而這個參數(也就是accept函數的第三參數)的長度必須和int一致,因為這是BSD套接字接口標準.最終POSIX的那幫家伙找到了解決的辦法,那就是創造了一個新的類型"socklen_t".Linux Torvalds說這是由于他們發現了自己的錯誤但又不好意思向大家伙兒承認,所以另外創造了一個新的數據類型.  

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)

責任編輯:韓亞珊 來源: CMPP.net
相關推薦

2011-08-15 10:21:09

man中文man

2011-08-24 16:48:36

man中文man

2011-08-11 16:11:49

at中文man

2011-08-25 10:21:56

man.conf中文man

2011-08-11 15:03:21

ACCESS中文man

2011-08-11 15:28:43

ali中文man

2011-08-11 16:31:49

biff中文man

2011-08-11 17:16:43

cce中文man

2011-08-11 18:05:04

chvt中文man

2011-08-11 18:13:07

clear中文man

2011-08-12 09:13:02

df中文man

2011-08-12 09:38:06

dircolors中文man

2011-08-12 09:44:37

dirname中文man

2011-08-12 10:20:02

echo中文man

2011-08-12 10:25:55

eject中文man

2011-08-12 11:07:19

git中文man

2011-08-12 13:18:19

head中文man

2011-08-12 13:49:23

hostid中文man

2011-08-12 13:54:46

hostname中文man

2011-08-12 14:53:56

kill中文man
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 一区二区三区四区视频 | 亚洲免费网 | 91国内产香蕉 | 精品国产乱码久久久久久蜜臀 | 精品成人佐山爱一区二区 | 国产精品福利网站 | www.黄网| 欧美精品一区三区 | 国产精品一区久久久 | www.久久久.com| 天堂中文av | 亚洲午夜一区二区 | 精品中文字幕一区 | 亚洲欧美另类在线观看 | 精品亚洲一区二区三区 | 国产精品欧美一区喷水 | 国产高清精品一区二区三区 | 亚洲一区二区免费看 | 国产在线精品一区二区 | 国产成人99久久亚洲综合精品 | 欧美国产日韩在线 | 麻豆一区一区三区四区 | 成人国产精品久久久 | 999免费观看视频 | 天天成人综合网 | 中文在线日韩 | 91精品国产综合久久精品图片 | 婷婷综合色 | 激情欧美一区二区三区中文字幕 | 欧美视频网 | 91精品一区 | 国产视频久久 | 综合精品久久久 | 欧美视频在线免费 | 污污的网站在线观看 | 中文字幕国产视频 | 欧美日韩国产一区 | 亚洲精品自拍视频 | 欧美日韩久| jizjizjiz中国护士18 | 成人av播放 |