Open In App

Accept system call


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.
Syntax:

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

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");                                                                                                             
        else                                                                                                                                                
                printf("Binding Error\n"); 
        // Listening                                                                                                                 
        l = listen(sd, BACKLOG);                                                                                                                             
        if(l > 0)                                                                                                                                            
                printf("Listening...\n");                                                                                                                    
        else                                                                                                                                                
                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("Accepted\n");                                                                                                                        
        else                                                                                                                                                
                printf("Not accepted\n");                                                                                                                    
}                                           
  
                                  

Output:

Binding Successfully
Listening.. 

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.

Article Tags :