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

SFML UdpSockets not sending/receiving without error?

本文关键字:接收 UdpSockets 有错误 SFML      更新时间:2023-10-16

我正在为我的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::套接字::D一 当阻塞设置为 true 或 false 时,两端均未收到任何内容

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