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

Author Topic: UdpSocket Not Sending Packet [SOLVED]  (Read 2070 times)

0 Members and 1 Guest are viewing this topic.

Charsmud

  • Newbie
  • *
  • Posts: 30
    • View Profile
UdpSocket Not Sending Packet [SOLVED]
« on: February 09, 2014, 08:36:13 pm »
Hello!  I am having an error with my UdpSocket when I try to send a packet from the server to the client.  When the packet is supposed to be sent, it triggers my if loop and prints out "I AM SAD":

#include "ServerCore.hpp"
#include "Packet.hpp"

#include <SFML/Network/IpAddress.hpp>
#include <iostream>

ServerCore::ServerCore(int port)
        : mSocket()
{
        mPort = port;
}

void ServerCore::enterServerLoop()
{
        if(mSocket.bind(mPort) != sf::Socket::Done)
        {
                std::cout << "SOMETHING DIDNT WORK" << std::endl;
        }
        std::cout << "Initialized Server" << std::endl;
       
        unsigned short port;
        sf::IpAddress ip;
        while(true)
        {
                std::cout << ":D" << std::endl;

                sf::Packet packet;

                std::size_t recieved;

                if(mSocket.receive(packet, ip, port) != sf::Socket::Done)
                {

                }
               
                int x;

                if(packet >> x)
                {
                        if(x == ClientPacket::PlayerConnect)
                        {
                                std::cout << "Player Connected!" << std::endl;
                                sf::Packet packet;

                                packet << ServerPacket::ServerStop;

                                if(mSocket.send(packet, mSender, 54000) != sf::Socket::Done)
                                {
                                        std::cout << "I AM SAD" << std::endl;
                                }
                        }
                        if(x == ClientPacket::PlayerDisconnect)
                                std::cout << "Player Disconnected!" << std::endl;
                        if(x == ClientPacket::PlayerQuit)
                                std::cout << "Player Quit!" << std::endl;
                        if(x == ClientPacket::PlayerJoin)
                                std::cout << "Player Join!" << std::endl;
                       
                }


                std::cout << "Recieved " << recieved << " bytes from " << ip << " on port " << port << std::endl;
        }

        sf::Packet packet2;

        packet2 << ServerPacket::ServerStop;

        /*if(mSocket.send(packet2, mSender, 54000) != sf::Socket::Done)
        {
                std::cout << "I AM SAD" << std::endl;
        }*/

}
 

Everything else works except for the mSocket.send if loop inside of if(x == ClientPacket::PlayerConnect).  I was thinking of doing an error check to see what the actual error is but I couldn't figure out how to do this.  Any suggestions on how to fix this or check the error?


EDIT:  I managed to fix the problem: I was referencing the wrong IP var. 
« Last Edit: February 15, 2014, 04:26:43 am by Charsmud »

Charsmud

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: Error Checking a UdpSocket
« Reply #1 on: February 15, 2014, 03:10:26 am »
If necessary, I can put up the Client code as well, but I'm not sure that it would actually help much. 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: UdpSocket Not Sending Packet [SOLVED]
« Reply #2 on: February 15, 2014, 09:12:43 am »
Adding an edit at the end of your first post is the best way to make people not see it ;)

Quote from: Charsmud
EDIT:  I managed to fix the problem: I was referencing the wrong IP var.
Laurent Gomila - SFML developer

 

anything