在linux系统中用c编写一个客户端和服务器端程序,客户端连接到服务器端后,请求一个文档,然后显示结果
时间: 2024-02-11 19:04:26 浏览: 76
好的,这是一个典型的客户端/服务器模型。我们可以使用Linux系统提供的socket API来实现这个程序。首先,我们需要编写服务器端程序。代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define PORT 8000
#define BUFFER_SIZE 1024
int main() {
int server_socket, client_socket;
struct sockaddr_in server_address, client_address;
socklen_t client_address_len = sizeof(client_address);
char buffer[BUFFER_SIZE];
// 创建socket对象
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("Error: Failed to create socket");
exit(1);
}
// 设置服务器地址
bzero(&server_address, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(PORT);
// 绑定IP地址和端口号
if (bind(server_socket, (struct sockaddr *)&server_address, sizeof(server_address)) == -1) {
perror("Error: Failed to bind socket");
exit(1);
}
// 监听请求
if (listen(server_socket, 1) == -1) {
perror("Error: Failed to listen to socket");
exit(1);
}
printf("Server is running...\n");
while (1) {
// 接受客户端请求
client_socket = accept(server_socket, (struct sockaddr *)&client_address, &client_address_len);
if (client_socket == -1) {
perror("Error: Failed to accept connection");
continue;
}
printf("Client connected: %s:%d\n", inet_ntoa(client_address.sin_addr), ntohs(client_address.sin_port));
// 接受客户端发送的请求
bzero(buffer, BUFFER_SIZE);
if (recv(client_socket, buffer, BUFFER_SIZE, 0) == -1) {
perror("Error: Failed to receive request");
close(client_socket);
continue;
}
printf("Client request: %s\n", buffer);
// 打开并读取文件内容
FILE *fp = fopen(buffer, "r");
if (fp == NULL) {
perror("Error: Failed to open file");
close(client_socket);
continue;
}
bzero(buffer, BUFFER_SIZE);
while (fgets(buffer, BUFFER_SIZE, fp) != NULL) {
// 发送文件内容到客户端
send(client_socket, buffer, strlen(buffer), 0);
bzero(buffer, BUFFER_SIZE);
}
// 关闭文件
fclose(fp);
// 关闭socket连接
close(client_socket);
}
// 关闭socket连接
close(server_socket);
return 0;
}
```
在这个程序中,我们首先创建一个socket对象,然后绑定IP地址和端口号,并开始监听请求。当有客户端连接时,我们接受客户端请求,读取请求文件的内容,并将其发送回客户端。最后,我们关闭socket连接。
接下来,我们编写客户端程序。代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SERVER_IP "127.0.0.1"
#define PORT 8000
#define BUFFER_SIZE 1024
int main() {
int client_socket;
struct sockaddr_in server_address;
char buffer[BUFFER_SIZE];
// 创建socket对象
client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
perror("Error: Failed to create socket");
exit(1);
}
// 设置服务器地址
bzero(&server_address, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr(SERVER_IP);
server_address.sin_port = htons(PORT);
// 连接服务器
if (connect(client_socket, (struct sockaddr *)&server_address, sizeof(server_address)) == -1) {
perror("Error: Failed to connect to server");
exit(1);
}
// 请求文件名称
char *request = "test.txt";
// 发送请求到服务器
if (send(client_socket, request, strlen(request), 0) == -1) {
perror("Error: Failed to send request");
exit(1);
}
// 接受服务器响应
bzero(buffer, BUFFER_SIZE);
while (recv(client_socket, buffer, BUFFER_SIZE, 0) > 0) {
printf("%s", buffer);
bzero(buffer, BUFFER_SIZE);
}
// 关闭socket连接
close(client_socket);
return 0;
}
```
在这个程序中,我们首先创建一个socket对象,并连接到服务器。然后,我们发送请求文件的名称到服务器,并等待服务器发送响应。最后,我们显示响应结果,并关闭socket连接。
将这两个程序保存到两个不同的文件中,然后使用gcc编译它们。在命令行中分别执行它们。如果一切正常,你应该能够看到客户端接收到了服务器发送的文件内容。
阅读全文