Click here to Skip to main content
15,878,959 members
Home / Discussions / C#
   

C#

 
GeneralRe: Detecting the correct outside port Pin
harold aptroot1-Jun-09 6:01
harold aptroot1-Jun-09 6:01 
GeneralRe: Detecting the correct outside port Pin
Dave Kreskowiak1-Jun-09 6:22
mveDave Kreskowiak1-Jun-09 6:22 
GeneralRe: Detecting the correct outside port Pin
harold aptroot1-Jun-09 6:28
harold aptroot1-Jun-09 6:28 
GeneralRe: Detecting the correct outside port Pin
Dave Kreskowiak1-Jun-09 8:54
mveDave Kreskowiak1-Jun-09 8:54 
GeneralRe: Detecting the correct outside port Pin
harold aptroot1-Jun-09 8:59
harold aptroot1-Jun-09 8:59 
GeneralRe: Detecting the correct outside port Pin
Dave Kreskowiak1-Jun-09 9:13
mveDave Kreskowiak1-Jun-09 9:13 
GeneralRe: Detecting the correct outside port Pin
harold aptroot1-Jun-09 9:21
harold aptroot1-Jun-09 9:21 
GeneralRe: Detecting the correct outside port Pin
Dave Kreskowiak1-Jun-09 10:49
mveDave Kreskowiak1-Jun-09 10:49 
OK. Your client connects to the server to report itself, right? The port number that the client uses will NOT be the port number that it uses to listen for other clients connecting to it. The port number that the server sees the client using is only temporary and only good for that one connection. It the client contacts the server tomorrow to report itself, it'll probably use a different port number. You cannot use this port number for anything useful. The only important part is the server can learn the Internet visible IP address of the client. That's it.

Now, in order for the client to listen for connections from other clients, it has to open a specific "well-known" port to listen for connections on. What makes this "well-known"?? YOU! You pick the port number that your app is going to use to listen for connections. This is going to be a standardized port number that every client is going to listen on.

Great! Now you've got a port number. Well, your client software has a port number. The user's NAT doesn't know that. So, the NAT has to be configured to forward incomming connections from the public internet to a specific IP and port number on the inside of the network, preferrably the client machine(!), and the port that the client is listening on.

Now, back up a second. The NAT can forward any port number exposed to the public internet to any port number on the inside network. So, you can easily have the NAT listening for a different port number on the public internet, but usually you don't want to do this. Normally, you'd just use the same port number that the client is listening for. Just keep it in mind for troubleshooting purposes.

So, now you've got the NAT listening on your well-known port and forward traffic on that port to the client inside the network. Now, another client starts up, contacts the server to report in and get the list of currently connected clients. Your new client attempts to connect to another client, using the IP address it got from the server and the well-known port number. That request is going to get forwarded to the first client by the NAT, and any response sent back to the new client.

Now, if your clients are only supporting a single connection, you can leave it at this. If your clients are going to be able to talk to multiple clients simultaneously, you'd use this initial connection to negotiate with the calling client a second connection on a different port. The new clinet would have to start a listener on a random port, telling the first client which port that is on the initial connection port. The first client would then open a connection back to the new client to start the communication channel the two clients are going to use to chat with. The initial connection can then be closed down and the first client can then reuse the same port to listen for another connection request...


A guide to posting questions on CodeProject[^]



Dave Kreskowiak
Microsoft MVP
Visual Developer - Visual Basic
     2006, 2007, 2008




GeneralRe: Detecting the correct outside port Pin
harold aptroot1-Jun-09 11:06
harold aptroot1-Jun-09 11:06 
GeneralRe: Detecting the correct outside port Pin
Dave Kreskowiak1-Jun-09 12:01
mveDave Kreskowiak1-Jun-09 12:01 
GeneralRe: Detecting the correct outside port Pin
harold aptroot1-Jun-09 12:43
harold aptroot1-Jun-09 12:43 
GeneralRe: Detecting the correct outside port Pin
Dave Kreskowiak1-Jun-09 16:40
mveDave Kreskowiak1-Jun-09 16:40 
GeneralRe: Detecting the correct outside port Pin
harold aptroot2-Jun-09 0:26
harold aptroot2-Jun-09 0:26 
GeneralRe: Detecting the correct outside port Pin
Dave Kreskowiak2-Jun-09 1:46
mveDave Kreskowiak2-Jun-09 1:46 
GeneralRe: Detecting the correct outside port Pin
harold aptroot2-Jun-09 2:48
harold aptroot2-Jun-09 2:48 
GeneralRe: Detecting the correct outside port Pin
S. Senthil Kumar1-Jun-09 11:07
S. Senthil Kumar1-Jun-09 11:07 
GeneralRe: Detecting the correct outside port Pin
Dave Kreskowiak1-Jun-09 12:02
mveDave Kreskowiak1-Jun-09 12:02 
GeneralRe: Detecting the correct outside port Pin
harold aptroot1-Jun-09 6:48
harold aptroot1-Jun-09 6:48 
QuestionNeed Help printing DataGridView Pin
jashimu1-Jun-09 5:00
jashimu1-Jun-09 5:00 
AnswerRe: Need Help printing DataGridView Pin
0x3c01-Jun-09 5:07
0x3c01-Jun-09 5:07 
AnswerRe: Need Help printing DataGridView Pin
Henry Minute1-Jun-09 5:12
Henry Minute1-Jun-09 5:12 
QuestionAPI to control bass and treble Pin
dptalt1-Jun-09 4:34
dptalt1-Jun-09 4:34 
AnswerRe: API to control bass and treble Pin
Michael Bookatz1-Jun-09 4:43
Michael Bookatz1-Jun-09 4:43 
AnswerRe: API to control bass and treble Pin
bearfx1-Jun-09 4:44
bearfx1-Jun-09 4:44 
AnswerRe: API to control bass and treble Pin
DaveyM691-Jun-09 4:49
professionalDaveyM691-Jun-09 4:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.