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

Author Topic: [SOLVED] Starting with Packet and Udp Socket  (Read 11248 times)

0 Members and 1 Guest are viewing this topic.

Xavier59

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
[SOLVED] Starting with Packet and Udp Socket
« on: August 30, 2015, 09:29:38 pm »
Hello,

I am starting with SFML and networking. I' am trying to send a simple packet to a server but this does not work.
I know I am still a newbie, so if there is some huge and stupid errors, please take the time to explain me or to redirect me.

Here is my socket object (Udp):

#include <iostream>
#include "SFML/Network.hpp"
#include "decode.hpp"
#include "receive.hpp"

mySocket::mySocket(unsigned short port){
        unsigned short m_port = port;
        std::string m_error = "not error";
        sf::UdpSocket m_socket;
        sf::Packet m_packet;
        sf::IpAddress m_address("82.236.48.119");
}

int mySocket::buildSocket(bool blocking){               // return 0 if everything ok. else, -1
        m_socket.setBlocking(blocking);
        if(m_socket.bind(sf::Socket::AnyPort) != sf::Socket::Done){
                m_error = "Error in function buildSocket : Unable to find a port \n";
                return 0;
        }
        return 1;
}

sf::Packet mySocket::returnPacket(){            // return the packet
        return m_packet;
}

std::string mySocket::error(){
        return m_error;
}

int mySocket::sendPacket(sf::Packet& sendPacket){               // return 0 if there was an error. else, 1

        if(m_socket.send(sendPacket, m_address, m_port) != sf::Socket::Done){
                m_error = "Error in function sendPacket : Unable to send th packet";
                return 0;
        }
        m_error = "It's working !";
        return 1;
}

 

Here is my function :

void sendMessages(){
    mySocket socket(12800);
    sf::Packet packet;
    socket.buildSocket(false);

    std::string myString = "connection";
    while(1){
        Sleep(2000);
        packet << myString;
        std::string erreur = socket.error();
        std::cout << erreur << std::endl;
        socket.sendPacket(packet);
    }
}

Expected output :

Quote
It's working !

Actual output :

Quote
Error in function sendPacket : Unable to send th packet

Thanks,
Xavier
« Last Edit: September 03, 2015, 09:11:43 pm by Xavier59 »

Xavier59

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Starting with Packet and Udp Socket
« Reply #1 on: September 02, 2015, 12:11:31 am »
I didn't find the solution.
As the server i'm working on is not fully working too, I would like to know if the failed to send the packet could be due to the unability from the server to receive the packet correctly ?
This appears strange to me but as far as I know, Udp is not secure, and then, do not need to know if the packet was receive (and then if the server really exists)
Thanks.

Verra

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Starting with Packet and Udp Socket
« Reply #2 on: September 02, 2015, 12:24:25 am »
Where do you do receiving? You only show sending code.
Also if you use this, sf::Socket::AnyPort, on both server and client, there's no guarantee you will pick the correct port when trying to connect. Use a known socket something like 51234.

Xavier59

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Starting with Packet and Udp Socket
« Reply #3 on: September 02, 2015, 06:40:44 pm »
Where do you do receiving? You only show sending code.
Also if you use this, sf::Socket::AnyPort, on both server and client, there's no guarantee you will pick the correct port when trying to connect. Use a known socket something like 51234.

On the server side, it is fix to the port 12800.
I'm only showing sending code because as I can't send anything actually, I don't see the point to show receving code. Btw, my question stay, could the failure during sending packet be causes by a not working server ? As it is Udp packet, it couldn't be the case, but just to be sure.

Verra

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Starting with Packet and Udp Socket
« Reply #4 on: September 02, 2015, 08:42:59 pm »
I don't see the point to show receving code.

I am starting with SFML and networking. I' am trying to send a simple packet to a server but this does not work.

Because your receiving code could have an error in it. I'd like to see it. It'd also be beneficial if you posted it as a minimal complete example. i.e) without your socket wrapper. I'd also double check that your ports are open. Especially if you are connecting to an external ip: sf::IpAddress m_address("82.236.48.119"); If both computers are on the same network you want to use the internal router ip, not 82.236... etc.

Xavier59

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Starting with Packet and Udp Socket
« Reply #5 on: September 02, 2015, 11:27:05 pm »
I just receive it simply (on the server) by doing that :

void createSocket(){
    unsigned short bindPort = 12800;
    unsigned short clientPort;
    int ret = 0;
    sf::UdpSocket socket;
    sf::IpAddress clientAddr;
    sf::Packet packet;
    Map mainMap;

    if (socket.bind(bindPort) != sf::Socket::Done){
        if (sf::Socket::Error) std::cout << "An unexpected error happened : Fatal Error !" << std::endl;
        if (sf::Socket::NotReady) std::cout << "The socket is not ready to send/receive data yet !" << std::endl;
    }

    std::cout << "The socket has been build and is ready to listen on port : " << bindPort << std::endl;

    while(1){
        packet.clear();
        socket.receive(packet, clientAddr, clientPort);
        std::cout << "A packet as been receive !" << std::endl;
        std::string header = readFromPacket(packet);
        ret = callFunction(packet, header, clientAddr, clientPort, mainMap, socket);
    }
}

But again, as it is UDP socket, I don't see how the receive method I use on my server could influence the fact that I can't send an UDP packet on the client side.

port 12 800 is open on the server. I didn't open port on the client as I didn't do it with python when i worked with socket (it was working :/ )

Btw, I want to communicate over computer who are not on the same network. But if it was the case, why an address like 82.236 is bad ? It will just take a little more time, isn't it ?


Thanks,
Xavier

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Starting with Packet and Udp Socket
« Reply #6 on: September 02, 2015, 11:38:14 pm »
Stop guessing.
 Fire up something like Wireshark (at both ends (sender and receiver)) and see what's actually going on. Then draw conclusions based on actual facts rather than guesswork.

Also, researching how the underlying IP and UDP protocols actually work wouldn't hurt you, (in my book it would actually be considered "doing your basic homework") before you try to implement a program using them...
« Last Edit: September 02, 2015, 11:58:04 pm by Jesper Juhl »

Xavier59

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Starting with Packet and Udp Socket
« Reply #7 on: September 02, 2015, 11:49:14 pm »
Of course I did !
There is not a single packet exiting my computer. And so it's not for the receiver.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Starting with Packet and Udp Socket
« Reply #8 on: September 02, 2015, 11:56:49 pm »
Did you check your local firewall rules? Are such packets even allowed to exit your system?

Xavier59

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Starting with Packet and Udp Socket
« Reply #9 on: September 03, 2015, 12:21:57 am »
I disabled my firewall for test, as I did for my anti-virus. But this isn't working.
I also tryed
sf::IpAddress m_address("127.0.0.1");
without more success.
Thanks.

dabbertorres

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • website/blog
Re: Starting with Packet and Udp Socket
« Reply #10 on: September 03, 2015, 04:56:18 am »
What port are you specifying (if any)? I don't see you picking an actual port anywhere. It looks like you may have attempted to in your top post, but you created a bunch of local variables in your constructor, which isn't doing what I think you want to be doing.

Xavier59

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Starting with Packet and Udp Socket
« Reply #11 on: September 03, 2015, 07:18:27 am »
I am using any specific port on my computer for sending and receiving as it is client side.
But I use, in my constructor :

mySocket socket(12800);

Then, I am trying to send a packet via the port 12800 of my server :

m_socket.send(sendPacket, m_address, m_port)

Thanks.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Starting with Packet and Udp Socket
« Reply #12 on: September 03, 2015, 07:52:01 am »
If sf::UdpSocket::send returns an error, I doubt it is caused by the environment (firewall, client, ...). A UDP socket doesn't care what happens to the data after you write them. So it is most likely a programming issue on client side.

Therefore, to help you, we must know:
- your OS
- your version of SFML
- a complete and minimal example that reproduces the problem, rather than incomplete parts of your original program (see http://sscce.org/)
Laurent Gomila - SFML developer

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Starting with Packet and Udp Socket
« Reply #13 on: September 03, 2015, 07:55:19 am »
Ok after reviewing your code I found your error:

mySocket::mySocket(unsigned short port){
    unsigned short m_port = port;
    std::string m_error = "not error";
    sf::UdpSocket m_socket;
    sf::Packet m_packet;
    sf::IpAddress m_address("82.236.48.119");
}

In this constructor, you declare local variables with same names as your members, instead of assigning values to these actual class members. So they remain empty / default-constructed, especially the port and address.

Do this instead:
mySocket::mySocket(unsigned short port)
: m_port(port),
, m_error("not error"),
, m_socket(),
, m_packet(),
, m_address("82.236.48.119")
{
}

By the way, you most likely don't need to keep a sf::Packet as a class member. Packets are usually used locally when sending / receiving data.
« Last Edit: September 03, 2015, 07:57:20 am by Laurent »
Laurent Gomila - SFML developer

Xavier59

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Starting with Packet and Udp Socket
« Reply #14 on: September 03, 2015, 05:05:03 pm »
This was the error ! It's now working...
I'm so sorry to annoyed you with that as I'm starting in POO !
Thanks.
NB: Is there a way to put the topic on "resolve" or something like that ? :)

 

anything