C++ 实现简单文件服务器 - put 和 get 功能
使用 C++ 实现简单文件服务器 - put 和 get 功能
本代码使用 C++ 语言实现了一个简单的文件服务器,支持 put (上传文件) 和 get (下载文件) 功能,客户端和服务器可以不在同一台机器上。
服务器代码
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define PORT 8888
#define BUFFER_SIZE 4096
using namespace std;
int main() {
int listenfd, connfd;
struct sockaddr_in servaddr, cliaddr;
socklen_t clilen;
char buffer[BUFFER_SIZE];
int n;
// 创建 socket
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd == -1) {
perror("Error creating socket");
exit(1);
}
// 绑定 socket 到端口
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
if (bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1) {
perror("Error binding socket");
exit(1);
}
// 监听传入连接
if (listen(listenfd, 10) == -1) {
perror("Error listening");
exit(1);
}
// 接受传入连接
while (1) {
clilen = sizeof(cliaddr);
connfd = accept(listenfd, (struct sockaddr*)&cliaddr, &clilen);
if (connfd == -1) {
perror("Error accepting connection");
continue;
}
// 接收来自客户端的命令
n = recv(connfd, buffer, BUFFER_SIZE, 0);
if (n == -1) {
perror("Error receiving command");
close(connfd);
continue;
} else if (n == 0) {
close(connfd);
continue;
}
buffer[n] = '\0';
// 解析命令
string command = buffer;
size_t pos = command.find(' ');
if (pos == string::npos) {
perror("Error parsing command");
close(connfd);
continue;
}
string action = command.substr(0, pos);
string filename = command.substr(pos+1);
// 处理 put 命令
if (action == "put") {
// 打开文件以写入
int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (fd == -1) {
perror("Error opening file for writing");
close(connfd);
continue;
}
// 接收来自客户端的文件数据
while ((n = recv(connfd, buffer, BUFFER_SIZE, 0)) > 0) {
write(fd, buffer, n);
}
if (n == -1) {
perror("Error receiving file data");
close(connfd);
continue;
}
// 关闭文件和连接
close(fd);
close(connfd);
}
// 处理 get 命令
else if (action == "get") {
// 打开文件以读取
int fd = open(filename.c_str(), O_RDONLY);
if (fd == -1) {
perror("Error opening file for reading");
close(connfd);
continue;
}
// 发送文件数据到客户端
while ((n = read(fd, buffer, BUFFER_SIZE)) > 0) {
send(connfd, buffer, n, 0);
}
if (n == -1) {
perror("Error sending file data");
close(connfd);
continue;
}
// 关闭文件和连接
close(fd);
close(connfd);
}
// 无效命令
else {
perror("Invalid command");
close(connfd);
continue;
}
}
return 0;
}
客户端代码
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define BUFFER_SIZE 4096
using namespace std;
int main(int argc, char** argv) {
if (argc < 2) {
cout << "Usage: " << argv[0] << " <command> [<options>]";
cout << "Commands:";
cout << " put [-h <hostname>] [-p <portname>] <local_filename> <remote_filename>";
cout << " get [-h <hostname>] [-p <portname>] <remote_filename> <local_filename>";
return 0;
}
// 解析命令行参数
string command = argv[1];
string hostname = "localhost";
string portname = "8888";
string local_filename = "";
string remote_filename = "";
for (int i = 2; i < argc; i++) {
string arg = argv[i];
if (arg == "-h" && i+1 < argc) {
hostname = argv[++i];
} else if (arg == "-p" && i+1 < argc) {
portname = argv[++i];
} else if (local_filename == "") {
local_filename = arg;
} else if (remote_filename == "") {
remote_filename = arg;
}
}
// 将主机名解析为 IP 地址
struct hostent* host = gethostbyname(hostname.c_str());
if (host == NULL) {
perror("Error resolving hostname");
exit(1);
}
char* ip = inet_ntoa(*(struct in_addr*)host->h_addr);
// 创建 socket
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
perror("Error creating socket");
exit(1);
}
// 连接到服务器
struct sockaddr_in servaddr;
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr(ip);
servaddr.sin_port = htons(atoi(portname.c_str()));
if (connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1) {
perror("Error connecting to server");
exit(1);
}
// 发送命令到服务器
string message = command + " " + remote_filename;
if (send(sockfd, message.c_str(), message.length(), 0) == -1) {
perror("Error sending command to server");
close(sockfd);
exit(1);
}
// 处理 put 命令
if (command == "put") {
// 打开文件以读取
int fd = open(local_filename.c_str(), O_RDONLY);
if (fd == -1) {
perror("Error opening local file for reading");
close(sockfd);
exit(1);
}
// 发送文件数据到服务器
char buffer[BUFFER_SIZE];
int n;
while ((n = read(fd, buffer, BUFFER_SIZE)) > 0) {
send(sockfd, buffer, n, 0);
}
if (n == -1) {
perror("Error sending file data to server");
close(sockfd);
exit(1);
}
// 关闭文件和连接
close(fd);
close(sockfd);
}
// 处理 get 命令
else if (command == "get") {
// 打开文件以写入
int fd = open(local_filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (fd == -1) {
perror("Error opening local file for writing");
close(sockfd);
exit(1);
}
// 接收来自服务器的文件数据
char buffer[BUFFER_SIZE];
int n;
while ((n = recv(sockfd, buffer, BUFFER_SIZE, 0)) > 0) {
write(fd, buffer, n);
}
if (n == -1) {
perror("Error receiving file data from server");
close(sockfd);
exit(1);
}
// 关闭文件和连接
close(fd);
close(sockfd);
}
return 0;
}
运行
服务器:
- 编译服务器代码:
g++ server.cpp -o server - 运行服务器:
./server
客户端:
- 编译客户端代码:
g++ client.cpp -o client - 上传文件:
./client put -h <服务器主机名> -p <端口号> <本地文件名> <远程文件名> - 下载文件:
./client get -h <服务器主机名> -p <端口号> <远程文件名> <本地文件名>
例如,将本地文件 local_file.txt 上传到服务器,并在服务器上保存为 remote_file.txt,可以使用以下命令:
./client put -h localhost -p 8888 local_file.txt remote_file.txt
说明
- 服务器监听端口 8888,客户端默认连接到
localhost:8888,可以通过命令行参数-h和-p修改服务器地址和端口。 - 服务器和客户端都使用了
socket进行网络通信。 - 服务器代码支持
put和get命令,分别用于上传和下载文件。 - 客户端代码支持解析命令行参数,并根据命令执行相应的操作。
注意事项
- 代码中使用了
perror()函数打印错误信息,建议在实际使用中进行更详细的错误处理。 - 代码仅供参考,建议在实际使用中进行完善和改进。
原文地址: https://www.cveoy.top/t/topic/oqpR 著作权归作者所有。请勿转载和采集!