Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Packets not sending / recieving  (Read 2012 times)

0 Members and 1 Guest are viewing this topic.

Youngy99

  • Newbie
  • *
  • Posts: 1
    • View Profile
Packets not sending / recieving
« on: October 08, 2013, 12:10:54 am »
So i am trying to write a server - client connection but when i send packets from my server they are not sending / the client is not receiving. Here is my Server login handler and my client networking cpp.

void Login::HandleLogin()
{
        Packet::PacketFormatHeader _packet = {8, 1};

        sf::Packet packet;
        packet << _packet;

        if(net.client.send(packet) != sf::Socket::Done) {
                cout << "Not working" << endl;
        }

        util::Logger::Dbg("Login step one passed: Sent first packet: Opcode 1");
        cout << _packet.opcode << " " << _packet.size << endl;
}

And i get "Not working return from"

   if(net.client.send(packet) != sf::Socket::Done) {
      cout << "Not working" << endl;
   }

And here is the client networking

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

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

        PacketFormatHeader _packet;

        socket.setBlocking(false);

        // Receive the packet at the other end
        sf::Packet packet;

        if(socket.receive(packet) != sf::Socket::Done) {
                cout << "not working" << endl;
        }
        // Extract the variables contained in the packet
        if(packet >> _packet) {
                cout << _packet.opcode << " " << _packet.size << 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);
}

And pretty much same thing happens it just says not working. But it does send a packet and what i send should come through as

1, 8 from the server
the client receives 52428 ?

Thanks in advance.