SFML UdpSockets没有发送/接收没有错误?

时间:2018-05-31 18:36:00

标签: c++ c++11 networking udp sfml

我正在为我的CS课程开发一个原始的多人战舰游戏。我能够让它运行并通过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;
}

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

编辑:新代码是“极简主义代码”,但仍然没有错误,但两端都没有收到任何错误。

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

//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 当阻塞设置为true或false

时,两端都没有收到任何内容

1 个答案:

答案 0 :(得分:0)

我以某种方式通过重复测试解决了问题。我相信我的错误是由于操作员重载将类加载到数据包而引起的。还是我的IP输入。修复其中之一。遗憾的是,尽管我确定这是两个问题之一,但我没有记录此修复程序。