C++实现简单UDP通信

发送端向接收端发送数据,ip地址和端口号要与接收端一致

C++实现简单UDP通信_第1张图片

C++实现简单UDP通信_第2张图片

 

C++实现简单UDP通信_第3张图片

 cpp_UDP客户端,发送端

#include 
#include 
#include 
using namespace  std;

//添加动态库的lib
#pragma comment(lib, "ws2_32.lib")

int main() {
    SOCKET m_Socket;
    SOCKADDR_IN m_RemoteAddress; //远程地址
    int m_RemoteAddressLen;

    // socket环境
    WSADATA  wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        cout << "WSAStartup error:" << GetLastError() << endl;
        return false;
    }

    // socket对象
    m_Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (m_Socket == INVALID_SOCKET)
    {
        closesocket(m_Socket);
        m_Socket = INVALID_SOCKET;
        return false;
    }

    // 远端地址
    const char* ip = "172.22.64.148";
    int port = 9800;
    m_RemoteAddress.sin_family = AF_INET;
    m_RemoteAddress.sin_port = htons(port);
    m_RemoteAddressLen = sizeof(m_RemoteAddress);
    inet_pton(AF_INET, ip, &m_RemoteAddress.sin_addr);

    // 接收和发送
    char recvBuf[1024] = { 0 };
    char sendBuf[1024] = "Nice to meet you!我的";

    while (1) {
        int sendLen = sendto(m_Socket, sendBuf, strlen(sendBuf), 0, (sockaddr*)&m_RemoteAddress, m_RemoteAddressLen);
        if (sendLen > 0) {
            std::printf("发送到远程端连接, 其ip: %s, port: %d\n", inet_ntoa(m_RemoteAddress.sin_addr), ntohs(m_RemoteAddress.sin_port));
            cout << "发送到远程端的信息: " << sendBuf << endl;
        }

        int recvLen = recvfrom(m_Socket, recvBuf, 1024, 0, NULL, NULL);
        if (recvLen > 0) {
            std::printf("接收到一个连接, 其ip: %s, port: %d\n", inet_ntoa(m_RemoteAddress.sin_addr), ntohs(m_RemoteAddress.sin_port));
            cout << "接收到一个信息: " << recvBuf << endl;
        }
    }

    closesocket(m_Socket);
    WSACleanup();
    return true;
}

cpp_UDP服务端,接收端


#include 
#include 
#include 

using namespace  std;

//添加动态库的lib
#pragma comment(lib, "ws2_32.lib")

int main() {
    SOCKET m_Socket;
    SOCKADDR_IN m_BindAddress;   //绑定地址
    SOCKADDR_IN m_RemoteAddress; //远程地址
    int m_RemoteAddressLen;


    // socket环境
    WSADATA  wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        cout << "WSAStartup error:" << GetLastError() << endl;
        return false;
    }

    // socket对象
    m_Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (m_Socket == INVALID_SOCKET)
    {
        closesocket(m_Socket);
        m_Socket = INVALID_SOCKET;
        return false;
    }

    // 绑定占用
    const char* ip = "172.22.64.42";
    int port = 9800;
    m_BindAddress.sin_family = AF_INET;
    m_BindAddress.sin_addr.S_un.S_addr = inet_addr(ip);
    m_BindAddress.sin_port = htons(port);
    auto ret = bind(m_Socket, (sockaddr*)&m_BindAddress, sizeof(SOCKADDR));
    if (ret == SOCKET_ERROR)
    {
        closesocket(m_Socket);
        m_Socket = INVALID_SOCKET;
        return false;
    }

    // 接收和发送
    char recvBuf[1024] = { 0 };
    char sendBuf[1024] = "Nice to meet you, too!";
    m_RemoteAddressLen = sizeof(m_RemoteAddress);

    std::printf("已设置绑定占用的连接, 其ip: %s, port: %d\n", inet_ntoa(m_BindAddress.sin_addr), ntohs(m_BindAddress.sin_port));

    while (1) {
        int recvLen = recvfrom(m_Socket, recvBuf, 1024, 0, (sockaddr*)&m_RemoteAddress, &m_RemoteAddressLen);
        if (recvLen > 0) {
            std::printf("接收到一个连接, 其ip: %s, port: %d\n", inet_ntoa(m_RemoteAddress.sin_addr), ntohs(m_RemoteAddress.sin_port));
            cout << "接收到一个信息: " << recvBuf << endl;


        }
        int sendLen = sendto(m_Socket, sendBuf, strlen(sendBuf), 0, (sockaddr*)&m_RemoteAddress, m_RemoteAddressLen);
        if (sendLen > 0) {
            cout << "发送到远程端的信息: " << sendBuf << endl;
        }
    }

    closesocket(m_Socket);
    WSACleanup();
    return true;
}

你可能感兴趣的:(udp,网络,网络协议)