温馨提示×

C++ recvfrom函数在实际项目中的应用案例

c++
小樊
86
2024-08-30 00:47:37
栏目: 编程语言

recvfrom() 是一个用于接收来自套接字的数据的函数,通常用于无连接的数据报套接字(如 UDP)

  1. 简单的 UDP 服务器:
#include<iostream>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>

const int BUFFER_SIZE = 1024;

int main() {
    int sockfd;
    struct sockaddr_in server_addr, client_addr;
    socklen_t client_addr_size;
    char buffer[BUFFER_SIZE];

    // 创建套接字
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) {
        std::cerr << "Error creating socket"<< std::endl;
        return 1;
    }

    // 配置服务器地址
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_port = htons(8080);

    // 绑定套接字
    if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
        std::cerr << "Error binding socket"<< std::endl;
        close(sockfd);
        return 1;
    }

    std::cout << "Server listening on port 8080..."<< std::endl;

    while (true) {
        // 接收数据
        client_addr_size = sizeof(client_addr);
        ssize_t received = recvfrom(sockfd, buffer, BUFFER_SIZE - 1, 0, (struct sockaddr *)&client_addr, &client_addr_size);
        if (received < 0) {
            std::cerr << "Error receiving data"<< std::endl;
            continue;
        }

        buffer[received] = '\0';
        std::cout << "Received message: "<< buffer<< std::endl;

        // 发送响应
        sendto(sockfd, "Message received", strlen("Message received"), 0, (struct sockaddr *)&client_addr, client_addr_size);
    }

    close(sockfd);
    return 0;
}

这个示例展示了如何使用 recvfrom() 函数创建一个简单的 UDP 服务器。服务器监听端口 8080,接收客户端发送的数据,并向客户端发送确认消息。

  1. 多线程 UDP 服务器:
#include<iostream>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
#include<thread>
#include <mutex>

const int BUFFER_SIZE = 1024;
std::mutex mtx;

void handle_client(int sockfd, struct sockaddr_in client_addr, socklen_t client_addr_size) {
    char buffer[BUFFER_SIZE];

    while (true) {
        // 接收数据
        ssize_t received = recvfrom(sockfd, buffer, BUFFER_SIZE - 1, 0, (struct sockaddr *)&client_addr, &client_addr_size);
        if (received < 0) {
            std::cerr << "Error receiving data"<< std::endl;
            break;
        }

        buffer[received] = '\0';
        mtx.lock();
        std::cout << "Received message from client: "<< buffer<< std::endl;
        mtx.unlock();

        // 发送响应
        sendto(sockfd, "Message received", strlen("Message received"), 0, (struct sockaddr *)&client_addr, client_addr_size);
    }
}

int main() {
    int sockfd;
    struct sockaddr_in server_addr, client_addr;
    socklen_t client_addr_size;

    // 创建套接字
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) {
        std::cerr << "Error creating socket"<< std::endl;
        return 1;
    }

    // 配置服务器地址
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = INADDR_ANY;
    server_addr.sin_port = htons(8080);

    // 绑定套接字
    if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
        std::cerr << "Error binding socket"<< std::endl;
        close(sockfd);
        return 1;
    }

    std::cout << "Server listening on port 8080..."<< std::endl;

    while (true) {
        // 等待客户端连接
        client_addr_size = sizeof(client_addr);
        if (recvfrom(sockfd, nullptr, 0, MSG_PEEK, (struct sockaddr *)&client_addr, &client_addr_size) < 0) {
            std::cerr << "Error waiting for client connection"<< std::endl;
            continue;
        }

        // 为客户端创建新线程
        std::thread client_thread(handle_client, sockfd, client_addr, client_addr_size);
        client_thread.detach();
    }

    close(sockfd);
    return 0;
}

这个示例展示了如何使用 recvfrom() 函数创建一个多线程 UDP 服务器。服务器监听端口 8080,为每个连接的客户端创建一个新线程以处理请求。这样可以同时处理多个客户端请求。

这些示例仅用于说明 recvfrom() 函数在实际项目中的应用。在实际开发中,你可能需要根据项目需求进行更多的错误处理和功能实现。

0