[Socket编程] 封装UDP实现一个socket类,实现客户端与服务端间的udp通信

目录

  • 头文件(udpsocket.hpp)
  • 客户端(udp_client.cpp)
  • 服务器(udp_server.cpp)


头文件(udpsocket.hpp)

#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

class UdpSocket{
    public:
        UdpSocket():_sock(-1){}		//初始化套接字描述符为 -1
        ~UdpSocket(){}
        bool Socket() {
            	//int socket(int domain, int type, int protocol);
            _sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
            if (_sock < 0) {
                perror("socket error");
                return false;
            }
            return true;
        }
        bool Bind(std::string &ip, uint16_t port) {
            struct sockaddr_in addr;
            addr.sin_family = AF_INET;		//地址域:此时为IPV4
            	//uint16_t htons(uint16_t hostshort);
            addr.sin_port = htons(port);
            	//in_addr_t inet_addr(const char *cp);
            addr.sin_addr.s_addr = inet_addr(ip.c_str());
            socklen_t len = sizeof(struct sockaddr_in);
           		//int bind(int sockfd, struct sockaddr *addr, socklen_t addrlen);
            int ret = bind(_sock, (struct sockaddr*)&addr, len);
            if (ret < 0) {
                perror("bind error");
                return false;
            }
            return true;
        }
        bool Recv(std::string &buf, struct sockaddr_in *saddr) {
            	//ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen);
            char tmp[1500] = {0};
            socklen_t len = sizeof(struct sockaddr_in);
            int ret = recvfrom(_sock, tmp, 1500, 0, 
                    (struct sockaddr*)saddr, &len);
            if (ret < 0) {
                perror("recvfrom error");
                return false;
            }
            buf.assign(tmp);
            return true;
        }
        bool Send(std::string &buf, struct sockaddr_in *daddr){
            	//ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, struct sockaddr *dest_addr, socklen_t addrlen);
            socklen_t len = sizeof(struct  sockaddr_in);
            int ret = sendto(_sock, buf.c_str(), buf.size(), 0, 
                    (struct sockaddr*)daddr, len);
            if (ret < 0) {
                perror("sendto error");
                return false;
            }
            return true;
        }
        bool Close() {
            close(_sock);
            _sock = -1;
            return true;
        }
    private:
        int _sock;	//套接字描述符
};

客户端(udp_client.cpp)

#include "udpsocket.hpp"

#define CHECK_RET(q) if((q) == false){return -1;}

int main(int argc, char *argv[]){
    if (argc != 3) {
        printf("./udp_srv ip port\n");
        return -1;
    }
    //这个地址是服务端的地址,为了让客户端直到数据请求发送到哪里
    std::string ip = argv[1];
    uint16_t port = atoi(argv[2]);

    UdpSocket sock;
    CHECK_RET(sock.Socket());
    //客户端并不推荐手动绑定地址

    struct sockaddr_in srv_addr;
    srv_addr.sin_family = AF_INET;
    srv_addr.sin_port = htons(port);
    srv_addr.sin_addr.s_addr = inet_addr(ip.c_str());
    
    while(1) {
        std::string buf;
        std::cout<<"client say:";
        fflush(stdout);
        std::cin>> buf;
        CHECK_RET(sock.Send(buf, &srv_addr));

        CHECK_RET(sock.Recv(buf, &srv_addr));
        std::cout<<"server say:"<<buf<<std::endl;
    }
    sock.Close();
}

服务器(udp_server.cpp)

#include "udpsocket.hpp"

#define CHECK_RET(q) if((q) == false){return -1;}

int main(int argc, char *argv[]){
    if (argc != 3) {
        printf("./udp_srv ip port\n");
        return -1;
    }
    std::string ip = argv[1];
    uint16_t port = atoi(argv[2]);

    UdpSocket sock;
    CHECK_RET(sock.Socket());
    CHECK_RET(sock.Bind(ip, port));

    while(1) {
        std::string buf;
        struct sockaddr_in cli_addr;
        CHECK_RET(sock.Recv(buf, &cli_addr));
        std::cout<<"client say:"<<buf<<std::endl;
        
        std::cout<<"server say:";
        fflush(stdout);
        std::cin>> buf;
        CHECK_RET(sock.Send(buf, &cli_addr));
    }
    sock.Close();
}

你可能感兴趣的:(计算机网络,Linux操作系统,C++)