Skip to main content

System Calls in Network Programming

What Are System Calls in Network Programming?

System calls are low-level functions that interact directly with the kernel, enabling user applications to communicate with hardware or perform networking tasks like creating sockets, sending/receiving data, and managing connections.Let's break down the key network programming functions and explain the parameters used in each.


1. socket()

Creates an endpoint for communication.


int socket(int domain, int type, int protocol);
  • domain – Specifies the protocol family (addressing type).
    • AF_INET – IPv4
    • AF_INET6 – IPv6
    • AF_UNIX – Local socket (inter-process communication)
  • type – Defines the communication semantics.
    • SOCK_STREAM – TCP (connection-oriented)
    • SOCK_DGRAM – UDP (connectionless)
  • protocol – Protocol to be used.
    • 0 – Automatically selects the default protocol (e.g., TCP for SOCK_STREAM).
    • IPPROTO_TCP – TCP
    • IPPROTO_UDP – UDP

Return Value:

  • A file descriptor for the socket. On error, returns -1.

2. bind()

Assigns an address to a socket.


int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
  • sockfd – The socket file descriptor returned by socket().
  • addr – A pointer to sockaddr structure specifying the address to bind.

    struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; addr.sin_port = htons(8080);
  • addrlen – Size of the addr structure, typically sizeof(addr).

Return Value:

  • 0 on success, -1 on error.

3. listen()

Marks the socket as passive, waiting for connections.


int listen(int sockfd, int backlog);
  • sockfd – The bound socket descriptor.
  • backlog – Maximum number of pending connections in the queue.
    • Typical values: 5 or 10.

Return Value:

  • 0 on success, -1 on error.

4. accept()

Accepts incoming connection requests.


int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
  • sockfd – Listening socket descriptor.
  • addr – Pointer to a sockaddr structure to store the client address.
  • addrlen – Size of the addr structure.
    • On return, it contains the size of the client address.

Return Value:

  • A new socket descriptor for the connection.

5. connect()

Initiates a connection to a remote server.


int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
  • sockfd – The socket descriptor created by socket().
  • addr – Pointer to the sockaddr structure representing the server’s address.
  • addrlen – Size of the addr structure.

Return Value:

  • 0 on success, -1 on error.

6. send()

Sends data through the socket.


ssize_t send(int sockfd, const void *buf, size_t len, int flags);
  • sockfd – The socket descriptor.
  • buf – Pointer to the data to be sent.
  • len – Length of data in bytes.
  • flags – Modifies send behavior (usually 0).

Return Value:

  • Number of bytes sent, -1 on error.

7. recv()

Receives data from the socket.


ssize_t recv(int sockfd, void *buf, size_t len, int flags);
  • sockfd – The socket descriptor.
  • buf – Buffer to store received data.
  • len – Maximum size of the buffer.
  • flags – Modifies receive behavior (0 for default).

Return Value:

  • Number of bytes received, -1 on error.

8. close()

Closes the socket and releases resources.


int close(int sockfd);
  • sockfd – The socket file descriptor.

Return Value:

  • 0 on success, -1 on error.

9. setsockopt()

Configures socket options.


int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);
  • sockfd – The socket descriptor.
  • level – Protocol level (SOL_SOCKET for general socket options).
  • optname – Option to set (e.g., SO_REUSEADDR to reuse local addresses).
  • optval – Pointer to the value to set.
  • optlen – Size of the optval.

Return Value:

  • 0 on success, -1 on error.

10. getsockopt()

Retrieves socket options.


int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen);
  • sockfd – The socket descriptor.
  • level – Protocol level (SOL_SOCKET).
  • optname – Option to retrieve.
  • optval – Buffer to store option value.
  • optlen – Size of the buffer.

Return Value:

  • 0 on success, -1 on error.

Example Breakdown


sockfd = socket(AF_INET, SOCK_STREAM, 0);
  • AF_INET – IPv4
  • SOCK_STREAM – TCP (connection-oriented)
  • 0 – Default protocol (TCP for stream sockets)

bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
  • sockfd – Socket to bind.
  • addr – Address structure containing IP and port.
  • sizeof(addr) – Size of the address structure.

listen(sockfd, 5);
  • 5 – Queue length for pending connections.

newsock = accept(sockfd, (struct sockaddr *)&client, &len);
  • client – Fills with the client's address.
    • len – Length of the client structure.


Comments

Popular posts from this blog

Server/Client Communication-python

The basic mechanisms of client-server setup are: A client app send a request to a server app.  The server app returns a reply.  Some of the basic data communications between client and server are: File transfer - sends name and gets a file.  Web page - sends url and gets a page.  Echo - sends a message and gets it back.  Client server communication uses socket.              To connect to another machine, we need a socket connection. What's a connection?  A relationship between two machines, where two pieces of software know about each other. Those two pieces of software know how to communicate with each other. In other words, they know how to send bits to each other. A socket connection means the two machines have information about each other, including network location (IP address) and TCP port. (If we can use anology, IP address is the phone number and the TCP port is the extension).  A so...

Banker's Algorithm

Banker's algorithm is a deadlock avoidance algorithm. It is named so because this algorithm is used in banking systems to determine whether a loan can be granted or not. Consider there are n account holders in a bank and the sum of the money in all of their accounts is S. Everytime a loan has to be granted by the bank, it subtracts the loan amount from the total money the bank has. Then it checks if that difference is greater than S. It is done because, only then, the bank would have enough money even if all the n account holders draw all their money at once. Banker's algorithm works in a similar way in computers. Whenever a new process is created, it must exactly specify the maximum instances of each resource type that it needs. Let us assume that there are n processes and m resource types. Some data structures are used to implement the banker's algorithm. They are: Available: It is an array of length m . It represents the number of available resourc...

Inter Process Communication-Message Queue

Interprocess communication (IPC) is a set of programming interfaces that allow a programmer to coordinate activities among different program processes that can run concurrently in an operating system. This allows a program to handle many user requests at the same time. Since even a single user request may result in multiple processes running in the operating system on the user's behalf, the processes need to communicate with each other. The IPC interfaces make this possible. Each IPC method has its own advantages and limitations so it is not unusual for a single program to use all of the IPC methods . Message Based Communication Messages are a very general form of communication. Messages can be used to send and receive formatted data streams between arbitrary processes. Messages may have types. This helps in message interpretation. The type may specify appropriate permissions for processes. Usually at the receiver end, messages are put in a queue. Messages may also be fo...