【问题标题】:SFML sending packets using structures not workingSFML 使用不工作的结构发送数据包
【发布时间】:2013-10-14 13:53:43
【问题描述】:

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

我使用的代码结构如下;

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

任何帮助我都非常感谢。

【问题讨论】:

    标签: c++ visual-c++ struct sfml


    【解决方案1】:

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

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

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

    // Server code
    packet << pac;
    
    // Client code
    if (packet >> _packet) 
    

    【讨论】:

      猜你喜欢
      • 2015-07-05
      • 1970-01-01
      • 2014-07-03
      • 1970-01-01
      • 2016-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多