C语言实现Linux网络编程
时间: 2023-07-23 21:23:05 浏览: 66
C语言是一种常用的编程语言,也是Linux网络编程中最常用的编程语言之一。在C语言中,可以使用Socket API进行网络编程。以下是一个简单的C语言程序,演示了如何使用Socket API实现一个简单的客户端/服务器模型:
服务器端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#define PORT 8888
#define MAXLINE 1024
int main() {
int server_fd, conn_fd;
struct sockaddr_in server_addr, client_addr;
char buffer[MAXLINE];
int n;
// 创建socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
// 初始化服务器地址
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(PORT);
// 绑定socket和地址
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("bind");
exit(1);
}
// 启动监听
if (listen(server_fd, 5) == -1) {
perror("listen");
exit(1);
}
printf("Server is listening on port %d...\n", PORT);
// 接收客户端连接
socklen_t client_len = sizeof(client_addr);
if ((conn_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_len)) == -1) {
perror("accept");
exit(1);
}
printf("Client connected.\n");
// 接收客户端消息并回传
while (1) {
memset(buffer, 0, sizeof(buffer));
if ((n = read(conn_fd, buffer, MAXLINE)) == -1) {
perror("read");
exit(1);
} else if (n == 0) {
printf("Client disconnected.\n");
break;
}
printf("Received message from client: %s", buffer);
if (write(conn_fd, buffer, n) == -1) {
perror("write");
exit(1);
}
}
close(conn_fd);
close(server_fd);
return 0;
}
```
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 8888
#define MAXLINE 1024
int main() {
int client_fd;
struct sockaddr_in server_addr;
char buffer[MAXLINE];
int n;
// 创建socket
if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
// 初始化服务器地址
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
server_addr.sin_port = htons(PORT);
// 连接服务器
if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("connect");
exit(1);
}
printf("Connected to server.\n");
// 发送消息给服务器
while (1) {
printf("Input message to send: ");
fgets(buffer, MAXLINE, stdin);
if (write(client_fd, buffer, strlen(buffer)) == -1) {
perror("write");
exit(1);
}
memset(buffer, 0, sizeof(buffer));
if ((n = read(client_fd, buffer, MAXLINE)) == -1) {
perror("read");
exit(1);
} else if (n == 0) {
printf("Server disconnected.\n");
break;
}
printf("Received message from server: %s", buffer);
}
close(client_fd);
return 0;
}
```
以上程序演示了一个简单的客户端/服务器模型,服务器可以接收客户端发送的消息并回传,客户端可以向服务器发送消息并接收回传。这只是一个简单的示例程序,Linux网络编程还涉及到很多高级的知识,需要深入学习和实践。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)