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.
domain– Specifies the protocol family (addressing type).AF_INET– IPv4AF_INET6– IPv6AF_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 forSOCK_STREAM).IPPROTO_TCP– TCPIPPROTO_UDP– UDP
Return Value:
- A file descriptor for the socket. On error, returns
-1.
2. bind()
Assigns an address to a socket.
sockfd– The socket file descriptor returned bysocket().addr– A pointer tosockaddrstructure specifying the address to bind.addrlen– Size of theaddrstructure, typicallysizeof(addr).
Return Value:
0on success,-1on error.
3. listen()
Marks the socket as passive, waiting for connections.
sockfd– The bound socket descriptor.backlog– Maximum number of pending connections in the queue.- Typical values:
5or10.
- Typical values:
Return Value:
0on success,-1on error.
4. accept()
Accepts incoming connection requests.
sockfd– Listening socket descriptor.addr– Pointer to asockaddrstructure to store the client address.addrlen– Size of theaddrstructure.- 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.
sockfd– The socket descriptor created bysocket().addr– Pointer to thesockaddrstructure representing the server’s address.addrlen– Size of theaddrstructure.
Return Value:
0on success,-1on error.
6. send()
Sends data through the socket.
sockfd– The socket descriptor.buf– Pointer to the data to be sent.len– Length of data in bytes.flags– Modifies send behavior (usually0).
Return Value:
- Number of bytes sent,
-1on error.
7. recv()
Receives data from the socket.
sockfd– The socket descriptor.buf– Buffer to store received data.len– Maximum size of the buffer.flags– Modifies receive behavior (0for default).
Return Value:
- Number of bytes received,
-1on error.
8. close()
Closes the socket and releases resources.
sockfd– The socket file descriptor.
Return Value:
0on success,-1on error.
9. setsockopt()
Configures socket options.
sockfd– The socket descriptor.level– Protocol level (SOL_SOCKETfor general socket options).optname– Option to set (e.g.,SO_REUSEADDRto reuse local addresses).optval– Pointer to the value to set.optlen– Size of theoptval.
Return Value:
0on success,-1on error.
10. getsockopt()
Retrieves socket options.
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:
0on success,-1on error.
Example Breakdown
AF_INET– IPv4SOCK_STREAM– TCP (connection-oriented)0– Default protocol (TCP for stream sockets)
sockfd– Socket to bind.addr– Address structure containing IP and port.sizeof(addr)– Size of the address structure.
5– Queue length for pending connections.
client– Fills with the client's address.len– Length of the client structure.

Comments
Post a Comment