linux socket编程代码
时间: 2023-11-21 16:41:35 浏览: 41
当然,我可以给你一个简单的 Linux Socket 编程的示例代码。以下是一个基于 TCP 协议的客户端和服务器端的示例:
客户端代码(client.c):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define SERVER_IP "127.0.0.1"
#define SERVER_PORT 12345
int main() {
int sockfd;
struct sockaddr_in server_addr;
char buffer[1024];
// 创建套接字
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
memset(&server_addr, 0, sizeof(server_addr));
// 设置服务器地址
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
if (inet_pton(AF_INET, SERVER_IP, &(server_addr.sin_addr)) <= 0) {
perror("Invalid address/ Address not supported");
exit(EXIT_FAILURE);
}
// 连接到服务器
if (connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("Connection failed");
exit(EXIT_FAILURE);
}
printf("Connected to the server.\n");
// 发送消息给服务器
strcpy(buffer, "Hello, server!");
if (write(sockfd, buffer, strlen(buffer)) < 0) {
perror("Write failed");
exit(EXIT_FAILURE);
}
printf("Message sent to the server: %s\n", buffer);
// 从服务器接收响应
memset(buffer, 0, sizeof(buffer));
if (read(sockfd, buffer, sizeof(buffer)) < 0) {
perror("Read failed");
exit(EXIT_FAILURE);
}
printf("Response from the server: %s\n", buffer);
// 关闭套接字
close(sockfd);
return 0;
}
```
服务器端代码(server.c):
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#define SERVER_PORT 12345
int main() {
int sockfd, new_sockfd;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_len;
char buffer[1024];
// 创建套接字
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
perror("Socket creation failed");
exit(EXIT_FAILURE);
}
memset(&server_addr, 0, sizeof(server_addr));
// 设置服务器地址
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_port = htons(SERVER_PORT);
// 绑定套接字到指定的 IP 地址和端口号
if (bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) {
perror("Binding failed");
exit(EXIT_FAILURE);
}
// 监听连接请求
if (listen(sockfd, 5) < 0) {
perror("Listening failed");
exit(EXIT_FAILURE);
}
printf("Server listening on port %d.\n", SERVER_PORT);
while (1) {
// 接收客户端连接
client_addr_len = sizeof(client_addr);
new_sockfd = accept(sockfd, (struct sockaddr*)&client_addr, &client_addr_len);
if (new_sockfd < 0) {
perror("Accepting failed");
exit(EXIT_FAILURE);
}
printf("Client connected.\n");
// 接收客户端发送的消息
memset(buffer, 0, sizeof(buffer));
if (read(new_sockfd, buffer, sizeof(buffer)) < 0) {
perror("Read failed");
exit(EXIT_FAILURE);
}
printf("Message from the client: %s\n", buffer);
// 发送响应给客户端
strcpy(buffer, "Hello, client!");
if (write(new_sockfd, buffer, strlen(buffer)) < 0) {
perror("Write failed");
exit(EXIT_FAILURE);
}
printf("Response sent to the client: %s\n", buffer);
// 关闭新套接字
close(new_sockfd);
printf("Client disconnected.\n");
}
// 关闭套接字
close(sockfd);
return 0;
}
```
请注意,这只是一个简单的示例代码,可能还需要进行错误处理和其他相关的细节处理。这个示例展示了一个最基本的 TCP 客户端和服务器端之间的通信过程。你可以根据你的需求进行修改和扩展。希望能对你有所帮助!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)