SFML UdpSockets 不是 sending/receiving 没有错误吗?

SFML UdpSockets not sending/receiving without error?

我正在为我的 CS class 开发一款原始的多人战舰游戏。我能够得到它 运行 并通过 LAN 发送信息。为了测试此通信是否有效,我创建了一个带有侦听器功能的循环,当确认已从另一台计算机的侦听线程接收到数据包时,该循环将停止。

当我这样做时,它似乎发送了数据包,但循环保持 运行 永远没有任何错误,也没有收到另一台计算机收到数据包的确认。你知道会发生什么吗?

//SFML headers
#include <SFML/Network.hpp>
//standard headers
#include <vector>
#include <iostream>
#include <string>
#include <thread>
#include <atomic>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <exception>


std::atomic_bool running;
unsigned short port;

//Functions for threading

void startListen(sf::UdpSocket *sock, std::atomic_bool *r, sf::IpAddress *ip)
{
    std::cout<<"Listener started"<<std::endl;
    while(*r == true)
    {
        sf::sleep(sf::microseconds(50));
        std::cout<<"Wait done"<<std::endl;
        std::cout<<std::to_string(port)<<" port from the listen thread."<<std::endl;
        sf::IpAddress sender;
        sf::Packet p;
        std::cout<<"tryna receive packet"<<std::endl;
        if (sock->receive(p, sender, port) != sf::Socket::Done)
        {
            std::cout<<"Attempt at packet retrieval but packet retrieval failed! Error code 23./nPlease contact developer at mldevelopingstudios@gmail.com to report this failure."<<std::endl;
        }
        std::cout<<"Packet received!"<<std::endl;
        std::string type;
        p >> type;
        std::cout<<"packet recieved, type: "<<type<<std::endl;
    }
}


int main()
{
    port = 4343;

    sf::UdpSocket sock;
    sf::UdpSocket recSock;
    if (recSock.bind(port) != sf::Socket::Done)
    {
        std::cout<<"Whoops we crashed!!!!!!!!!!";
        return 0;
    }
    //sock.unbind();

    std::string ip;
    sf::IpAddress IP1 = sf::IpAddress::getLocalAddress();
    sf::String ip11 = IP1.toString();
    std::cout<<"You LAN IP is: "<<ip11.toAnsiString()<<std::endl;
    std::cout<<"Please enter the target IP address of the computer you'd like to play with."<<std::endl;
    std::string iiii;
    std::cin>>iiii;
    sf::String o(iiii);
    ip = o;
    sf::IpAddress IP(ip);

    std::thread listen(startListen, &recSock, &running, &IP);
    listen.detach();

    std::cout<<"Connection check"<<std::endl;
    running = true;

    std::string name;
    std::cout<<"Name? "<<std::endl;
    std::cin>>name;

    while(running == true)
    {
        std::string mess = name;
        sf::Packet packk;
        std::string temp;
        std::cout<<"Message: "<<std::endl;
        std::cin>>temp;
        if(temp == "!!STOP!!")
        {
            running = false;
        }
        else
        {
            mess = mess+ ": "+ temp;
            packk<<mess;
            if(sock.send(packk, IP, port)!= sf::Socket::Done)
            {
                std::cout<<"Sending error in test loop!!!!"<<std::endl;
            }
        }
    }

    recSock.unbind();
    return 0;
}

非常感谢任何帮助, 谢谢,祝你有愉快的一天。

编辑:新代码是 "minimalist code",仍然没有错误,但两端都没有收到任何东西。

编辑:添加代码以显示正在发生的事情

//SFML headers
#include <SFML/Network.hpp>
//standard headers
#include <vector>
#include <iostream>
#include <string>
#include <thread>
#include <atomic>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <exception>


std::atomic_bool running;
unsigned short port;

//Functions for threading

void startListen(sf::UdpSocket *sock, std::atomic_bool *r, sf::IpAddress *ip)
{
    std::cout<<"Listener started"<<std::endl;
    while(*r == true)
    {
        unsigned short por = 4343;
        sock->setBlocking(false);
        std::cout<<std::to_string(por)<<" port from the listen thread."<<std::endl;
        sf::IpAddress sender;
        sf::Packet p;
        std::cout<<"tryna receive packet"<<std::endl;
        sf::Socket::Status stat = sock->receive(p, sender, por);
        switch(stat)
        {
            case(sf::Socket::Done):
            {
                std::cout<<"Packet receiving completed!"<<std::endl;
                std::cout<<"Packet received!"<<std::endl;
                std::string type;
                p >> type;
                std::cout<<"packet recieved, type: "<<type<<std::endl;
                break;
            }
            case(sf::Socket::NotReady):
                std::cout<<"Socket not ready to received!"<<std::endl;
                break;
            case(sf::Socket::Partial):
                std::cout<<"socket Kinda received?"<<std::endl;
                break;
            case(sf::Socket::Disconnected):
                std::cout<<"Socket disconnected!"<<std::endl;
                break;
            case(sf::Socket::Error):
                std::cout<<"Errorrrrrrrrrrrrrrrrrrr"<<std::endl;
                break;
            default:
                std::cout<<"Oh noooooo"<<std::endl;
                break;
        }
    }
}


int main()
{
    port = 4343;

    sf::UdpSocket sock;
    sf::UdpSocket recSock;
    if (recSock.bind(port) != sf::Socket::Done)
    {
        std::cout<<"Whoops we crashed!!!!!!!!!!";
        return 0;
    }
    std::string ip;
    sf::IpAddress IP1 = sf::IpAddress::getLocalAddress();
    sf::String ip11 = IP1.toString();
    std::cout<<"You LAN IP is: "<<ip11.toAnsiString()<<std::endl;
    std::cout<<"Please enter the target IP address of the computer you'd like to play with."<<std::endl;
    std::string iiii;
    std::cin>>iiii;
    sf::String o(iiii);
    ip = o;
    sf::IpAddress IP(ip);

    std::thread listen(startListen, &recSock, &running, &IP);
    listen.detach();

    std::cout<<"Connection check"<<std::endl;
    running = true;

    std::string name;
    std::cout<<"Name? "<<std::endl;
    std::cin>>name;

    while(running == true)
    {
        std::string mess = name;
        sf::Packet packk;
        std::string temp;
        std::cout<<"Message: "<<std::endl;
        std::cin>>temp;
        if(temp == "!!STOP!!")
        {
            running = false;
        }
        else
        {
            mess = mess+ ": "+ temp;
            packk<<mess;
            sf::Socket::Status stat = sock.send(packk, IP, port);
            switch(stat)
            {
                case(sf::Socket::Done):
                    std::cout<<"Packet Sending completed!"<<std::endl;
                    break;

                case(sf::Socket::NotReady):
                    std::cout<<"Socket not ready to send!"<<std::endl;
                    break;
                case(sf::Socket::Partial):
                    std::cout<<"socket Kinda sent?"<<std::endl;
                    break;
                case(sf::Socket::Disconnected):
                    std::cout<<"Socket disconnected!"<<std::endl;
                    break;
                case(sf::Socket::Error):
                    std::cout<<"Errorrrrrrrrrrrrrrrrrrr"<<std::endl;
                    break;
                default:
                    std::cout<<"Oh noooooo"<<std::endl;
            }
        }
    }

    recSock.unbind();
    return 0;
}

接收端的套接字不断报告sf::Socket::NotReady 发送报告 sf::Socket::Done 当 blocking 设置为 true 或 false

时,两端都没有收到任何东西

我通过反复测试以某种方式解决了问题。我相信我的错误来自运算符重载以将我的 类 加载到数据包中。或者这是我的 IP 条目。两者之一,修复它。可悲的是,我没有记录修复,但我确信这是两个问题之一。