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

Author Topic: Data sent over UDP gives me strange ports  (Read 2196 times)

0 Members and 1 Guest are viewing this topic.

chip2n

  • Newbie
  • *
  • Posts: 1
    • View Profile
Data sent over UDP gives me strange ports
« on: March 15, 2011, 12:08:23 am »
Hi!

SocketUPD.Receive() gives me really strange ports. Since I both send data to port 6000 and bind the socket to port 6000, shouldn't the ReceivedPort variable be set to 6000 after the Receive method?

Here's the code:

sender.cpp
Code: [Select]

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

int main()
{
    sf::IPAddress Address("127.0.0.1");
    sf::SocketUDP Socket;

    char SendMessage[] = "Yo! :P";
    char ReceivedMessage[128];

    std::size_t Received;
    sf::IPAddress Sender;
    unsigned short Port = 6000;

    if(Socket.Send(SendMessage, 6, Address, Port) != sf::Socket::Done)
        std::cout << "ERROR: Cannot send data";


    return 0;
}


receiver.cpp
Code: [Select]

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

int main()
{
    sf::IPAddress Address("193.11.222.117");
    sf::SocketUDP Socket;

    unsigned short Port = 6000;
    unsigned short ReceivedPort;

    char ReceivedMessage[128];
    std::size_t Received;
    sf::IPAddress Sender;

    Socket.Bind(Port);

    Socket.Receive(ReceivedMessage, sizeof(ReceivedMessage), Received, Sender, ReceivedPort);

    std::cout << Sender << ":" << ReceivedPort << std::endl;

    std::cout << ReceivedMessage << std::endl;

    return 0;
}


Output when ran three times:
Code: [Select]

127.0.0.1:59975
Yo! :P
127.0.0.1:38462
Yo! :P
126.0.0.1:35934
Yo! :P


This is just a test program, since I'm pretty much just starting programming with SFML. Thanks in advance!

devlin

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Data sent over UDP gives me strange ports
« Reply #1 on: March 15, 2011, 08:29:15 am »
You're only listening on port 6000 on the server.
The operating system is using a random port to send with from your client. (as it should be)

 

anything