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

CNC Lathe Programming Tutorial: Mastering the F60 Code
https://zeidei.com/technology/122622.html

Oven-Baked Goodness: A Nutritional Guide to Delicious & Healthy Recipes
https://zeidei.com/health-wellness/122621.html

The Ultimate Guide to Apparel Inventory Management: Streamlining Your Business for Success
https://zeidei.com/business/122620.html

What Does a Healthcare Registrar Do? A Comprehensive Guide to This Crucial Role
https://zeidei.com/health-wellness/122619.html

Raising Bilingual Children: A Comprehensive Guide for Dual-Language Families
https://zeidei.com/lifestyle/122618.html
Hot

A Beginner‘s Guide to Building an AI Model
https://zeidei.com/technology/1090.html

DIY Phone Case: A Step-by-Step Guide to Personalizing Your Device
https://zeidei.com/technology/1975.html

Android Development Video Tutorial
https://zeidei.com/technology/1116.html

Odoo Development Tutorial: A Comprehensive Guide for Beginners
https://zeidei.com/technology/2643.html

Database Development Tutorial: A Comprehensive Guide for Beginners
https://zeidei.com/technology/1001.html