How to find outgoing port number?

Hello all,

I have a web application uses a tcp socket to connect to an outside server (of mine). How can I tell the outside server, what Port number of the tcp socket has been assigned, by the router, on the tcp socket end?

I am trying to create a tcp hole punch.
Thank you,

You shouldn’t need to know. Your socket is listening on port x and the hole is punched for that port only.

Hi Wayne,

I understand that ComputerA tcp socket has to connect to the outside world on ComputerB. Then ComputerC connects to computer B who then hands off ComputerC to Computer A but on the same port that ComputerA made its outgoing connection to Computer B on.

ComputerA is normally listening on port 80 or 8080. But the router has to route ComputerC request to A and only knows to do that if C comes back on the same port as the outgoing A…

Have I got that all wrong?

I think I see what you’re trying to do, I just can’t see how you can achieve the result you’re looking for. You may need to pass the Session ID from A to B who passes it to C to reconnect back to A using that Session ID.

Well, there’s no session. The session is created only when ComputerC actually gets to the listener of ComputerA.

I have the APP of ComputerA connecting to ComputerB.
Wikipedialink text

I tried first with UPnP, but although easier, terribly unreliable.
Thank you for your input as usual Wayne!

@Tim Seyfarth
I’ve just read this thread 3 times and am thoroughly confused. You seem to be using Computer B and “Router” in your statements interchangeably, but what that middle device is determines how you might solve this.

FWIW, the port numbers of an outgoing connection rarely has anything that do with the ports for incoming connections (unless you’re doing something like ftp), so I don’t think what You’re describing gets you what you want.

If you’re already creating a tcpsocket from Computer A to Computer C, why not just use that as a 2-way communication channel?

I don’t think Xojo exposes the remote port, just the RemoteAddress. You may need a feature request.

Hi Greg.

Computer B is not the router, it is a Computer on the internet, on a separate network (hosted at my office). Computer A is a web app that can be anywhere in the world and is behind a NAT. Computer C is another computer that a user is trying to connect to Computer A and is behind yet another NAT.

Without setting up port forwarding on ComputerA’s network, Computer C cannot access it, even with port 80, or 8080 since the router (on the ComputerC’s newtwork) has no way to know where to point it to. Hence the reason to create a TCP Hole punch.

Here’s another link that explains what I am trying to do I wish it was simpler. UPnP does not work reliably - at least for me and one other user of Xojo, maybe someone else has gotten it to work?

Any alternate solutions would be appreciated!

One more question… are all of these computers controlled by you or your company? If so, you might consider something like ZeroTier which bypasses all of this stuff.

Hi Greg.
The only computer controlled from us, is the Server - Computer B.

All of this is done to avoid having to set up port forwarding. Its an effort to emulate how Team Viewer and others do the same thing.