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

Author Topic: UDP socket  (Read 3524 times)

0 Members and 3 Guests are viewing this topic.

Linuxxon

  • Newbie
  • *
  • Posts: 10
    • MSN Messenger - rasmus.linusson@gmail.com
    • View Profile
    • http://www.linuxxon.com
UDP socket
« on: February 13, 2012, 08:23:18 am »
I'm writing a client/server interface for a game my friends and I are working on and have stumbled upon a couple of problems.

First one:
Whenever I call receive on my listening UDP socket and no data has been sent to the server it receives from 255.255.255.255, is that normal?

Second one:
After a while of receiving data from a client it just stops to listen (I think, don't exactly know what's wrong). I can still send data to my client (from another socket) but it doesn't react to any received data and it's always random. Sometimes I'm able to send 50 and sometimes it just handles three.

Here is a paste of how I handle incoming connections http://pastebin.com/LGaSQRKz

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
UDP socket
« Reply #1 on: February 13, 2012, 08:55:10 am »
Quote
Whenever I call receive on my listening UDP socket and no data has been sent to the server it receives from 255.255.255.255, is that normal?

No. Did Receive return sf::Socket::Done? I guess it didn't, and what you get is an invalid address.

Quote
After a while of receiving data from a client it just stops to listen (I think, don't exactly know what's wrong). I can still send data to my client (from another socket) but it doesn't react to any received data and it's always random. Sometimes I'm able to send 50 and sometimes it just handles three.

It's hard to help without more code. But the first thing to do is to check the status code returned by every call.
Laurent Gomila - SFML developer

Linuxxon

  • Newbie
  • *
  • Posts: 10
    • MSN Messenger - rasmus.linusson@gmail.com
    • View Profile
    • http://www.linuxxon.com
UDP socket
« Reply #2 on: February 13, 2012, 10:12:36 am »
I added some console prints to find where the program stops. And the problem is that it won't go further than when I call receive.

It returns the whole function if the receive call isn't equal to sf::Socket::Done and IsValid returns 1 at all times...

Linuxxon

  • Newbie
  • *
  • Posts: 10
    • MSN Messenger - rasmus.linusson@gmail.com
    • View Profile
    • http://www.linuxxon.com
Minimal Code
« Reply #3 on: February 16, 2012, 08:15:12 am »
This code recreates the problem (:

Code: [Select]
#include <SFML/Network.hpp>
#include <iostream>

void Receive();

sf::SocketUDP Socket;

int main()
{
    std::cout << "Send or receive (0/1): ";
    int choice;
    std::cin >> choice;

    if (choice == 1)
    {
        Socket.Bind(4639);
        Socket.SetBlocking(false);

        while (true)
        {
            Receive();
        }
    }
    else
    {
        while (true)
        {
            sf::Packet Packet;
            Packet << 1;
            Socket.Send(Packet, "localhost", 4639);
        }
    }

    return EXIT_SUCCESS;
}


void Receive()
{
    sf::Packet Packet;
    sf::IPAddress IP;
    unsigned short port;

    Socket.Receive(Packet, IP, port);

    if (IP != "255.255.255.255")
    {
        int PType = -1;
        Packet >> PType;

        if (PType == -1)
        {
            std::cout << "Bad Packet!" << std::endl;
        }

        std::cout << "Packet received! IP: " << IP << " Port: " << port << " Packet Type: " << PType << std::endl;

    }
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
UDP socket
« Reply #4 on: February 16, 2012, 08:23:23 am »
Check status codes returned by socket functions.

Any code posted which doesn't do that is useless.
Laurent Gomila - SFML developer

Linuxxon

  • Newbie
  • *
  • Posts: 10
    • MSN Messenger - rasmus.linusson@gmail.com
    • View Profile
    • http://www.linuxxon.com
UDP socket
« Reply #5 on: February 16, 2012, 02:32:52 pm »
The problem does not occur when I set the socket to blocking /:

Need some time to think about this...

 

anything