#include <sys/types.h>
|
#include <sys/socket.h>
|
int
accept (int s, struct sockaddr * restrict addr, socklen_t * restrict addrlen); |
If no pending connections are present on the queue, and the original socket is not marked as non-blocking, accept blocks the caller until a connection is present. If the original socket is marked non-blocking and no pending connections are present on the queue, accept returns an error as described below. The accepted socket may not be used to accept more connections. The original socket s remains open.
The argument addr is a result argument that is filled-in with the address of the connecting entity, as known to the communications layer. The exact format of the addr argument is determined by the domain in which the communication is occurring. A null pointer may be specified for addr if the address information is not desired; in this case, addrlen is not used and should also be null. Otherwise, the addrlen argument is a value-result argument; it should initially contain the amount of space pointed to by addr; on return it will contain the actual length (in bytes) of the address returned. This call is used with connection-based socket types, currently with SOCK_STREAM.
It is possible to select a socket for the purposes of doing an accept by selecting it for read.
For certain protocols which require an explicit confirmation, such as ISO or DATAKIT, accept can be thought of as merely dequeueing 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.
#include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> void GetSockName() { int sock_fd; int newsock_fd; struct sockaddr_in addr; struct sockaddr_in ss; struct sockaddr_in new_socket; unsigned int len; unsigned int addr_len; sock_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(5000); bind(sock_fd,(sockaddr*)&addr,sizeof(addr)); listen(sock_fd,1); newsock_fd = accept(sock_fd,(sockaddr*)&new_socket,&addr_len); // Code blocks here if (newsock_fd <= 0) { perror("accept:"); } close(newsock_fd); close(sock_fd); }
[EBADF] | |
The descriptor is invalid. | |
[EINTR] | |
The accept operation was interrupted. | |
[EMFILE] | |
The per-process descriptor table is full. | |
[ENFILE] | |
The system file table is full. | |
[ENOTSOCK] | |
The descriptor references a file, not a socket. | |
[EINVAL] | |
listen has not been called on the socket descriptor. The addrlen argument is invalid. | |
[EWOULDBLOCK] | |
The socket is marked non-blocking and no connections are present to be accepted. | |
[ECONNABORTED] | |
A connection arrived, but it was closed while waiting on the listen queue. | |
The accept system call appeared in BSD 4.2 .
Feedback
For additional information or queries on this page send feedback
© 2005-2007 Nokia |