views:

343

answers:

1

What is the best way to check if client has an open port, and if it's forwarded properly?

The app works like this currently:

  1. Client creates a socket for incoming connections and wants to notify everyone about his open port. Client also tries to setup port forwarding using UPnP (but it's not always present and enabled).
  2. Client then sends his port to the "central" known server.
  3. Server should check if the port is open (forwarded properly) and return its status.
  4. If there is a forwarding problem, client will notify user.

The goal is to have an the IP + port info saved on the server to be able to give it to other clients. I could try to open a Socket from server to client and see if it fails, but is there an easier (faster) way to do it?

For example, this site does something like that: http://www.canyouseeme.org/

+1  A: 

No, there isn't.
To check if a connection can be established, you need to try to establish a connection :) Using a Socket is a sensible way of doing just that.

You could possibly skip this step for some clients, by checking if the machine has a public IP address or a local one. Still, there is the possibility of company firewalls and things like that, so trying to open a connection is the safest way to go.

Using an ICMP echo request (ping) is not an option, because ICMP is a different protocol from TCP (or UDP) that has no concept of a port number. Pinging can be used to determine if an IP address is reachable, but this is of no added value to your case.

Thorarin
So, how does this work then: http://www.canyouseeme.org/ ?
Groo
@Groo: by attempting to open a socket as you described?
Thorarin
Oh, ok, I though you said that it isn't possible at all. ;) Ok, this is not actually a problem, I thought that something like ping could be possible (to use as little resources as possible).
Groo
The way the site you linked (canyouseeme) works, it seems to do exactly the "try to open connection and see if it fails", just like you and Thorarin said.
F.Aquino
@F.Aquino: thanks for the tip.
Groo