试图通过udp套接字广播 [英] trying to broadcast through udp socket

查看:135
本文介绍了试图通过udp套接字广播的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰巧遇到我的广播服务器的问题。基本上,我想要它从发射它的那一刻连续发送广播。由于某种原因,它不会开始,直到它从某处接收到连接。我必须搞砸了一些东西,但我不能意识到什么。

I've bumped into a problem with my broadcasting server. basically, I want it to send broadcasts continuously from the moment I launch it. for some reason it will not start until it receives a connection from somewhere. I must have messed up something but I can't realise what.

这里是我的代码:

WSADATA wsaData;
    WSAStartup(MAKEWORD(2, 2), &wsaData);
    SOCKET sock;
    sock = socket(AF_INET, SOCK_DGRAM, 0);
    char broadcast = 'a';

    if(setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) < 0)
    {
        perror("broadcast options");
        closesocket(sock);
        return 1;
    }
    struct sockaddr_in Recv_addr;
    struct sockaddr_in Sender_addr;
    int len = sizeof(struct sockaddr_in);
    char recvBuff[50];
    int recvBuffLen = 50;
    //char sendMsg[] = "broadcast message from salam rofl";

    Recv_addr.sin_family = AF_INET;
    Recv_addr.sin_port = htons(PORT);
    Recv_addr.sin_addr.s_addr = INADDR_ANY;

    if(bind(sock, (sockaddr*)&Recv_addr, sizeof(Recv_addr)) < 0)
    {
        perror("bind");
        _getch;
        closesocket(sock);
        return 1;
    }

    //recvfrom(sock, recvBuff, recvBuffLen, 0, (sockaddr *)&Sender_addr, &len);
    //cout << "\nreceived message: " << recvBuff;

    while(1)
    {
        Sleep(3000);


        //_getch();

        getTime();
        if(sendto(sock, currentTime, strlen(currentTime)+1, 0, (sockaddr *)&Sender_addr, sizeof(Sender_addr)) < 0)
        {
            perror("borhot send: ");
            _getch();
            closesocket(sock);
            return 0;
        }
        else cout << "message sent successfully";
    }

    _getch;
    closesocket(sock);
    WSACleanup();
    return 0;

基本上如果我删除recvfrom,它会给我一个发送错误拼图我。此外,如果我与客户端发送它,它将开始广播,但如果我与另一个客户端连接,只有第一个客户端正在接收广播。

basically if I remove recvfrom, it will give me a send error ("No error") which simply puzzles me. also, if I send it something with a client, it will start broadcasting, but if I connect with another client, only the first client is receiving the broadcast.

提前谢谢。

推荐答案

看起来像你的 Sender_addr 永远不会被初始化,因此当你删除 recvfrom 时,你会收到一个错误,当

Looks like your Sender_addr is never being initialized, thus when you remove the recvfrom you're getting an error, and when the recvfrom is in place it's getting populated with the address of the first client to connect (but never being updated).

如果你没有在第一个客户端连接的地址,知道你想要广播的客户端的地址,你需要设置一些握手,他们发送给你一个ping,你收到它recvfrom,并将他们的地址存储在一个列表或东西。然后,当您广播时,您需要将您的消息发送到列表中的每个客户端地址。

If you don't know the addresses of the clients that you want to broadcast to, you'll need to setup some handshake where they send you a ping, you receive it with recvfrom, and you store their address in a list or something. Then, when you broadcast, you need to send your message to every client address in the list.

这篇关于试图通过udp套接字广播的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆