0

所以我试图让我的服务器向我的客户端发送一个包含操作码和大小的数据包,但是当我发送它时,它什么也没打印/不起作用。

我正在为代码使用结构,结构如下;

struct PacketFormatHeader
{
    unsigned short size; // How many bytes are in the data
    unsigned short opcode; // The operation code of this packet
} _packet;

这是我发送数据包的服务器 login.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);
}

我非常感谢任何帮助。

4

1 回答 1

0

SFML 数据包元素的读取顺序与写入顺序相同:

if (packet >> _packet.size >> _packet.opcode)

但是,您为 PacketFormatHeader 正确定义了写入 (<<) 和读取 (>>) 运算符,为什么不使用它们呢?

// Server code
packet << pac;

// Client code
if (packet >> _packet) 
于 2014-03-15T12:36:31.107 回答