c语言编写一个客户端和服务器端程序,客户端连接到服务器端后,给服务器发出读取一个文档的指令“read a.txt”,服务器读取a.txt,如果该文档存在,则返回结果并显示,如果该文件不存在,则返回“The file is not exist”。
时间: 2024-02-27 11:51:40 浏览: 61
以下是一个简单的 C 语言套接字客户端和服务器端程序,能够实现您的需求:
服务器端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define PORT 8888
#define BUFFER_SIZE 1024
int main(int argc, char *argv[]) {
int server_fd, client_fd;
struct sockaddr_in server_addr, client_addr;
socklen_t client_addr_len;
char buffer[BUFFER_SIZE];
int bytes_received;
FILE *fp;
// 创建套接字
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
// 设置服务器地址结构体
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_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("bind");
exit(EXIT_FAILURE);
}
// 监听连接请求
if (listen(server_fd, 5) == -1) {
perror("listen");
exit(EXIT_FAILURE);
}
printf("Server is listening on port %d...\n", PORT);
while (1) {
// 接受客户端连接
client_addr_len = sizeof(client_addr);
if ((client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_addr_len)) == -1) {
perror("accept");
continue;
}
// 接收客户端发送的指令
if ((bytes_received = recv(client_fd, buffer, BUFFER_SIZE, 0)) == -1) {
perror("recv");
close(client_fd);
continue;
}
// 处理读取文档指令
if (strncmp(buffer, "read ", 5) == 0) {
// 获取文件名
char *filename = buffer + 5;
// 打开文件
if ((fp = fopen(filename, "r")) == NULL) {
snprintf(buffer, BUFFER_SIZE, "The file \"%s\" is not exist.\n", filename);
send(client_fd, buffer, strlen(buffer), 0);
} else {
// 读取文件内容
fseek(fp, 0, SEEK_END);
int filesize = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *filecontent = (char *)malloc(filesize + 1);
fread(filecontent, 1, filesize, fp);
filecontent[filesize] = '\0';
// 发送文件内容
send(client_fd, filecontent, filesize, 0);
// 释放内存并关闭文件
free(filecontent);
fclose(fp);
}
}
// 关闭客户端套接字
close(client_fd);
}
// 关闭服务器套接字
close(server_fd);
return 0;
}
```
客户端代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#define SERVER_IP "127.0.0.1"
#define PORT 8888
#define BUFFER_SIZE 1024
int main(int argc, char *argv[]) {
int client_fd;
struct sockaddr_in server_addr;
char buffer[BUFFER_SIZE];
int bytes_received;
// 创建套接字
if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
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(SERVER_IP);
server_addr.sin_port = htons(PORT);
// 连接服务器
if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
perror("connect");
exit(EXIT_FAILURE);
}
// 发送读取文档指令
snprintf(buffer, BUFFER_SIZE, "read a.txt");
if (send(client_fd, buffer, strlen(buffer), 0) == -1) {
perror("send");
exit(EXIT_FAILURE);
}
// 接收服务器返回的结果
bytes_received = recv(client_fd, buffer, BUFFER_SIZE, 0);
if (bytes_received == -1) {
perror("recv");
exit(EXIT_FAILURE);
} else {
buffer[bytes_received] = '\0';
printf("%s", buffer);
}
// 关闭套接字
close(client_fd);
return 0;
}
```
请注意,这只是一个简单的示例程序,实际使用时需要做更多的错误处理和安全措施。同时,文件名输入不应该直接从客户端传入,而应该进行一些安全检查和过滤。
阅读全文