TCP/IP UDP广播无法发送或接收

TCP/IP UDP广播无法发送或者接收数据

在看《TCP/IP 网络编程》这本书的时候,看到广播那一节,跟着书上写代码,怎么写都不行,广播就是没法发送/接收,发送端一直在发送数据,接收端就是没有反应。

对了好几遍源码,没有问题。实在是愁人。

最后查了很多资料,确定是网卡的问题。

现在的计算机都是多网卡,至少是有线+无线网卡,如果安装了虚拟机的话,还会有虚拟网卡。

广播地址无法区分网卡,只能按照默认网卡优先级发送,这就导致我们的数据没有走那个我们需要的网卡发送出去。进而导致收不到数据。

解决办法

  1. 禁用一些网卡,将用不到的网卡全部禁用掉
  2. 在代码里添加绑定IP地址的逻辑,绑定到具体的网卡IP

我是用的是第2种方式,比较方便灵活。

发送端Linux源码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#ifndef FILEPATH
#define FILEPATH "../news.txt"
#endif

constexpr int BUF_SIZE = 30;

int main(int argc, char* argv[])
{
    if (argc != 4) {
        std::cout << "Usage: " << argv[0] << "  " << std::endl;
        return 0;
    }

    int send_socket = socket(PF_INET, SOCK_DGRAM, 0);

    // 绑定到具体的网卡IP
    sockaddr_in self_adr;
    std::memset(&self_adr, 0, sizeof(self_adr));
    self_adr.sin_family = AF_INET;
    self_adr.sin_addr.s_addr = inet_addr(argv[1]);
    self_adr.sin_port = 0; // 随机分配一个端口
    int res = bind(send_socket, (sockaddr*)&self_adr, sizeof(self_adr));
    if (res == -1) {
        std::cout << "bind error";
    }

    sockaddr_in broad_adr;
    std::memset(&broad_adr, 0, sizeof(broad_adr));
    broad_adr.sin_family = AF_INET;
    broad_adr.sin_addr.s_addr = inet_addr(argv[2]);
    broad_adr.sin_port = htons(std::atoi(argv[3]));

    int so_brd = 1;
    int rtn = setsockopt(send_socket, SOL_SOCKET, SO_BROADCAST, &so_brd, sizeof(so_brd));
    if (rtn == -1) {
        std::cout << "setsockopt error" << std::endl;
        return 0;
    }

    std::ifstream fi { FILEPATH };
    while (!fi.eof()) {
        std::string msg;
        fi >> msg;
        int s = sendto(send_socket, msg.c_str(), msg.size(), 0, (sockaddr*)&broad_adr, sizeof(broad_adr));
        std::cout << s << ":" << msg << std::endl;
        sleep(2);
    }

    close(send_socket);

    return 0;
}


接收端Linux源码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 

constexpr int BUF_SIZE = 30;

int main(int argc, char* argv[])
{
    if (argc != 2) {
        std::cout << "Usage: " << argv[0] << " " << std::endl;
        return 0;
    }

    int recv_sock = socket(PF_INET, SOCK_DGRAM, 0);

    sockaddr_in adr;
    std::memset(&adr, 0, sizeof(adr));
    adr.sin_family = AF_INET;
    adr.sin_addr.s_addr = htonl(INADDR_ANY);
    int port = std::atoi(argv[1]);
    adr.sin_port = htons(port);
    std::cout << "PORT: " << port << std::endl;

    int rtn = bind(recv_sock, (sockaddr*)&adr, sizeof(adr));
    if (rtn == -1) {
        std::cout << "bind error" << std::endl;
        return 0;
    }

    char buf[BUF_SIZE] = { 0 };
    while (true) {
        sockaddr_in src_adr;
        socklen_t sl = 0;
        int l = recvfrom(recv_sock, buf, BUF_SIZE - 1, 0, (sockaddr*)&src_adr, &sl);
        std::string srcIp = inet_ntoa(src_adr.sin_addr);
        std::cout << srcIp << " - ";
        if (l < 0) {
            break;
        }
        buf[l] = 0;
        std::cout << buf;
    }

    close(recv_sock);

    return 0;
}


接收端Win源码:

#include 
#include 
#include 
#include 
#include 

constexpr int BUF_SIZE = 30;

int main(int argc, char* argv[])
{
    if (argc != 2) {
        std::cout << "Usage: " << argv[0] << " " << std::endl;
        return 0;
    }

    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        std::cout << "WSA error" << std::endl;
        return 0;
    }

    SOCKET recvSock = socket(PF_INET, SOCK_DGRAM, 0);
    SOCKADDR_IN adr;
    std::memset(&adr, 0, sizeof(adr));
    adr.sin_family = AF_INET;
    adr.sin_addr.s_addr = htonl(INADDR_ANY);
    int port = std::atoi(argv[1]);
    adr.sin_port = htons(port);

    int rtn = bind(recvSock, (SOCKADDR*)&adr, sizeof(adr));
    if (rtn == SOCKET_ERROR) {
        std::cout << "bind error" << std::endl;
        return 0;
    }

    std::cout << "服务已启动:" << port << std::endl;

    char buf[BUF_SIZE] = { 0 };
    while (true) {
        int strLen = recvfrom(recvSock, buf, BUF_SIZE - 1, 0, nullptr, 0);
        if (strLen < 0) {
            break;
        }
        buf[strLen] = 0;
        std::cout << buf << std::endl;
    }

    closesocket(recvSock);
    WSACleanup();

    return 0;
}

你可能感兴趣的:(C++网络编程,tcp/ip,udp,广播,多播,Socket,局域网,tcp)