0


【在Linux世界中追寻伟大的One Piece】Socket编程UDP

1 -> UDP网络编程

1.1 -> V1版本 -echo server

简单的回显服务器和客户端代码。

备注:代码中会用到地址转换函数

nocopy.hpp

#pragma once
#include <iostream>

class nocopy
{
public:
    nocopy() {}
    nocopy(const nocopy&) = delete;
    const nocopy& operator = (const nocopy&) = delete;
    ~nocopy() {}
};

UdpServer.hpp

#pragma once
#include <iostream>
#include <string>
#include <cerrno>
#include <cstring>
#include <unistd.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "nocopy.hpp"
#include "Log.hpp"
#include "Comm.hpp"
#include "InetAddr.hpp"

const static uint16_t defaultport = 8888;
const static int defaultfd = -1;
const static int defaultsize = 1024;

class UdpServer : public nocopy
{
public:
    UdpServer(uint16_t port = defaultport)
        : _port(port), _sockfd(defaultfd)
    {
    }

    void Init()
    {
        // 1. 创建 socket,就是创建了文件细节
        _sockfd = socket(AF_INET, SOCK_DGRAM, 0);
        if (_sockfd < 0)
        {
            lg.LogMessage(Fatal, "socket errr, %d : %s\n", errno,
                strerror(errno));
            exit(Socket_Err);
        }

        lg.LogMessage(Info, "socket success, sockfd: %d\n",
            _sockfd);

        // 2. 绑定,指定网络信息
        struct sockaddr_in local;

        bzero(&local, sizeof(local)); // memset
        local.sin_family = AF_INET;
        local.sin_port = htons(_port);
        local.sin_addr.s_addr = INADDR_ANY; // 0

        // local.sin_addr.s_addr = inet_addr(_ip.c_str()); // 1. 4字节 IP 2. 变成网络序列
        // 结构体填完,设置到内核中了吗??没有
        int n = ::bind(_sockfd, (struct sockaddr*)&local,
            sizeof(local));
        if (n != 0)
        {
            lg.LogMessage(Fatal, "bind errr, %d : %s\n", errno,
                strerror(errno));
            exit(Bind_Err);
        }
    }

    void Start()
    {
        // 服务器永远不退出
        char buffer[defaultsize];
        for (;;)
        {
            struct sockaddr_in peer;

            socklen_t len = sizeof(peer); // 不能乱写
            ssize_t n = recvfrom(_sockfd, buffer, sizeof(buffer) -
                1, 0, (struct sockaddr*)&peer, &len);
            if (n > 0)
            {
                InetAddr addr(peer);
                buffer[n] = 0;

                std::cout << "[" << addr.PrintDebug() << "]# " <<
                    buffer << std::endl;

                sendto(_sockfd, buffer, strlen(buffer), 0, (struct
                    sockaddr*)&peer, len);
            }
        }
    }

    ~UdpServer()
    {
    }

private:
    // std::string _ip; // 后面要调整
    uint16_t _port;
    int _sockfd;
};

InetAddr.hpp

#pragma once
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

class InetAddr
{
public:
    InetAddr(struct sockaddr_in& addr) :_addr(addr)
    {
        _port = ntohs(_addr.sin_port);
        _ip = inet_ntoa(_addr.sin_addr);
    }

    std::string Ip() 
    { 
        return _ip; 
    }

    uint16_t Port() 
    { 
        return _port; 
    };

    std::string PrintDebug()
    {
        std::string info = _ip;
        info += ":";
        info += std::to_string(_port); // "127.0.0.1:4444"

        return info;
    }

    ~InetAddr() {}

private:
    std::string _ip;
    uint16_t _port;
    struct sockaddr_in _addr;
};

Comm.hpp

#pragma once

enum 
{
    Usage_Err = 1,
    Socket_Err,
    Bind_Err
};
  • 云服务器不允许直接bind公有IP,我们也不推荐编写服务器的时候,bind明确的IP,推荐直接写成INADDR_ANY。
/* Address to accept any incoming messages. */

#define INADDR_ANY ((in_addr_t) 0x00000000)

在网络编程中,当一个进程需要绑定一个网络端口以进行通信时,可以使用INADDR_ANY作为IP地址参数。这样做意味着该端口可以接受来自任何IP地址的连接请求,无论是本地主机还是远程主机。例如,如果服务器有多个网卡(每个网卡上有不同的IP地址),使用INADDR_ANY可以省去确定数据是从服务器上具体哪个网卡/IP地址上面获取的。

UdpClient.hpp

#include <iostream>
#include <cerrno>
#include <cstring>
#include <string>
#include <unistd.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

void Usage(const std::string& process)
{
    std::cout << "Usage: " << process << " server_ip server_port"
        << std::endl;
}

// ./udp_client server_ip server_port
int main(int argc, char* argv[])
{
    if (argc != 3)
    {
        Usage(argv[0]);

        return 1;
    }

    std::string serverip = argv[1];
    uint16_t serverport = std::stoi(argv[2]);

    // 1. 创建 socket
    int sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0)
    {
        std::cerr << "socket error: " << strerror(errno) <<
            std::endl;

        return 2;
    }

    std::cout << "create socket success: " << sock << std::endl;
    // 2. client 要不要进行 bind? 一定要 bind 的!!
    // 但是,不需要显示 bind,client 会在首次发送数据的时候会自动进行bind
    // 为什么?server 端的端口号,一定是众所周知,不可改变的,client 需要 port,bind 随机端口.
    // 为什么?client 会非常多.
    // client 需要 bind,但是不需要显示 bind,让本地 OS 自动随机 bind,选择随机端口号
    // 2.1 填充一下 server 信息
    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(serverport);
    server.sin_addr.s_addr = inet_addr(serverip.c_str());
    while (true)
    {
        // 我们要发的数据
        std::string inbuffer;
        std::cout << "Please Enter# ";
        std::getline(std::cin, inbuffer);

        // 我们要发给谁呀?server
        ssize_t n = sendto(sock, inbuffer.c_str(),
            inbuffer.size(), 0, (struct sockaddr*)&server, sizeof(server));
        if (n > 0)
        {
            char buffer[1024];
            //收消息
            struct sockaddr_in temp;
            socklen_t len = sizeof(temp);
            ssize_t m = recvfrom(sock, buffer, sizeof(buffer) - 1,
                0, (struct sockaddr*)&temp, &len); // 一般建议都是要填的.
            if (m > 0)
            {
                buffer[m] = 0;
                std::cout << "server echo# " << buffer <<
                    std::endl;
            }
            else
                break;
        }
        else
            break;
    }

    close(sock);

    return 0;
}

1.2 -> V2版本 -DictServer

实现一个简单的英译汉的网络字典

dict.txt

**apple:苹果 **

**banana:香蕉 **

**cat:猫 **

**dog:狗 **

**book:书 **

**pen:笔 **

**happy:快乐的 **

**sad:悲伤的 **

**run:跑 **

**jump:跳 **

**teacher:老师 **

**student:学生 **

**car:汽车 **

**bus:公交车 **

**love:爱 **

**hate:恨 **

**hello:你好 **

**goodbye:再见 **

**summer:夏天 **

winter:冬天

Dict.hpp

#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>

const std::string sep = ": ";

class Dict
{
private:
    void LoadDict()
    {
        std::ifstream in(_confpath);
        if (!in.is_open())
        {
            std::cerr << "open file error" << std::endl; // 后面可以用日志替代打印
                return;
        }

        std::string line;
        while (std::getline(in, line))
        {
            if (line.empty()) 
                continue;

            auto pos = line.find(sep);
            if (pos == std::string::npos) 
                continue;

            std::string key = line.substr(0, pos);
            std::string value = line.substr(pos + sep.size());
            _dict.insert(std::make_pair(key, value));
        }

        in.close();
    }

public:
    Dict(const std::string& confpath) :_confpath(confpath)
    {
        LoadDict();
    }

    std::string Translate(const std::string& key)
    {
        auto iter = _dict.find(key);
        if (iter == _dict.end()) 
            return std::string("Unknown");

        else 
            return iter->second;
    }

    ~Dict()
    {}

private:
    std::string _confpath;
    std::unordered_map<std::string, std::string> _dict;
};

UdpServer.hpp

#pragma once
#include <iostream>
#include <string>
#include <cerrno>
#include <cstring>
#include <unistd.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unordered_map>
#include <functional>
#include "nocopy.hpp"
#include "Log.hpp"
#include "Comm.hpp"
#include "InetAddr.hpp"

const static uint16_t defaultport = 8888;
const static int defaultfd = -1;
const static int defaultsize = 1024;

using func_t = std::function<void(const std::string& req,
    std::string* resp)>;

class UdpServer : public nocopy
{
public:
    UdpServer(func_t func, uint16_t port = defaultport)
        : _func(func), _port(port), _sockfd(defaultfd)
    {
    }

    void Init()
    {
        // 1. 创建 socket,就是创建了文件细节
        _sockfd = socket(AF_INET, SOCK_DGRAM, 0);
        if (_sockfd < 0)
        {
            lg.LogMessage(Fatal, "socket errr, %d : %s\n", errno,
                strerror(errno));
            exit(Socket_Err);
        }

        lg.LogMessage(Info, "socket success, sockfd: %d\n",
            _sockfd);

        // 2. 绑定,指定网络信息
        struct sockaddr_in local;
        bzero(&local, sizeof(local)); // memset
        local.sin_family = AF_INET;
        local.sin_port = htons(_port);
        local.sin_addr.s_addr = INADDR_ANY; // 0

        // local.sin_addr.s_addr = inet_addr(_ip.c_str()); // 1. 4字节 IP 2. 变成网络序列
        // 结构体填完,设置到内核中了吗??没有
        int n = ::bind(_sockfd, (struct sockaddr*)&local,
            sizeof(local));
        if (n != 0)
        {
            lg.LogMessage(Fatal, "bind errr, %d : %s\n", errno,
                strerror(errno));
            exit(Bind_Err);
        }
    }

    void Start()
    {
        // 服务器永远不退出
        char buffer[defaultsize];
        for (;;)
        {
            struct sockaddr_in peer;
            socklen_t len = sizeof(peer); // 不能乱写
            ssize_t n = recvfrom(_sockfd, buffer, sizeof(buffer) -
                1, 0, (struct sockaddr*)&peer, &len);
            if (n > 0)
            {
                InetAddr addr(peer);
                buffer[n] = 0;

                std::cout << "[" << addr.PrintDebug() << "]# " <<
                    buffer << std::endl;

                std::string value;
                _func(buffer, &value); // 回调业务翻译方法
                sendto(_sockfd, value.c_str(), value.size(), 0,
                    (struct sockaddr*)&peer, len);
            }
        }
    }

    ~UdpServer()
    {
    }

private:
    // std::string _ip; // 后面要调整
    uint16_t _port;
    int _sockfd;
    func_t _func;
};

Main.cc

#include "UdpServer.hpp"
#include "Comm.hpp"
#include "Dict.hpp"
#include <memory>

void Usage(std::string proc)
{
    std::cout << "Usage : \n\t" << proc << " local_port\n" <<
        std::endl;
}

Dict gdict("./dict.txt");
void Execute(const std::string& req, std::string* resp)
{
    *resp = gdict.Translate(req);
}

// ./udp_server 8888
int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);

        return Usage_Err;
    }

    // std::string ip = argv[1];
    uint16_t port = std::stoi(argv[1]);
    std::unique_ptr<UdpServer> usvr =
        std::make_unique<UdpServer>(Execute, port);

    usvr->Init();
    usvr->Start();

    return 0;
}

1.3 -> V2版本 -DictServer(封装版)

udp_socket.hpp

#pragma once
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <cassert>
#include <string>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

typedef struct sockaddr sockaddr;
typedef struct sockaddr_in sockaddr_in;

class UdpSocket 
{
public:
    UdpSocket() : fd_(-1) 
    {
    }

    bool Socket() 
    {
        fd_ = socket(AF_INET, SOCK_DGRAM, 0);
        if (fd_ < 0) 
        {
            perror("socket");

            return false;
        }

        return true;
    }

    bool Close() 
    {
        close(fd_);

        return true;
    }

    bool Bind(const std::string& ip, uint16_t port) 
    {
        sockaddr_in addr;

        addr.sin_family = AF_INET;
        addr.sin_addr.s_addr = inet_addr(ip.c_str());
        addr.sin_port = htons(port);

        int ret = bind(fd_, (sockaddr*)&addr, sizeof(addr));
        if (ret < 0) 
        {
            perror("bind");

            return false;
        }

        return true;
    }

    bool RecvFrom(std::string* buf, std::string* ip = NULL,
        uint16_t* port = NULL) 
    {
        char tmp[1024 * 10] = { 0 };
        sockaddr_in peer;

        socklen_t len = sizeof(peer);
        ssize_t read_size = recvfrom(fd_, tmp,
            sizeof(tmp) - 1, 0,
            (sockaddr*)&peer, &len);
        if (read_size < 0) 
        {
            perror("recvfrom");

            return false;
        }

        // 将读到的缓冲区内容放到输出参数中
        buf->assign(tmp, read_size);
        if (ip != NULL) 
        {
            *ip = inet_ntoa(peer.sin_addr);
        }

        if (port != NULL) 
        {
            *port = ntohs(peer.sin_port);
        }

        return true;
    }

    bool SendTo(const std::string& buf, const std::string& ip,
        uint16_t port) 
    {
        sockaddr_in addr;

        addr.sin_family = AF_INET;
        addr.sin_addr.s_addr = inet_addr(ip.c_str());
        addr.sin_port = htons(port);

        ssize_t write_size = sendto(fd_, buf.data(), buf.size(), 0,
            (sockaddr*)&addr, sizeof(addr));
        if (write_size < 0) 
        {
            perror("sendto");

            return false;
        }

        return true;
    }

private:
    int fd_;
};

UDP通用服务器

udp_server.hpp

#pragma once
#include "udp_socket.hpp"

// C 式写法
// typedef void (*Handler)(const std::string& req, std::string*resp);
// C++ 11 式写法, 能够兼容函数指针, 仿函数, 和 lambda

#include <functional>

typedef std::function<void(const std::string&, std::string*
    resp)> Handler;

class UdpServer 
{
public:
    UdpServer() 
    {
        assert(sock_.Socket());
    }

    ~UdpServer() 
    {
        sock_.Close();
    }

    bool Start(const std::string& ip, uint16_t port, Handler
        handler) 
    {
        // 1. 创建 socket
        // 2. 绑定端口号
        bool ret = sock_.Bind(ip, port);
        if (!ret) 
        {
            return false;
        }

        // 3. 进入事件循环
        for (;;) 
        {
            // 4. 尝试读取请求
            std::string req;
            std::string remote_ip;

            uint16_t remote_port = 0;
            bool ret = sock_.RecvFrom(&req, &remote_ip, &remote_port);
            if (!ret) 
            {
                continue;
            }

            std::string resp;
            // 5. 根据请求计算响应
            handler(req, &resp);
            // 6. 返回响应给客户端
            sock_.SendTo(resp, remote_ip, remote_port);
            printf("[%s:%d] req: %s, resp: %s\n", remote_ip.c_str(),
                remote_port,
                req.c_str(), resp.c_str());
        }
        sock_.Close();

        return true;
    }

private:
    UdpSocket sock_;
};

实现英译汉服务器

以上代码是对udp服务器进行通用接口的封装。基于以上封装,实现一个查字典的服务器就很容易了。

dict_server.cc

#include "udp_server.hpp"
#include <unordered_map>
#include <iostream>

std::unordered_map<std::string, std::string> g_dict;

void Translate(const std::string& req, std::string* resp) 
{
    auto it = g_dict.find(req);
    if (it == g_dict.end()) 
    {
        *resp = "未查到!";

        return;
    }

    *resp = it->second;
}

int main(int argc, char* argv[]) 
{
    if (argc != 3) 
    {
        printf("Usage ./dict_server [ip] [port]\n");

        return 1;
    }

    // 1. 数据初始化
    g_dict.insert(std::make_pair("hello", "你好"));
    g_dict.insert(std::make_pair("world", "世界"));
    g_dict.insert(std::make_pair("c++", "最好的编程语言"));
    g_dict.insert(std::make_pair("bit", "特别 NB"));

    // 2. 启动服务器
    UdpServer server;
    server.Start(argv[1], atoi(argv[2]), Translate);

    return 0;
}

UDP通用客户端

udp_client.hpp

#pragma once
#include "udp_socket.hpp"

class UdpClient 
{
public:
    UdpClient(const std::string& ip, uint16_t port) : ip_(ip),
        port_(port) 
    {
        assert(sock_.Socket());
    }

    ~UdpClient() 
    {
        sock_.Close();
    }

    bool RecvFrom(std::string* buf) 
    {
        return sock_.RecvFrom(buf);
    }

    bool SendTo(const std::string& buf) 
    {
        return sock_.SendTo(buf, ip_, port_);
    }

private:
    UdpSocket sock_;
    // 服务器端的 IP 和 端口号
    std::string ip_;
    uint16_t port_;
};

实现英译汉客户端

#include "udp_client.hpp"
#include <iostream>

int main(int argc, char* argv[]) 
{
    if (argc != 3) 
    {
        printf("Usage ./dict_client [ip] [port]\n");

        return 1;
    }

    UdpClient client(argv[1], atoi(argv[2]));
    for (;;) 
    {
        std::string word;
        std::cout << "请输入您要查的单词: ";
        std::cin >> word;
        if (!std::cin) 
        {
            std::cout << "Good Bye" << std::endl;

            break;
        }

        client.SendTo(word);
        std::string result;
        client.RecvFrom(&result);
        std::cout << word << " 意思是 " << result << std::endl;
    }

    return 0;
}

感谢各位大佬支持!!!

互三啦!!!

标签: linux 运维 服务器

本文转载自: https://blog.csdn.net/weixin_74809706/article/details/143165428
版权归原作者 枫叶丹4 所有, 如有侵权,请联系我们删除。

“【在Linux世界中追寻伟大的One Piece】Socket编程UDP”的评论:

还没有评论