Skip to content
Related Articles

Related Articles

Accept system call

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 14 Jun, 2017
Improve Article
Save Article

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.

// C program to show accept system call is blocking call
#include <stdio.h>                                                                                                                                       
#include <sys/types.h>                                                                                                                                   
#include <sys/socket.h>                                                                                                                                  
#include <netinet/in.h>                                                                                                                                  
#include <string.h>                                                                                                                                      
#define BACKLOG 3                                                                                                                                            
// Driver program                                                                                                                                                            
int main()                                                                                                                                               
        struct sockaddr_in my_addr, peer_addr;                                                                                                               
        socklen_t peer_addr_size;                                                                                                                            
        int sd, b, l, acc;                                                                                                                                   
        my_addr.sin_family = AF_INET;
        // Creating socket                                                                                                                       
        sd = socket(AF_INET, SOCK_STREAM, 0); 
        // Binding                                                                                                               
        b = bind(sd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr_in));                                                                               
        if(b > 0)                                                                                                                                            
                printf("Binded Successfully\n");                                                                                                             
                printf("Binding Error\n"); 
        // Listening                                                                                                                 
        l = listen(sd, BACKLOG);                                                                                                                             
        if(l > 0)                                                                                                                                            
                printf("Not listening..\n");                                                                                                                 
        peer_addr_size = sizeof(struct sockaddr_in); 
        // Accept system call                                                                                                    
        acc = accept(sd, (struct sockaddr *)&peer_addr, &peer_addr_size);                                                                                    
        if(acc > 0)                                                                                                                                      
                printf("Not accepted\n");                                                                                                                    


Binding Successfully

Errors in accept() call

  1. 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.
  2. EBADF : sockfd is not an open file descriptor.
  3. ECONNABORTED : A connection has been aborted.
  4. EFAULT : The addr argument is not in a writable part of the user address space.
  5. EINTR : The system call was interrupted by a signal that was caught before a valid connection arrived; see signal(7).
  6. EINVAL : Socket is not listening for connections, or addrlen is invalid (e.g., is negative).
  7. EINVAL : (accept4()) invalid value in flags.
  8. EMFILE : The per-process limit on the number of open file descriptors has been reached.
  9. ENFILE : The system-wide limit on the total number of open files has been reached.
  10. 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.
  11. ENOTSOCK : The file descriptor sockfd does not refer to a socket.
  12. EOPNOTSUPP : The referenced socket is not of type SOCK_STREAM.
  13. 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 or mail your article to 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.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!