用c++进行套接字编程,实现如下功能:实现一个FTP协议的客户端和服务端,完成基本的文件传输功能。Windows平台下只能使用Winsock(winsock.h),请勿使用其它高层封装的Socket库。
时间: 2024-04-08 22:34:42 浏览: 105
好的,我理解你的需求。下面是使用C++编写基于FTP协议的客户端和服务端的示例代码。
首先是FTP服务器端的代码:
```cpp
#include <iostream>
#include <string>
#include <fstream>
#include <winsock.h>
#pragma comment(lib, "ws2_32.lib")
#define MAX_BUFFER_SIZE 1024
#define PORT 21
void SendFile(SOCKET clientSocket, const std::string& filename) {
std::ifstream file(filename, std::ios::binary);
if (!file) {
std::cout << "Error opening file: " << filename << std::endl;
return;
}
char buffer[MAX_BUFFER_SIZE];
memset(buffer, 0, sizeof(buffer));
while (!file.eof()) {
file.read(buffer, sizeof(buffer));
int bytesRead = file.gcount();
send(clientSocket, buffer, bytesRead, 0);
memset(buffer, 0, sizeof(buffer));
}
file.close();
}
int main() {
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
std::cout << "Failed to initialize winsock!" << std::endl;
return -1;
}
SOCKET listenSocket = socket(AF_INET, SOCK_STREAM, 0);
if (listenSocket == INVALID_SOCKET) {
std::cout << "Failed to create socket!" << std::endl;
return -1;
}
sockaddr_in serverAddress{};
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr = INADDR_ANY;
serverAddress.sin_port = htons(PORT);
if (bind(listenSocket, (sockaddr*)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR) {
std::cout << "Failed to bind socket!" << std::endl;
closesocket(listenSocket);
return -1;
}
if (listen(listenSocket, SOMAXCONN) == SOCKET_ERROR) {
std::cout << "Error listening on socket!" << std::endl;
closesocket(listenSocket);
return -1;
}
std::cout << "FTP Server started and listening on port " << PORT << std::endl;
while (true) {
sockaddr_in clientAddress{};
int clientAddressSize = sizeof(clientAddress);
SOCKET clientSocket = accept(listenSocket, (sockaddr*)&clientAddress, &clientAddressSize);
if (clientSocket == INVALID_SOCKET) {
std::cout << "Error accepting client connection!" << std::endl;
closesocket(listenSocket);
WSACleanup();
return -1;
}
char clientIP[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(clientAddress.sin_addr), clientIP, INET_ADDRSTRLEN);
std::cout << "Accepted connection from " << clientIP << ":" << ntohs(clientAddress.sin_port) << std::endl;
char buffer[MAX_BUFFER_SIZE];
memset(buffer, 0, sizeof(buffer));
recv(clientSocket, buffer, sizeof(buffer), 0);
if (strcmp(buffer, "get") == 0) {
std::string filename;
recv(clientSocket, buffer, sizeof(buffer), 0);
filename = buffer;
SendFile(clientSocket, filename);
}
closesocket(clientSocket);
}
closesocket(listenSocket);
WSACleanup();
return 0;
}
```
接下来是FTP客户端的代码:
```cpp
#include <iostream>
#include <string>
#include <fstream>
#include <winsock.h>
#pragma comment(lib, "ws2_32.lib")
#define MAX_BUFFER_SIZE 1024
#define SERVER_IP "127.0.0.1"
#define PORT 21
void ReceiveFile(SOCKET clientSocket, const std::string& filename) {
std::ofstream file(filename, std::ios::binary);
if (!file) {
std::cout << "Error creating file: " << filename << std::endl;
return;
}
char buffer[MAX_BUFFER_SIZE];
memset(buffer, 0, sizeof(buffer));
int bytesRead;
while ((bytesRead = recv(clientSocket, buffer, sizeof(buffer), 0)) > 0) {
file.write(buffer, bytesRead);
memset(buffer, 0, sizeof(buffer));
}
file.close();
}
int main() {
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
std::cout << "Failed to initialize winsock!" << std::endl;
return -1;
}
SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (clientSocket == INVALID_SOCKET) {
std::cout << "Failed to create socket!" << std::endl;
WSACleanup();
return -1;
}
sockaddr_in serverAddress{};
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(PORT);
inet_pton(AF_INET, SERVER_IP, &(serverAddress.sin_addr));
if (connect(clientSocket, (sockaddr*)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR) {
std::cout << "Failed to connect to server!" << std::endl;
closesocket(clientSocket);
WSACleanup();
return -1;
}
std::cout << "Connected to FTP server at " << SERVER_IP << ":" << PORT << std::endl;
std::string command;
std::cout << "Enter command (get <filename>): ";
std::getline(std::cin, command);
send(clientSocket, command.c_str(), command.size(), 0);
if (command.substr(0, 3) == "get") {
std::string filename = command.substr(4);
ReceiveFile(clientSocket, filename);
std::cout << "File " << filename << " received from server." << std::endl;
}
closesocket(clientSocket);
WSACleanup();
return 0;
}
```
请注意,这只是一个简单的示例代码,没有进行错误处理和安全性检查。在实际使用中,需要进一步完善代码以满足实际需求。
希望对你有所帮助!如果有任何其他问题,请随时问我。
阅读全文