用c语言编写一个客户端和服务器端程序,客户端连接到服务器端后,给服务器发出读取一个文档的指令“read a.txt”,服务器读取a.txt,如果该文档存在,则返回结果并显示,如果该文件不存在,则返回“The file is not exist”
时间: 2024-02-22 18:00:41 浏览: 71
以下是用C语言编写的一个简单的客户端和服务器端程序,实现了客户端连接到服务器端后,给服务器发出读取一个文档的指令“read a.txt”,服务器读取a.txt,如果该文档存在,则返回结果并显示,如果该文件不存在,则返回“The file is not exist”:
客户端代码:client.c
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define SERVER_IP "127.0.0.1"
#define SERVER_PORT 8888
int main(int argc, char const *argv[])
{
int client_socket;
struct sockaddr_in server_address;
char message[1024] = {0};
char buffer[1024] = {0};
// 创建socket
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1)
{
perror("socket creation failed");
exit(EXIT_FAILURE);
}
// 设置服务器地址信息
server_address.sin_family = AF_INET;
server_address.sin_port = htons(SERVER_PORT);
if (inet_pton(AF_INET, SERVER_IP, &server_address.sin_addr) <= 0)
{
perror("invalid address");
exit(EXIT_FAILURE);
}
// 连接服务器
if (connect(client_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0)
{
perror("connection failed");
exit(EXIT_FAILURE);
}
// 发送指令
strcpy(message, "read a.txt");
if (send(client_socket, message, strlen(message), 0) < 0)
{
perror("send failed");
exit(EXIT_FAILURE);
}
// 接收结果
if (recv(client_socket, buffer, sizeof(buffer), 0) < 0)
{
perror("receive failed");
exit(EXIT_FAILURE);
}
// 显示结果
printf("%s\n", buffer);
// 关闭socket
close(client_socket);
return 0;
}
```
服务器端代码:server.c
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define SERVER_PORT 8888
int main(int argc, char const *argv[])
{
int server_socket, client_socket;
struct sockaddr_in server_address, client_address;
char buffer[1024] = {0};
FILE *fp;
// 创建socket
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1)
{
perror("socket creation failed");
exit(EXIT_FAILURE);
}
// 设置服务器地址信息
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;
server_address.sin_port = htons(SERVER_PORT);
// 绑定socket
if (bind(server_socket, (struct sockaddr *)&server_address, sizeof(server_address)) < 0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
// 监听socket
if (listen(server_socket, 3) < 0)
{
perror("listen failed");
exit(EXIT_FAILURE);
}
// 接收连接
printf("Waiting for client connection...\n");
if ((client_socket = accept(server_socket, (struct sockaddr *)&client_address, (socklen_t *)&client_address)) < 0)
{
perror("accept failed");
exit(EXIT_FAILURE);
}
printf("Client connected successfully!\n");
// 接收指令
if (recv(client_socket, buffer, sizeof(buffer), 0) < 0)
{
perror("receive failed");
exit(EXIT_FAILURE);
}
// 处理指令
if (strncmp(buffer, "read ", 5) == 0)
{
fp = fopen(buffer + 5, "r");
if (fp != NULL)
{
// 读取文件内容并发送
while (fgets(buffer, sizeof(buffer), fp) != NULL)
{
if (send(client_socket, buffer, strlen(buffer), 0) < 0)
{
perror("send failed");
exit(EXIT_FAILURE);
}
}
fclose(fp);
}
else
{
// 文件不存在
strcpy(buffer, "The file is not exist");
if (send(client_socket, buffer, strlen(buffer), 0) < 0)
{
perror("send failed");
exit(EXIT_FAILURE);
}
}
}
// 关闭socket
close(client_socket);
close(server_socket);
return 0;
}
```
编译和运行:
1. 分别保存上面的客户端代码和服务器端代码到 client.c 和 server.c 文件中;
2. 在终端中执行下面的命令来分别编译客户端和服务器端程序:
```
gcc -o client client.c
gcc -o server server.c
```
3. 在终端中先运行服务器端程序:
```
./server
```
4. 在终端中再运行客户端程序:
```
./client
```
5. 如果 a.txt 文件存在,则会显示该文件的内容;如果 a.txt 文件不存在,则会显示 “The file is not exist”。
阅读全文