SFML 使用结构发送数据包不起作用
SFML sending packets using structures not working
所以我试图让我的服务器向我的客户端发送一个包含操作码和大小的数据包,但是当我发送它时,它什么也打印出来/不起作用。
我正在为代码使用结构,结构如下;
struct PacketFormatHeader
{
unsigned short size; // How many bytes are in the data
unsigned short opcode; // The operation code of this packet
} _packet;
这是我发送数据包的服务器登录名.cpp。
#include "NetServer.h"
using namespace net;
Login::Login(void)
{
}
Login::~Login(void)
{
}
sf::Packet& operator <<(sf::Packet& packet, const Packet::PacketFormatHeader& p)
{
return packet << p.size << p.opcode;
}
sf::Packet& operator >>(sf::Packet& packet, Packet::PacketFormatHeader& p)
{
return packet >> p.size >> p.opcode;
}
bool Login::CheckAccount()
{
if(p.IsPlayerBanned() == true) {
// DenyLogin();
}
if(_checkAccountInUse == true) {
//DenyLogin();
}
return _accountPassed = true;
}
void Login::HandleLogin()
{
sf::Uint32 x = 12;
pac._packet.size = x;
pac._packet.opcode = 1;
sf::Packet packet;
sf::Packet& operator <<(sf::Packet& packet, const Packet::PacketFormatHeader& p);
packet << pac._packet.size << pac._packet.opcode;
net.client.send(packet);
util::Logger::Dbg("Login step one passed: Sent first packet: Opcode 1");
}
当调用它时,它创建了数据包(我希望(,然后客户端应该收到它。这是我的客户端网络(自从我一直试图让它工作一段时间以来,这很混乱(
#include "Network.h"
Network::Network(void)
{
}
Network::~Network(void)
{
}
struct PacketFormatHeader
{
unsigned short size; // How many bytes are in the data
unsigned short opcode; // The operation code of this packet
} _packet;
sf::Packet& operator <<(sf::Packet& packet, const PacketFormatHeader& p)
{
return packet << p.size << p.opcode;
}
sf::Packet& operator >>(sf::Packet& packet, PacketFormatHeader& p)
{
return packet >> p.size >> p.opcode;
}
void Network::InitNetwork(const sf::IpAddress &remoteAdress, int port)
{
sf::Socket::Status status = socket.connect(remoteAdress, port);
if (status != sf::Socket::Done)
{
cout << "Could not connect to server" << endl;
SetConnected(false);
RetryConnection("25.196.76.171", 12975);
} else {
cout << "Connected to server" << endl;
SetConnected(true);
socket.setBlocking(false);
sf::Packet& operator <<(sf::Packet& packet, const PacketFormatHeader& p);
sf::Packet& operator >>(sf::Packet& packet, PacketFormatHeader& p);
// Receive the packet at the other end
sf::Packet packet;
socket.receive(packet);
// Extract the variables contained in the packet
_packet.opcode;
_packet.size;
if (packet >> _packet.opcode >> _packet.size)
{
cout << "Data extracted successfully" << endl;
cout << _packet.opcode << _packet.size << endl;
}
}
}
void Network::SetConnected(bool i)
{
i = this->_isConnected;
}
bool Network::GetConnected()
{
return _isConnected;
}
void Network::RetryConnection(const sf::IpAddress &remoteAdress, int port)
{
for (int i = 0; i < 5; i++) {
sf::Socket::Status status = socket.connect(remoteAdress, port);
if (status != sf::Socket::Done)
{
cout << "Retrying for connection" << endl;
SetConnected(false);
} else {
cout << "Connected to server" << endl;
SetConnected(true);
}
}
MessageBoxes::Error(0, MB_OK, "Could not establish connection with the server: system exiting");
exit(EXIT_FAILURE);
}
我非常感谢任何帮助。
SFML 数据包元素的读取顺序与写入顺序相同:
if (packet >> _packet.size >> _packet.opcode)
但是,您正确地为 PacketFormatHeader 定义了写入 (<<( 和读取 (>>( 运算符,为什么不使用它们呢?
// Server code
packet << pac;
// Client code
if (packet >> _packet)
相关文章:
- 编译器上的策略数据结构不起作用
- 使用选择排序对数组数据结构进行排序,但它不起作用
- cpp / c ++中的grpc客户端代码,元数据x-api-key/x-goog-api-key不起作用,给了我语音A
- 将一个阵列中的数据存储到另外两个阵列中不起作用
- C++覆盖父结构中的数据不起作用
- C++ Eclipse:二叉搜索树节点>数据=变量似乎不起作用
- 尝试读取数据文件,存储在数组中并打印所有元素,但它不起作用
- 为什么指针>数据有效,而 *double_pointer->data 不起作用?
- 模板参数在具有相同数据类型的单个类型名的构造函数中不起作用
- OpenGL 第一个多维数据集渲染不起作用
- Lambda 表达式闭包函数不起作用
- 气泡排序不传递数据或不起作用
- pybind11:Python 到 C++ 数据类型转换不起作用
- 参数包扩展不起作用
- 使用数据初始化 cv::Mat 不起作用
- 函数调用不起作用,矢量访问数据
- 绑定数据成员在VS2012中不起作用
- 为什么我的链接数据类型复制构造函数不起作用?
- SFML 使用结构发送数据包不起作用
- 为什么我的模板参数包不起作用?