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

CSL 332 Networking Lab KTU 2019 Scheme - Dr Binu V P

CSL 332 Networking Lab KTU BTech 2019 Scheme About Me Scheme Syllabus Experiments 1.Learn the Networking Commands and Network Configuration Files     Basic networking commands     More Networking commands     Network configuration Files     View the configuration and address of your network interface     Network Connectivity      View Active TCP connections     MAC address of another machine using ARP  2.  System calls in Network Programming 3.  Simple TCP/IP Client Server Program 4.  Simple UDP Client Server Program 5.Application Programs     Concurrent UDP Time Server     Checking Prime Numbers 6. Simulate ARQ Protocols  / sliding window protocols          Stop and Wait           Go-Back-N          Selective Repeat  7. Routing Protocols - Distance Vector and Link State   ...

Stop and Wait ARQ

Here's a simple C program that demonstrates the Stop-and-Wait ARQ protocol. This basic implementation simulates the sender transmitting packets one at a time and waiting for an acknowledgment from the receiver. If the acknowledgment is not received, the sender retransmits the packet. Key Points: The sender sends one packet at a time. If the receiver acknowledges it (ACK), the sender sends the next packet. If the acknowledgment is lost, the sender retransmits after a timeout. C Program: Stop-and-Wait ARQ Simulation #include <stdio.h> #include <stdlib.h> #include <time.h> #include <unistd.h>  // for sleep() #define TIMEOUT 3  // Timeout duration in seconds #define TOTAL_PACKETS 5  // Number of packets to send int simulate_acknowledgment() {     // Simulate a 70% chance of successful acknowledgment     return rand() % 10 < 7; } int main() {     srand(time(0));  // Seed for random number generation     i...

Go-Back-N ARQ

Here's a simple Go-Back-N ARQ implementation using C socket programming to simulate communication between a client (sender) and a server (receiver). Overview: Go-Back-N ARQ allows the sender to send multiple packets (window size) without waiting for individual ACKs. If a packet is lost or an ACK is not received, all packets starting from the lost packet are retransmitted. The server randomly simulates ACK loss or successful reception. Program Structure: Server : Simulates ACK reception with a chance of ACK loss, acknowledging packets up to the first lost packet. Client : Sends packets in a sliding window fashion, retransmitting the entire window if an ACK is lost. Server - Receiver #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <time.h> #define PORT 8080 #define BUFFER_SIZE 1024 #define LOSS_PROBABILITY 30  // 30% chance of ACK loss int main() {     int server_fd, new_soc...