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

Author Topic: [SOLVED]Difference between remote port and bind port  (Read 2347 times)

0 Members and 1 Guest are viewing this topic.

DashWave

  • Newbie
  • *
  • Posts: 9
    • View Profile
[SOLVED]Difference between remote port and bind port
« on: December 15, 2013, 01:46:44 am »
I'm trying to implement UDP hole punching to allow peer-to-peer connections but I don't understand exactly how UDP endpoints work. Specifically, what is the difference between the the_port in

sf::Packet packet;
sf::IpAddress ip;
unsigned short the_port;
socket.receive(packet, ip, the_port);
 

and

socket.bind(the_port);
 
« Last Edit: December 15, 2013, 02:23:32 am by DashWave »

DashWave

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Difference between remote port and bind port
« Reply #1 on: December 15, 2013, 02:03:56 am »
After researching I found the following on Wikipedia:
Quote
Source port number
This field identifies the sender's port when meaningful and should be assumed to be the port to reply to if needed. If not used, then it should be zero. If the source host is the client, the port number is likely to be an ephemeral port number. If the source host is the server, the port number is likely to be a well-known port number

I still don't quite understand the concept of the source port though. If the receiver is to reply to the sender using the source port then surely the sender must have already bound to the source port? I do not see any way of getting/setting the source port when sending something using SFML.

Edit: Okay so after reading the documentation like I should have done in the first place I found that the source port is whatever port the sender's socket was bound to. I'm not quite sure as to the behaviour when the sender's socket is not bound but it shouldn't matter as the receiver cannot respond to a sender that is not bound.
« Last Edit: December 15, 2013, 02:12:08 am by DashWave »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [SOLVED]Difference between remote port and bind port
« Reply #2 on: December 15, 2013, 08:23:35 am »
When you send without binding the socket first, it is automatically bound to a free port by the OS.
Laurent Gomila - SFML developer

 

anything