The accept() system call with the connection-based socket types(SOCK_STREAM, SOCK_SEQPACKET). It extracts the first connection request on queue of pending connections for the listening socket, sockfd, creates a new connected socket, and returns a new file descriptor referring to that socket. The newly created socket is not in the listening state. The original socket sockfd is unaffected by this call.
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
- sockfd: The argument sockfd is a socket that has been created with socket(), bound to a local address with bind(), and is listening for connections after a listen().
- addr: The argument addr is a pointer to a sockaddr structure. This structure is filled in with the address of the peer socket, as known to the communications layer. The exact format of the address returned addr is determined by the socket’s address family. When addr is NULL,
nothing is filled in; in this case, addrlen is not used, and should also be NULL.
- addrlen: The addrlen argument is a value-result argument: the caller must initialize it to contain the size (in bytes) of the structure pointed to by addr; on return it will contain the actual size of the peer address.
- Returns: On success, these system calls return a non-negative integer that is a file descriptor for the accepted socket. On error, -1 is returned, and errno is set appropriately.
The returned address is truncated if the buffer provided is too small; in this case, addrlen will return a value greater than was supplied to the call.
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() fails with the error EAGAIN or EWOULDBLOCK.
Binding Successfully Listening..
Errors in accept() call
- EAGAIN or EWOULDBLOCK : The socket is marked nonblocking and no connections are present to be accepted. POSIX.1-2001 and POSIX.1-2008 allow either error to be returned for this case, and do not require these constants to have the same value, so a portable application should check for both possibilities.
- EBADF : sockfd is not an open file descriptor.
- ECONNABORTED : A connection has been aborted.
- EFAULT : The addr argument is not in a writable part of the user address space.
- EINTR : The system call was interrupted by a signal that was caught before a valid connection arrived; see signal(7).
- EINVAL : Socket is not listening for connections, or addrlen is invalid (e.g., is negative).
- EINVAL : (accept4()) invalid value in flags.
- EMFILE : The per-process limit on the number of open file descriptors has been reached.
- ENFILE : The system-wide limit on the total number of open files has been reached.
- 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.
- ENOTSOCK : The file descriptor sockfd does not refer to a socket.
- EOPNOTSUPP : The referenced socket is not of type SOCK_STREAM.
- EPROTO: Protocol error.
This article is contributed by Pramod Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Wait System Call in C
- pipe() System call
- dup() and dup2() Linux system call
- Difference between Call by Value and Call by Reference
- Can we call an undeclared function in C++?
- A C/C++ Function Call Puzzle
- Is it possible to call constructor and destructor explicitly?
- Difference between CALL and JUMP instructions
- system() in C/C++
- Amazing stuff with system() in C / C++
- Input-output system calls in C | Create, Open, Close, Read, Write
- Slack Bytes in Structures : Explained with Example
- Difference between Iterators and Pointers in C/C++ with Examples
- C program to count number of vowels and consonants in a String