#include #include #include #include <WinSock2.h> #include <WS2tcpip.h> #pragma comment(lib, "ws2_32.lib")

#define BUFFER_SIZE 2048

using namespace std;

int sendCommand(SOCKET clientSocket, const string& command) { if (send(clientSocket, command.c_str(), command.length(), 0) == SOCKET_ERROR) { cerr << "Failed to send command" << endl; return 1; } return 0; }

int sendFile(SOCKET clientSocket, const string& fileName) { // Open file ifstream file(fileName, ios::binary); if (!file) { cerr << "Failed to open file" << endl; return 1; }

// Send file name
if (send(clientSocket, fileName.c_str(), fileName.length(), 0) == SOCKET_ERROR) {
    cerr << "Failed to send file name" << endl;
    file.close();
    return 1;
}

// Read and send file data
char buffer[BUFFER_SIZE];
while (!file.eof()) {
    file.read(buffer, BUFFER_SIZE);
    int bytesRead = file.gcount();
    if (send(clientSocket, buffer, bytesRead, 0) == SOCKET_ERROR) {
        cerr << "Failed to send file data" << endl;
        file.close();
        return 1;
    }
}

// Close file
file.close();

cout << "File uploaded successfully" << endl;

return 0;

}

int downloadFile(SOCKET clientSocket, const string& fileName) { // Send file name if (send(clientSocket, fileName.c_str(), fileName.length(), 0) == SOCKET_ERROR) { cerr << "Failed to send file name" << endl; return 1; }

// Receive file data
ofstream file(fileName, ios::binary);
if (!file) {
    cerr << "Failed to create file" << endl;
    return 1;
}

char buffer[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = recv(clientSocket, buffer, BUFFER_SIZE, 0)) > 0) {
    file.write(buffer, bytesRead);
}

// Close file
file.close();

if (bytesRead == SOCKET_ERROR) {
    cerr << "Failed to receive file data" << endl;
    return 1;
}

cout << "File downloaded successfully" << endl;

return 0;

}

int listFiles(SOCKET clientSocket) { char buffer[BUFFER_SIZE]; int bytesRead; while ((bytesRead = recv(clientSocket, buffer, BUFFER_SIZE, 0)) > 0) { cout.write(buffer, bytesRead); }

if (bytesRead == SOCKET_ERROR) {
    cerr << "Failed to receive file list" << endl;
    return 1;
}

return 0;

}

int main() { // Initialize Winsock WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { cerr << "Failed to initialize winsock" << endl; return 1; }

// Create socket
SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (clientSocket == INVALID_SOCKET) {
    cerr << "Failed to create socket" << endl;
    WSACleanup();
    return 1;
}

// Server address
sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(8888);
InetPton(AF_INET, TEXT("127.0.0.1"), &serverAddress.sin_addr.s_addr);

// Connect to server
if (connect(clientSocket, (sockaddr*)&serverAddress, sizeof(serverAddress)) == SOCKET_ERROR) {
    cerr << "Failed to connect to server" << endl;
    closesocket(clientSocket);
    WSACleanup();
    return 1;
}

// Send command
cout << "Enter command (1 for upload, 2 for download, 3 for file list): ";
string command;
cin >> command;
if (sendCommand(clientSocket, command) != 0) {
    closesocket(clientSocket);
    WSACleanup();
    return 1;
}

// Upload file
if (command == "1") {
    // Enter file name
    cout << "Enter file name to upload: ";
    string fileName;
    cin >> fileName;

    if (sendFile(clientSocket, fileName) != 0) {
        closesocket(clientSocket);
        WSACleanup();
        return 1;
    }
}
// Download file
else if (command == "2") {
    // Enter file name
    cout << "Enter file name to download: ";
    string fileName;
    cin >> fileName;

    if (downloadFile(clientSocket, fileName) != 0) {
        closesocket(clientSocket);
        WSACleanup();
        return 1;
    }
}
// List files
else if (command == "3") {
    if (listFiles(clientSocket) != 0) {
        closesocket(clientSocket);
        WSACleanup();
        return 1;
    }
}

// Close socket
closesocket(clientSocket);

// Cleanup Winsock
WSACleanup();

return 0;
#include iostream#include fstream#include string#include WinSock2h#include WS2tcpiph#pragma commentlib ws2_32lib#define BUFFER_SIZE 2048using namespace std;int main Initialize Winsock WSADATA w

原文地址: https://www.cveoy.top/t/topic/hWWs 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录