The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: RedXVII on June 29, 2006, 11:33:58 PM

Title: Winsock - port numbers
Post by: RedXVII on June 29, 2006, 11:33:58 PM
Can anyone tell me the point behind port numbers. I dont get it, Why do we have them and what purpose do the serve? Why not just connect to an IP with a programID/program name or something?
Title: Re: Winsock - port numbers
Post by: drhowarddrfine on June 29, 2006, 11:59:53 PM
Each port number can serve a different purpose.  It's almost like selecting a different directory using the same IP address.  Web servers listen on port 80.  FTP is done with a different port number and on and on.  You would never want an outside service accessing programs on your computer, would you? 

Now nothing stops your program from listening and accepting everything that comes in to your IP address, no matter the port number. 
Title: Re: Winsock - port numbers
Post by: gwapo on June 30, 2006, 02:48:31 AM
Well, you can think of it as a busy highway, information goes in and out, and you can think "Port number" as the lanes in this busy highway. Your information can go safely to its destination if it is using the correct lane, otherwise, without lanes, there will be unimaginable problems, and sorts of bottleneck.

\
-chris
Title: Re: Winsock - port numbers
Post by: zooba on June 30, 2006, 08:14:56 AM
Also, imagine the uproar if Microsoft based servers would only accept connections from 'x.x.x.x:Internet Explorer' :bg

Basically, they let programs communicate without having to know what's at the other end.
Title: Re: Winsock - port numbers
Post by: Tedd on June 30, 2006, 12:00:56 PM
Quote from: RedXVII on June 29, 2006, 11:33:58 PM
Can anyone tell me the point behind port numbers. I dont get it, Why do we have them and what purpose do the serve? Why not just connect to an IP with a programID/program name or something?
You've answered your own question :P Port numbers almost 'are' the program-ID/name or something :bg More correctly, they're used as service identifiers, so you can use different programs for the same purpose (there isn't only one type of web-browser/mail-client/etc).
The real point of ports is that each connection your computer makes/receives must be separated, otherwise they'd just get mixed up -- how would you know which program wants which data? (Okay, you could use program IDs, but they're called ports in this case :wink) So, each connection is differentiated by the port it communicates on; and also the address it's connected to, but you could connect to the same address twice (as long as they're on different ports then it's okay.)
Title: Re: Winsock - port numbers
Post by: ToutEnMasm on June 30, 2006, 01:21:25 PM

Port number for use with .................
110 is POP3,109 is pop2 (email server).Another use , others ports , refers to the norm of communication.
Why it is like that ?,it is ........
                    ToutEnMasm


Title: Re: Winsock - port numbers
Post by: drhowarddrfine on June 30, 2006, 07:52:08 PM
I forgot the background but those are called "well known ports" and are reserved for those programs. 
Title: Re: Winsock - port numbers
Post by: Mark Jones on July 01, 2006, 02:07:03 AM
There are quite a few de-facto "standard" ports. 21 is for FTP, 22 is SSH+FTP, 80-83 are HTTP, 443/500 are HTTPS, 1755/7070 for streaming video, etc. ANY data can be sent on ANY port - but both parties must decide on a specific (open) port before communicating.

Technically, think of ports as a post-IP routing filter or subdivision qualifier. Like say 123.80.24.1:80 is JoeBloe's website and 123.80.24.1:21 is his FTP. His HTTP server listens on port 80 while his FTP server listens on port 21, but both softwares are present at his same physical IP. The idea of "ports" allows up to 64k connectable "services" on a single IP - something definately required for serving webpages to multiple users at once! :8)
Title: Re: Winsock - port numbers
Post by: RedXVII on July 02, 2006, 05:27:01 PM
Oh right, i think i get it. Handy,

But i was experimenting and i came accross something confusing, This might get a bit complicated but bear with me.

I made a small program where I make a server and my mate connects to it. We send text to each other then disconnect. I had a server on (bind + listen) port 1999 and my mate connected to that port, using that port number. I then accepted the incoming connection, so i have a new socket, a connection from his to mine. I then looked at my firewall to see what was going on. I had a connection from me:1999 to him:4632. To me, this doesnt make sense, 2 questions:

1) Dont i already have a server on port 1999, so why do i have connection on it aswell. I thaught this wasnt allowed.

2) I thaught i specified port 1999, why did it randomly allocate port 4632 for our connection?
Title: Re: Winsock - port numbers
Post by: drhowarddrfine on July 02, 2006, 08:30:29 PM
When you are a server, there are two ports.  One is the listening port and the other is the connection port.  Once you connect, you could delete the listening port but, of course, if you serve more than one client you want to keep the listening port open.
Title: Re: Winsock - port numbers
Post by: Tedd on July 04, 2006, 12:06:37 PM
Quote from: RedXVII on July 02, 2006, 05:27:01 PM
I made a small program where I make a server and my mate connects to it. We send text to each other then disconnect. I had a server on (bind + listen) port 1999 and my mate connected to that port, using that port number. I then accepted the incoming connection, so i have a new socket, a connection from his to mine. I then looked at my firewall to see what was going on. I had a connection from me:1999 to him:4632. To me, this doesnt make sense, 2 questions:

1) Dont i already have a server on port 1999, so why do i have connection on it aswell. I thaught this wasnt allowed.

2) I thaught i specified port 1999, why did it randomly allocate port 4632 for our connection?

It's okay, it's a bit puzzling at first. If you say connect to your mate on port 123, then this is what will happen -- a connection will be created to your mate's machine on port 123, and he will receive your connection on port 123. BUT, your side of the connection is not on port 123, because you asked for the 'other' end to be port 123, so your side opens the connection through a currently unused port.
This is a good thing because then you can open different connections to different machines on the same port (for the 'other' end) e.g. open 5 web-pages on 5 different web-sites, you wouldn't want to block port 80 on your machine with only a single web-site.
So, if you connect to your mate on port 123, and your mate connects to you on port 123, then it's all fine - but this is two different connections, so there's no real need to do that. However, if you really want to say which port to open the connection (from your side) then you can "bind" the sockets before connecting :wink (In the same way you bind before "listen"ing)