gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:47:36 25 4
gpt4 key购买 nike

我正在为我的 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;
}

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

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

编辑:添加代码以显示发生了什么

//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;
}

接收端socket不断报sf::Socket::NotReady发送报告 sf::Socket::Done当 blocking 设置为 true 或 false 时,两端都没有收到任何东西

最佳答案

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

关于c++ - SFML UdpSockets 没有发送/接收没有错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50630718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com