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

#define BUFFER_SIZE 2048

using namespace std;

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 (send(clientSocket, command.c_str(), command.length(), 0) == SOCKET_ERROR) {
    cerr << "Failed to send command" << endl;
    closesocket(clientSocket);
    WSACleanup();
    return 1;
}

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

    // Open file
    ifstream file(fileName, ios::binary);
    if (!file) {
        cerr << "Failed to open file" << endl;
        closesocket(clientSocket);
        WSACleanup();
        return 1;
    }

    // Send file name
    if (send(clientSocket, fileName.c_str(), fileName.length(), 0) == SOCKET_ERROR) {
        cerr << "Failed to send file name" << endl;
        closesocket(clientSocket);
        WSACleanup();
        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();
            closesocket(clientSocket);
            WSACleanup();
            return 1;
        }
    }

    // Close file
    file.close();

    cout << "File uploaded successfully" << endl;
}
// Download file
else if (command == "2") {
    // Enter file name
    cout << "Enter file name to download: ";
    string fileName;
    cin >> fileName;

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

    // Receive file data
    ofstream file(fileName, ios::binary);
    if (!file) {
        cerr << "Failed to create file" << endl;
        closesocket(clientSocket);
        WSACleanup();
        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;
        closesocket(clientSocket);
        WSACleanup();
        return 1;
    }

    cout << "File downloaded successfully" << endl;
}
// List files
else if (command == "3") {
    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;
        closesocket(clientSocket);
        WSACleanup();
        return 1;
    }
}

// Close socket
closesocket(clientSocket);

// Cleanup Winsock
WSACleanup();

return 0;
#include tcharh#include stdioh#include winsock2h#include stdlibh #include stringh #include Ws2tcpiph#include iostream#includefstream#pragma commentlibws2_32lib#define mm 2048#define fname_mm 512 usi

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

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