accept

The accept command in Linux is used to accept a connection on a socket. It is a system call that is used by network servers to accept connections from clients.

The sockfd argument is the file descriptor of the socket that is listening for connections. The addr argument is a pointer to a sockaddr structure that will be filled in with the address of the client that connected. The addrlen argument is the size of the addr structure.

The accept command returns a file descriptor that can be used to communicate with the client.

Here is an example of how to use the accept command:

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>

int main() {
  int sockfd, new_sockfd;
  struct sockaddr_in addr;
  socklen_t addrlen;

  // Create a socket.
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    perror("socket");
    return 1;
  }

  // Bind the socket to a port.
  addr.sin_family = AF_INET;
  addr.sin_port = htons(8080);
  addr.sin_addr.s_addr = INADDR_ANY;
  addrlen = sizeof(addr);
  if (bind(sockfd, (struct sockaddr *)&addr, addrlen) < 0) {
    perror("bind");
    return 1;
  }

  // Listen for connections.
  listen(sockfd, 10);

  // Accept a connection.
  new_sockfd = accept(sockfd, (struct sockaddr *)&addr, &addrlen);
  if (new_sockfd < 0) {
    perror("accept");
    return 1;
  }

  // Do something with the new connection.

  return 0;
}

This code will create a socket, bind it to port 8080, and listen for connections. When a connection is accepted, the code will do something with the new connection.

I hope this helps! Let me know if you have any other questions.

Last updated