C Language Socket Programming Tutorial166


Socket programming is a fundamental part of computer networking. It allows applications to communicate with each other over a network, such as the Internet. In this tutorial, we will explore the basics of socket programming in the C language.

What is a Socket?

A socket is an endpoint of a bidirectional communication channel. It is used to send and receive data over a network. Sockets are created using the `socket` system call. The `socket` function takes three parameters:* `domain`: The address family of the socket. The most common address families are `AF_INET` (IPv4) and `AF_INET6` (IPv6).
* `type`: The type of socket. The most common socket types are `SOCK_STREAM` (TCP) and `SOCK_DGRAM` (UDP).
* `protocol`: The protocol to use for communication. The most common protocols are `IPPROTO_TCP` (TCP) and `IPPROTO_UDP` (UDP).

Creating a Socket

The following code shows how to create a socket:```c
#include
int main() {
int sockfd;
// Create a socket
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// Check for errors
if (sockfd == -1) {
perror("socket");
return EXIT_FAILURE;
}
// ...
// Close the socket
close(sockfd);
return EXIT_SUCCESS;
}
```

Binding a Socket to an Address

Once a socket has been created, it must be bound to an address. This is done using the `bind` system call. The `bind` function takes three parameters:* `sockfd`: The socket to bind.
* `addr`: The address to bind the socket to.
* `addrlen`: The length of the address structure.

The following code shows how to bind a socket to an address:```c
#include
#include
int main() {
int sockfd;
struct sockaddr_in addr;
// Create a socket
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// Check for errors
if (sockfd == -1) {
perror("socket");
return EXIT_FAILURE;
}
// Fill in the address structure
addr.sin_family = AF_INET;
addr.sin_port = htons(8080);
addr.sin_addr.s_addr = INADDR_ANY;
// Bind the socket to the address
if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
perror("bind");
return EXIT_FAILURE;
}
// ...
// Close the socket
close(sockfd);
return EXIT_SUCCESS;
}
```

Listening for Connections

Once a socket has been bound to an address, it can start listening for connections. This is done using the `listen` system call. The `listen` function takes two parameters:* `sockfd`: The socket to listen on.
* `backlog`: The maximum number of pending connections.

The following code shows how to listen for connections:```c
#include
int main() {
int sockfd;
struct sockaddr_in addr;
// Create a socket
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// Check for errors
if (sockfd == -1) {
perror("socket");
return EXIT_FAILURE;
}
// Fill in the address structure
addr.sin_family = AF_INET;
addr.sin_port = htons(8080);
addr.sin_addr.s_addr = INADDR_ANY;
// Bind the socket to the address
if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
perror("bind");
return EXIT_FAILURE;
}
// Listen for connections
if (listen(sockfd, 5) == -1) {
perror("listen");
return EXIT_FAILURE;
}
// ...
// Close the socket
close(sockfd);
return EXIT_SUCCESS;
}
```

Accepting a Connection

When a client connects to the server, the server will accept the connection using the `accept` system call. The `accept` function takes three parameters:* `sockfd`: The socket to accept the connection on.
* `addr`: The address of the client.
* `addrlen`: The length of the address structure.

The `accept` function returns a new socket that is used to communicate with the client.

The following code shows how to accept a connection:```c
#include
int main() {
int sockfd, newsockfd;
struct sockaddr_in addr;
// Create a socket
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// Check for errors
if (sockfd == -1) {
perror("socket");
return EXIT_FAILURE;
}
// Fill in the address structure
addr.sin_family = AF_INET;
addr.sin_port = htons(8080);
addr.sin_addr.s_addr = INADDR_ANY;
// Bind the socket to the address
if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
perror("bind");
return EXIT_FAILURE;
}
// Listen for connections
if (listen(sockfd, 5) == -1) {
perror("listen");
return EXIT_FAILURE;
}
// Accept a connection
newsockfd = accept(sockfd, (struct sockaddr *)&addr, &addrlen);
// Check for errors
if (newsockfd == -1) {
perror("accept");
return EXIT_FAILURE;
}
// ...
// Close the sockets
close(sockfd);
close(newsockfd);
return EXIT_SUCCESS;
}
```

Sending and Receiving Data

Once a connection has been established, the server and client can send and receive data using the `send` and `recv` system calls. The `send` function takes three parameters:* `sockfd`: The socket to send data on.
* `buf`: The buffer containing the data to send.
* `len`: The length of the data to send.

The `recv` function takes three parameters:* `sockfd`: The socket to receive data on.
* `buf`: The buffer to receive the data into.
* `len`: The length of the data to receive.

The `send` and `recv` functions return the number of bytes sent or received.

The following code shows how to send and receive data:```c
#include
int main() {
int sockfd, newsockfd;
struct sockaddr_in addr;
char buf[1024];
// Create a socket
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
// Check for errors
if (sockfd == -1) {
perror("socket");
return EXIT_FAILURE;
}
// Fill in the address structure
addr.sin_family = AF_INET;
addr.sin_port = htons(8080);
addr.sin_addr.s_addr = INADDR_ANY;
// Bind the socket to the address
if (bind(sockfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
perror("bind");
return EXIT_FAILURE;
}
// Listen for connections
if (listen(sockfd, 5) == -1) {
perror("listen");
return EXIT_FAILURE;
}
// Accept a connection
newsockfd = accept(sockfd, (struct sockaddr *)&addr, &addrlen);
// Check for errors
if (newsockfd == -1) {
perror("accept");
return EXIT_FAILURE;
}
// Send data
if (send(newsockfd, "Hello, world!", strlen("Hello, world!"), 0) == -1) {
perror("send");
return EXIT_FAILURE;
}
// Receive data
if (recv(newsockfd, buf, sizeof(buf), 0) == -1) {
perror("recv");
return EXIT_FAILURE;
}
// Print

2025-01-01


Previous:How to Print Data in Python: A Comprehensive Guide

Next:Panda Database Tutorial: A Comprehensive Guide for Beginners