欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UDP连接类

程序员文章站 2022-06-29 18:55:10
...

也就是为了代码不那么乱,封了UDP的连接类,还凑合吧

头文件UDP_Conn.h

#ifndef UDP_CONN_H

#define UDP_CONN_H

#include <string>
#include <netinet/in.h>

using namespace std;

class UDP_Conn
{
private:
  int udp_fd;
  struct sockaddr_in localAddr;

public:
  UDP_Conn(string IP_Bind, int Port_Bind);
  ~UDP_Conn();

  void SendMessage(char *msg, int size, struct sockaddr_in *addr);
  void SendMessage(char *msg, int size, const char *IP, int port);

  void RecvMessage(char *msg, struct sockaddr_in *addr);
  void RecvMessage(char *msg, char *source_IP, int &Port);
};

#endif

源文件UDP_Conn.cpp

#include "Global.h"
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/select.h>

#include "UDP_Conn.h"


#define Msg_Length 400

UDP_Conn::UDP_Conn(string IP_Bind, int Port_Bind)
{

    // 对套接字初始化
    bzero(&localAddr, sizeof(localAddr));
    localAddr.sin_family = AF_INET;
    localAddr.sin_port = htons(Port_Bind);
    // localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    localAddr.sin_addr.s_addr = inet_addr(IP_Bind.c_str());

    // 创建套接口
    if ((udp_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket create failed\n");
        exit(EXIT_FAILURE);
    }
    // puts("socket create success");
    // 设置端口
    if (bind(udp_fd, (struct sockaddr *)&localAddr, sizeof(localAddr)) < 0)
    {
        perror("bind fail\n");
        close(udp_fd);
        exit(EXIT_SUCCESS);
    }
    // puts("bind success");
}

void UDP_Conn::SendMessage(char *msg, int size, struct sockaddr_in *addr)
{
    int err = sendto(udp_fd, msg, size, 0, (struct sockaddr *)addr, sizeof(struct sockaddr_in));
    // printf("err: %d\n", err);
}

void UDP_Conn::SendMessage(char *msg, int size, const char *IP, int port)
{
    cout << IP << ":" << port << endl;
    struct sockaddr_in remoteAddr;

    bzero(&remoteAddr, sizeof(struct sockaddr_in));
    remoteAddr.sin_family = AF_INET;
    remoteAddr.sin_port = htons(port);
    remoteAddr.sin_addr.s_addr = inet_addr(IP);

    this->SendMessage(msg, size, &remoteAddr);
}

void UDP_Conn::RecvMessage(char *msg, struct sockaddr_in *addr)
{
    socklen_t addrlen = sizeof(struct sockaddr_in);
    bzero(addr, sizeof(struct sockaddr_in));

    recvfrom(udp_fd, msg, Msg_Length, 0, (struct sockaddr *)&addr, &addrlen);
}

void UDP_Conn::RecvMessage(char *msg, char *source_IP, int &Port)
{
    struct sockaddr_in addr;
    socklen_t addrlen = sizeof(struct sockaddr_in);
    bzero(&addr, sizeof(struct sockaddr_in));

    msg = new char[Msg_Length]; 
    recvfrom(udp_fd, msg, Msg_Length, 0, (struct sockaddr *)&addr, &addrlen);

    strcpy(source_IP, inet_ntoa(addr.sin_addr));
    Port = addr.sin_port;
}

UDP_Conn::~UDP_Conn()
{
    close(udp_fd);
}