UDP接口封装

server.h

复制代码
#include <iostream>
#include <string>
#include <vector>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
#include <cstdlib>

using namespace std;

class Server{
public:
    // 修正构造函数名称
    Server(string ip,uint16_t port):sockfd_(-1),ip_(ip),port_(port){}

    bool init(){
        //创建套接字
        int socket_fd = socket(AF_INET,SOCK_DGRAM,0);

        if(socket_fd==-1){
            perror("socket error");
            return false;
        }
        cout<<"socket success"<<endl;

        // 修正成员变量赋值
        sockfd_ = socket_fd;

        //绑定
        struct sockaddr_in local;
        memset(&local, '\0', sizeof(local));
        local.sin_family=AF_INET;
        local.sin_addr.s_addr=inet_addr(ip_.c_str());
        local.sin_port=htons(port_);

        if(bind(sockfd_,(struct sockaddr*)&local,sizeof(struct sockaddr_in))<0){
            perror("bind error");
            return false;
        }
        cout<<"bind success"<<endl;

        return true;

    }

    void start(){
        vector<char>buffer(4096);
        while(1){
            //信息发送方信息
            struct sockaddr_in sendfrom;
            socklen_t len=sizeof(struct sockaddr_in);
            // 修正 recvfrom 函数调用
            ssize_t size = recvfrom(sockfd_,buffer.data(),buffer.size()-1,0,(struct sockaddr*)&sendfrom,&len);
            if(size>0){
                buffer[size]='\0';
                int port = ntohs(sendfrom.sin_port);
                string ip = inet_ntoa(sendfrom.sin_addr);
                cout<<ip<<":"<<port<<"#"<<buffer.data()<<endl;

                //发送响应数据
                string response = "Message received";
                sendto(sockfd_,response.c_str(),response.size(),0,(struct sockaddr*)&sendfrom,len);

            }
            else{
                perror("recvfrom error");
            }
        }
    }

    ~Server(){
        if(sockfd_>=0){
            close(sockfd_);
        }
    }


private:
    //文件描述符
    int sockfd_;
    //ip地址
    string ip_;
    //端口号
    uint16_t port_;

};

server.cpp

复制代码
#include "server.h"

int main(int argc,char*argv[]){
    if(argc<2){
        std::cerr << "Please provide a port number as an argument.";
        return -1;
    }

    //端口号转换
    uint16_t port =atoi(argv[1]);
    std::string ip="127.0.0.1";
    Server server(ip,port);

    server.init();
    server.start();


    return 0;


}

client.h

复制代码
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <cstring>
#include <cstdlib>

class Client
{
public:
    Client(std::string server_ip, uint16_t server_port)
        :_sockfd(-1)
        , _server_ip(server_ip)
        , _server_port(server_port)
    {}

    bool init()
    {
        // 创建套接字
        _sockfd = socket(AF_INET, SOCK_DGRAM, 0);
        if (_sockfd < 0)
        {
            std::cerr << "socket error" << std::endl;
            return false;
        }
        return true;
    }

    void start()
    {
        struct sockaddr_in server;
        memset(&server, '\0', sizeof(server));
        // 协议家族
        server.sin_family = AF_INET;
        // 端口号
        server.sin_port = htons(_server_port);
        // IP
        server.sin_addr.s_addr = inet_addr(_server_ip.c_str());

        std::string input;
        char buffer[1024];
        socklen_t server_len = sizeof(server);

        while (1)
        {
            std::cout << "Please enter# ";
            std::getline(std::cin, input);

            ssize_t sent_bytes = sendto(_sockfd, input.c_str(), input.length(), 0, (struct sockaddr*)&server, server_len);
            if (sent_bytes == -1)
            {
                std::cerr << "sendto error" << std::endl;
                continue;
            }

            // 接收服务器响应
            ssize_t recv_bytes = recvfrom(_sockfd, buffer, sizeof(buffer) - 1, 0, (struct sockaddr*)&server, &server_len);
            if (recv_bytes > 0)
            {
                buffer[recv_bytes] = '\0';
                std::cout << "Server response: " << buffer << std::endl;
            }
            else if (recv_bytes == -1)
            {
                std::cerr << "recvfrom error" << std::endl;
            }
        }
    }

    ~Client()
    {
        if (_sockfd >= 0)
        {
            close(_sockfd);
        }
    }

private:
    uint16_t _server_port;
    std::string _server_ip;
    int _sockfd;
};    

client.cpp

复制代码
#include"client.h"

int main(int argc, char* argv[])
{
    if (argc < 3)
    {
        std::cout << "name  ip  port" <<std::endl;
        return -1;
    }
    uint16_t port = atoi(argv[2]);
    std::string ip = argv[1];
    Client client(ip, port);

    client.init();
    client.start();

    return 0;
}

编译运行

g++ server.cpp -o server

g++ client.cpp -o client
服务端启动

./server 5555 端口号

./client 127.0.0.1 5555 ip地址 端口号

相关推荐
猿周LV10 小时前
网络原理 - 应用层, 传输层(UDP 和 TCP) 进阶, 网络层, 数据链路层 [Java EE]
服务器·网络·网络协议·tcp/ip·udp·java-ee
FPGA_Linuxer16 小时前
FPGA 100G UDP纯逻辑协议栈
网络协议·fpga开发·udp
沧浪之水!2 天前
【Linux网络】:套接字之UDP
linux·网络·udp
HH牛码2 天前
通讯的基础概念:涵盖串行通信、并行通信、TCP、UDP、Socket 等关键概念和技术
tcp/ip·udp
Sunlight_7772 天前
第六章 QT基础:5、QT的UDP网络编程
网络·qt·udp
rufeike2 天前
UDP协议理解
网络·网络协议·udp
SuperW3 天前
Linux学习——UDP
linux·学习·udp
浪前3 天前
【网络篇】从零写UDP客户端/服务器:回显程序源码解析
服务器·网络·udp
IT瘾君3 天前
Java基础:网络编程UDP&TCP详解
java·网络·udp·tcp