How to get a free socket port? C ++

I am writing a test client / UDP server and I want to receive it through a firewall. Presumably, all I need to do is send both sides to the correct IP address and server. Obtaining an IP address is not a problem, but how can I choose a client free port and report this to the user? Ultimately, I would like him to connect to the matchmaking server, but right now I need a simple working prototype, and I would like to specify the port number so that my friend / tester can send me # via IM so that we can test.

How to get the port number? sorry for the long desc. I notice that people tell me not to do what I ask, when I do not give desc :(

+4
source share
6 answers

To use a highly technical term, this is actually a pretty nasty problem or even a couple of nasty problems. Depending on the configuration of the firewall, it usually allows you to respond to requests from another endpoint on the IP endpoint as the request arrives. So ... if your friend gets the UDP datagram using something like the recvfrom() system call, the address parameter will get the IP endpoint information for the response. Therefore, the other end should be able to respond using sendto() using the same addressing information. Sort of:

 /* initiator */ struct sockaddr_in hisaddr; memset(&hisaddr, 0, sizeof(hisaddr)); hisaddr.sin_addr.s_addr = htonl(target_ip); hisaddr.sin_port = htons(target_port); sendto(sd, msg_ptr, msg_sz, 0, (struct sockaddr*)&hisaddr, sizeof(hisaddr)); /* receiver */ struct sockaddr_in peeraddr; socklen_t peer_sz = sizeof(peeraddr); recvfrom(sd, buf_ptr, buf_sz, 0, (struct sockaddr*)&peeraddr, &peer_sz); /* build response */ sendto(sd, msg_ptr, msg_sz, 0, (struct sockaddr*)&peeraddr, peer_sz); 

peeraddr on the other hand, will be your external address, or rather, the IP address of your firewall and the port number that he decided to use. The port number that you specified in the code may be completely different than the port to which your friend should send. Ultimately, it doesn't matter which port you are going to use, since the firewall can send and receive on a completely different port - this is what Network Address Translation is all about. I would recommend reading RFC3235 for some tips on how to overcome this obstacle.

IMHO's best approach is as follows:

  • Let the OS select the port by calling bind() with a zero port number or skipping the binding altogether
  • When the client receives address information from the socket level (for example, the fifth and sixth arguments to recvfrom() )
  • The client sends a response to the endpoint obtained in the previous step.
  • Change the firewall settings until the previous steps are completed

Of course, all the magic is at the last step. If you can disable NAT or make sure that the firewall will never switch ports, then smoothing the port number and bind ing will work. You can take a look at %WINDIR%\system32\drivers\etc\services (or /etc/services depending on the tilt of your OS) to find out which port numbers are reserved or even used.

+5
source

Generally speaking, you - as a developer - choose a port. You can configure the application to read the port from the configuration file or user input, but no magic firewall will tell you which port to use ...

0
source

If I understand your question correctly, I’m not sure that there is a way to do what you want programmatically (and even if there is, I don’t think this is the right approach). I think you need to find a port that is not used on the server machine (and maybe another or the same port on the client machine, if the connection is bidirectional) And this port should be able to go through your firewall, I assume that since you say that “getting an IP address is not a problem”, have you already set up a firewall to forward some or all of the ports to a specific computer inside the firewall? If so, the port you are looking for is one of the ports you sent. You can simply select arbitrary if no other service is running on this port. Ports below 1024 are reserved, so you probably want to choose a larger number than this. You can use a simple port scan tool, such as nmap , to see which services are running on your computer, on which ports, and select another. Note that nmap can be tricked by firewalls and various bind rules when creating sockets.

0
source

I think you'd better choose a fixed port rather than relying on a random port number chosen by O / S.

If you are using a random port, you will have to change the firewall settings each time the program starts.

0
source

If you use WINSOCK, check this link: http://msdn.microsoft.com/en-us/library/aa280717(VS.60).aspx Basically you have 2 options that set the port to 0 and allow the system to assign you one or choose a random attempt to open a socket, if it doesn’t work, try another one (be sure to avoid reserved ports)

0
source

bind () socket before sending your data. Specify port 0 for binding (), and the OS will select an unused port for you. Then you can use getsockname () to find out which port was selected.

0
source

All Articles