Accept system call - GeeksforGeeks

14 Jun, 2017
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); 
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");                                                                                                                     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
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 memoryallocation 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.
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 memoryallocation 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 contribute@geeksforgeeks.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.
system-programming
C Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
TCP Server-Client implementation in C
Multithreading in C
Exception Handling in C++
'this' pointer in C++
Arrow operator -> in C/C++ with Examples
UDP Server-Client implementation in C
Understanding "extern" keyword in C
Smart Pointers in C++ and How to Use Them
Multiple Inheritance in C++
Sorting Vector of Pairs in C++ | Set 1 (Sort by first and second)