0

Here is my understanding about port and port number (correct me if I am not correct):

  • a "port" is a communication endpoint in the transport layer

  • a "port number" is an address assigned to a port.

Is the relation between a port and a port number like that between a network interface and an IP address, in that given a port, can we change the port number assigned to it, similar to that it is possible to change the IP address assigned to a network interface?

Or is the relation more like a network interface and a MAC address, in that the MAC address of a network interface is almost never changed, more like an identity rather than an address?

pnuts
  • 58,317
  • 11
  • 87
  • 139
Tim
  • 1
  • 141
  • 372
  • 590

3 Answers3

1

I'm assuming you are asking about TCP and/or UDP ports here.

In this case, ports are 16 bit numbers which allow many applications running in your OS to all make use of protocols like TCP and UDP without interferring with each other. One application might use port 2000 to receive data on it, and another 3000. When your TCP/IP stack receives an IP packet destined for a local IP address, if it's TCP or UDP and an application has 'bound' that port number (with that local IP address, or without an IP address) the data in the packet can then be routed to the right application.

When an application makes an outgoing connection, it 'binds' a local port and uses it as the source port for outgoing TCP or UDP packets, and when the other end (the peer) responds, the reply packets come back with a destination port set to the local source port, ensuring that the replies are delivered back to the right application.

There are so called well known ports, such as port 80. Web servers generally listen on port 80, and all commonly used services usually have a well known port so that clients of those services know what port to use as a destination port when sending packets out.

To answer your question about analogies with MAC addresses... A port number is a bit like the address of a local application in your OS, in the same way that your IP is the address of your OS or computer on the internet or the MAC address of your network interface card is the address of your OS or computer on the local network (although it's really specifically the address of that network card to be completely accurate, because a computer can have multiple network cards on different physical networks).

TCP and UDP are really transport layer protocols, so you could say the ports are a communication endpoint in that respect, but they work in conjunction with the IP address (or another protocol if they are encapsulated in something else).

Michael
  • 3,639
  • 14
  • 29
  • Thanks. Is it correct to distinguish between a port and its port number, just like a network interface and its IP address or MAC address? My question is whether the port number of a port can be changed. – Tim Oct 22 '15 at 20:06
  • Do you mean there is no assignment of port numbers to ports (thinking of assignment of IP addresses to network interfaces)? Are ports born with port numbers that are fixed to them forever? – Tim Oct 22 '15 at 20:45
1

Your question is "Relation between port and port number?". In computer science when you refer to a port it has these meanings:

  • Logical port is the port number
  • Physical port is the port you find in the devices like router, switches, etc. and where the jack of a cable is inserted. For example in a router you can assign an IP address (not a port number) to a physical port.

If your doubts are about difference between IP and Port then refer to my post here What is the difference between a port and a socket?

For more information about a port number refer to my post here https://stackoverflow.com/a/52707197/2197108

Mosab Shaheen
  • 1,114
  • 10
  • 25
0

Think of an IP address as the street address of a building, and the port number as the room number in that building. Different buildings have different street addresses, but they can all have the same room numbers.

Ron Maupin
  • 6,180
  • 4
  • 29
  • 36
  • Thanks. Is it correct to distinguish between a port and its port number, just like a network interface and its IP address or MAC address? My question is whether the port number of a port can be changed. – Tim Oct 22 '15 at 20:05
  • The port numbers are 0 to 65535. You question about changing the numbers doesn't really make sense. An application decides to which port number it will respond. For instance, by default and convention, most web servers will respond to port 80 for HTTP. Most web server applications let you change that to a different port number, but you aren't changing the port number; you are changing the port to which the web server responds. Basically, the web server changes rooms in the building, but it doesn't change the room number on the room. – Ron Maupin Oct 22 '15 at 20:11
  • Port numbers are meaningless until an application reserves a port number and begins using it. All 65,536 port numbers could be sent to on a host, but, unless an application decides to use one of the port numbers, nothing happens. When you knock on the door of the room, if the room is empty, nobody answers. It's only when an application occupies the room does a port number mean anything. A port number is merely a concept to let traffic flow to a particular application which has reserved that port number. – Ron Maupin Oct 22 '15 at 20:17
  • Do you mean there is no assignment of port numbers to ports (thinking of assignment of IP addresses to network interfaces)? Are ports born with port numbers that are fixed to them forever? – Tim Oct 22 '15 at 20:43
  • Port numbers aren't born until an application requests, from the transport layer, the use of a port number. A port number dies when the application release the port number back to the transport layer. Port numbers are just concepts: they don't actually exist anywhere, but they are referenced by a layer-4 segment in its header. It's just a convenient way for a sender to contact the receiving application which has reserved that port number. – Ron Maupin Oct 22 '15 at 20:47
  • Port numbers are also assigned per transport layer protocol. The two most used transport layer protocols are TCP and UDP. for Instance, TCP port 12345 could be reserved from TCP by Application X, and UDP port 12345 (same port number, different transport protocol) could be reserved by Application Y. Some transport layer protocols don't use port numbers. – Ron Maupin Oct 22 '15 at 20:50
  • By assignment, we mean different things. I mean assign port numbers to ports, not yet assign port (or port numbers) to applications. – Tim Oct 22 '15 at 20:56
  • What I'm saying is that ports don't actually exist; they are just concepts for the connection of the transport layer to an application. You can't assign a port number to a port because there is no port. An application will request of the transport layer protocol that any segment coming is addressed to a port number be sent to that application. Until the application requests that, any traffic addressed to that port number will be dropped. The same thing happens after the application releases the port number. – Ron Maupin Oct 22 '15 at 21:01