c语言编写一个客户端和服务器端程序,客户端连接到服务器端后,请求一个文档,然后显示结果
时间: 2024-02-10 08:10:52 浏览: 78
ssl.zip_c ssl_c语言 openssl_linux openssl_site:www.pudn.com
好的,这是一个比较简单的网络编程问题,以下是一个示例代码:
服务器端程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <fcntl.h>
#define PORT 8001
#define BUFFER_SIZE 1024
int main() {
int server_socket, client_socket;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_len;
char buffer[BUFFER_SIZE];
ssize_t len;
// 创建一个socket对象
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
// 绑定IP地址和端口号
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("bind");
exit(EXIT_FAILURE);
}
// 监听客户端连接
if (listen(server_socket, 1) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
printf("服务器已启动,等待客户端连接...\n");
while (1) {
// 接受客户端连接请求
client_socket = accept(server_socket, (struct sockaddr *)&client_addr, &client_addr_len);
if (client_socket < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
printf("客户端已连接,客户端地址:%s\n", inet_ntoa(client_addr.sin_addr));
// 接收客户端发送的数据
memset(buffer, 0, BUFFER_SIZE);
len = recv(client_socket, buffer, BUFFER_SIZE, 0);
if (len <= 0) {
close(client_socket);
continue;
}
if (strcmp(buffer, "get_document") == 0) {
// 打开文件并读取数据
int fd = open("document.txt", O_RDONLY);
if (fd < 0) {
perror("open");
close(client_socket);
continue;
}
memset(buffer, 0, BUFFER_SIZE);
while ((len = read(fd, buffer, BUFFER_SIZE)) > 0) {
// 发送数据给客户端
if (send(client_socket, buffer, len, 0) < 0) {
perror("send");
break;
}
memset(buffer, 0, BUFFER_SIZE);
}
close(fd);
}
// 关闭客户端连接
close(client_socket);
}
// 关闭服务器端连接
close(server_socket);
return 0;
}
```
客户端程序:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 8001
#define BUFFER_SIZE 1024
int main() {
int client_socket;
struct sockaddr_in server_addr;
char buffer[BUFFER_SIZE];
ssize_t len;
// 创建一个socket对象
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket < 0) {
perror("socket");
exit(EXIT_FAILURE);
}
// 连接服务器
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_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
perror("connect");
exit(EXIT_FAILURE);
}
// 发送请求
memset(buffer, 0, BUFFER_SIZE);
strcpy(buffer, "get_document");
if (send(client_socket, buffer, strlen(buffer), 0) < 0) {
perror("send");
exit(EXIT_FAILURE);
}
// 接收数据
memset(buffer, 0, BUFFER_SIZE);
while ((len = recv(client_socket, buffer, BUFFER_SIZE, 0)) > 0) {
printf("%s", buffer);
memset(buffer, 0, BUFFER_SIZE);
}
// 关闭连接
close(client_socket);
return 0;
}
```
以上代码实现了一个简单的客户端和服务器端程序,客户端连接到服务器端后,请求一个文档,然后显示结果。在这个例子中,服务器端读取本地文件 "document.txt" 的内容,并发送给客户端。客户端连接服务器并发送 "get_document" 请求,然后接收服务器端发送的文档内容并打印出来。
阅读全文