C++ Windows Socket 简单示例

服务端

#include 
#include 

#pragma comment(lib,"ws2_32.lib")
#pragma warning(disable:4996)

#define SERVICE_IP "192.168.X.X"
#define SERVICE_PORT (1024 + 256)
#define SERVICE_BUFFER_SIZE 1024

int main() {
    // 初始化 WinSocket
    WSADATA data;
    if (WSAStartup(MAKEWORD(2, 2), &data) != 0) {
        std::cout << "初始化失败" << std::endl;
        WSACleanup();
        return -1;
    }

    // 创建用于监听的 socket
    SOCKET listen_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (listen_socket == -1) {
        std::cout << "创建 socket 失败" << std::endl;
        WSACleanup();
        return -1;
    }
 
    // 绑定服务器信息和端口
    sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = SERVICE_PORT;
    addr.sin_addr.S_un.S_addr = inet_addr(SERVICE_IP);
    if (bind(listen_socket, (sockaddr*)&addr, sizeof(addr)) == -1) {
        std::cout << "绑定 socket 失败" << std::endl;
        WSACleanup();
        return -1;
    }

    // 监听绑定的端口
    if (listen(listen_socket, 5) == -1) {
        std::cout << "监听 socket 失败" << std::endl;
        WSACleanup();
        return -1;
    }

    // 监听到客户端的连接请求后创建 socket 进行连接
    sockaddr client;
    int client_len = sizeof(client);
    SOCKET client_socket = accept(listen_socket, &client, &client_len);
    if (client_socket == -1) {
        std::cout << "接收客户端 socket 失败" << std::endl;
        WSACleanup();
        return -1;
    }

    // 发送信息到客户端
    char service_write_buffer[SERVICE_BUFFER_SIZE] = "服务器已接收客户端请求";
    if (send(client_socket, service_write_buffer, sizeof(service_write_buffer), 0) == -1) {
        std::cout << "服务端发送响应信息失败" << std::endl;
        WSACleanup();
        return -1;
    }

    // 接收客户端发送来的信息
    char service_read_buffer[SERVICE_BUFFER_SIZE];
    if (recv(client_socket, service_read_buffer, SERVICE_BUFFER_SIZE, 0) <= 0) {
        std::cout << "接收客户端发送信息失败" << std::endl;
        WSACleanup();
        return -1;
    }

    std::cout << "客户端请求: " << service_read_buffer << std::endl;
    
    WSACleanup();
    
    return 0;
}

客户端

#include 
#include 

#pragma comment(lib,"ws2_32.lib")
#pragma warning(disable:4996)

#define SERVICE_IP "192.168.X.X"
#define SERVICE_PORT (1024 + 256)
#define CLIENT_BUFFER_SIZE 1024

int main() {
    // 初始化 WinSocket
    WSADATA data;
    if (WSAStartup(MAKEWORD(2, 2), &data) != 0) {
        std::cout << "初始化失败" << std::endl;
        WSACleanup();
        return -1;
    }

    // 创建 socket 用于连接服务端
    SOCKET client_socket = socket(AF_INET, SOCK_STREAM, 0);
    if (client_socket == -1) {
        std::cout << "创建 socket 失败" << std::endl;
        WSACleanup();
        return -1;
    }

    // 设置服务端地址和端口信息并进行连接
    sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = SERVICE_PORT;
    addr.sin_addr.S_un.S_addr = inet_addr(SERVICE_IP);
    if (connect(client_socket, (sockaddr*)&addr, sizeof(addr)) == -1) {
        std::cout << "连接服务端失败" << std::endl;
        WSACleanup();
        return -1;
    }

    // 接收服务端发送来的响应信息
    char client_read_buffer[CLIENT_BUFFER_SIZE];
    if (recv(client_socket, client_read_buffer, CLIENT_BUFFER_SIZE, 0) <= 0) {
        std::cout << "接收服务端响应失败" << std::endl;
        WSACleanup();
        return -1;
    }
    else {
        std::cout << "服务端响应: " << client_read_buffer << std::endl;
    }

    // 发送信息到服务端
    char client_write_buffer[CLIENT_BUFFER_SIZE] = "客户端已接收到服务端响应";
    if (send(client_socket, client_write_buffer, sizeof(client_write_buffer), 0) == -1) {
        std::cout << "向服务端发送信息失败" << std::endl;
        WSACleanup();
        return -1;
    }

    WSACleanup();

    return 0;
}

你可能感兴趣的:(c++,windows,开发语言)