SFML UdpSockets没有错误地发送/接收?
SFML UdpSockets not sending/receiving without error?
我正在为我的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条目。两者之一,修复了它。可悲的是,我没有记录修复程序,尽管我确信这是两个问题之一。
相关文章:
- 使用流处理接收到的数据
- 从udp接收帧对于人脸识别来说太慢
- 在调用接收数组的方法时,模板化数组大小是不是一种糟糕的做法
- visual是否可以在c++中创建一个接收无限数量相同类型(或至少相当数量)参数的函数
- boost::asio UDP 广播客户端仅接收"fast"数据包
- 使用模板化的键类型定义 std::map,该键类型基于作为参数接收的函数
- (Winsock) UDP 接收工作正常,但同一套接字的发送失败
- 接收字符串并使用它来调用方法C++
- 如何通过UDP接收QByteArray并将其解析为位字段结构?
- 在 capnp FlatArrayMessageReader 的对齐内存缓冲区中接收 zmq 消息
- 从 Python 接收C++字符串
- 接收和返回函数指针的函数指针的类型?
- 无法使用 g++ 从多个文件接收正确的输出
- 使用 winsock 接收 http 请求
- 如何判断SSL_read是否已经接收并处理了来自单个消息的所有记录
- 从套接字 C++ 发送和接收文件
- 无法接收WM_CLIPBOARDUPDATE消息
- 对象接收堆栈溢出异常 c++ 的排序向量
- 如果变量数据包含大于 vector 所有元素的整数,则仅在视觉工作室上接收"矢量下标超出范围"?
- SFML UdpSockets没有错误地发送/接收?