What are ports and protocols really?
Solution 1:
Futher to Hello71s answer, it might help to visualise a port by thinking about the structure of an address in a packet. A packet being a unit of data passed around a network. TCP is an example of a transport layer protocol that uses ports, and is commonly used over IP.
So IP has two addressing components - the source IP and the destination IP. TCP adds to this by using a source port and a destination port. It is the ports that enables the recieving machine to differentiate traffic destined for the same IP address - ie, if you have a server that recieves both web requests and email on a single IP address, then you need to determine which application should recieve the data - the email service or the web service. So they may look like this if a single user was to carry out a web request and an email request to the same server:
Source IP Source Port Dest IP Dest Port Service
10.1.1.10 23434 192.168.1.1 80 web
10.1.1.10 34343 192.168.1.1 25 incoming email
The web service owns port 80 and the email service owns port 25 - they "listen" on their respective ports, which enables the traffic to end up in the right place.
The source port is "ephemeral" - in that is it made up at the time the packet is sent. However, it still serves a useful purpose. It enables both ends of the connection to keep track of separate conversations. Consider if our user sent two simultanous web requests:
Source IP Source Port Dest IP Dest Port Service
10.1.1.10 23232 192.168.1.1 80 web request 1
10.1.1.10 23234 192.168.1.1 80 web request 2
This lets the web service know that these are separate requests, but also, the return traffic from the webserver - the web pages - are sent back to the respective source ports, which enables the browser to know which request the server is responding to.
Note that this all refers to port numbers, from a TCP/IP perspective, the actual data being moved across these ports could be anything. It doesn't care or have any awareness of applications, so if you had web traffic on port 25 and email on port 80, it would be none-the-wiser.
It is up to the sending and receiving application to ensure the data is the right structure, and this is where application protocols come in. HTTP is an example of an application protocol that web browsers use to communicate with web servers. It is a well defined protocol that ensure that the browser will send requests to any web server and that webserver will understand and respond sensibly. But what it doesn't include in its definition is anything about how packets get from A to B - that is the responsibility of the preceding layers - the transport, internet and link layers.
Solution 2:
Although this question has already been marked as answered, I wanted to address some of the additional questions asked in the OP.
Are these ports physical objects?
No ports are not physical objects.
A port number is a 16-bit unsigned integer, which means the range of ports available for use is from 1 to 65535 (port number 0 is reserved and can't be used). A process associates its input or output channels via Internet sockets, a type of file descriptor, with a transport protocol, a port number and an IP address. This process is known as binding, and enables sending and receiving data via the network.
Are they something built into part of my computer? Are they even something physical? Or written in code? Where is this code? The operating system? What truly is a port?
The operating system's networking software has the task of transmitting outgoing data from all application ports onto the network, and forwarding arriving network packets to a process by matching the packet's IP address and port number.
Only one process may bind to a specific IP address and port combination using the same transport protocol. Common application failures, sometimes called port conflicts, occur when multiple programs attempt to bind to the same port numbers on the same IP address using the same protocol.
The above paragraph is key to understanding why ports/protocols are used in networking. If we did not have a way to specify the protocol, which transmits data over an agreed apon port number - you would not be able to do more than 1 thing at a time (check your email and use the web) because your computer would have no way to differentiate between data for your email client and data for the website you are browsing.
How many ports are there?
Port numbers are assigned in various ways, based on three ranges:
Well Known/System Ports (0-1023) - This range of ports are used by system processes that provide widely used types of network services (HTTP/80, HTTPS/443, Telnet/21, SSH/22)
Registered/User Ports (1024-49151) - The range of port numbers from 1024 to 49151 are the registered ports. They are assigned by IANA for specific service upon application by a requesting entity. (Webmin/10000, HTTP Proxy/8080, Remote Desktop Protocol/3389, etc)
Ephemeral/Dynamic/Private Ports (49152-65535) - The range 49152–65535 contains dynamic or private ports that cannot be registered with IANA. This range is used for custom or temporary purposes and for automatic allocation of ephemeral ports.
Can I increase or decrease the number of ports?
In regard to being able to increase the number of available ports, you cannot assign a port above 65535 due to the math that allows networking to work (binary) - so the answer to this question is no, you cannot increase the total number of available ports above 65535.
What are protocols?
Within computer science, a communications protocol is a system of digital rules for message exchange within or between computers. When messages are exchanged through a computer network, the rules system is called a network protocol. A protocol is basically an agreed upon set of instructions/commands/calls that two networked devices can both communicate over. Think if we didn't have agreed upon protocols and web servers just randomly sent data to web browsers that the browser did not know what to do with? Luckily we have HTTP and which every web browser created has built into the software so it can communicate with any web server that also speaks the same language (HTTP).
I'd imagine they're some sort of code.... Can you create your own protocol? What language do you use to create a protocol?
Yes you can create your own protocols. Protocols are written in a wide variety of languages. I am not a software developer, but I am pretty sure that as long as whatever language you are using has libraries that allow you to write software that can communicate over TCP/IP (there are other protocol suites, but TCP/IP is the most widely used) you can use that language to write a protocol. The programming language 'C' seems to be the most commonly used to write protocols. This is due to the fact that many of the first network protocols were developed on UNIX in the 1970's and C happens to be the language that UNIX itself is written in.
How do you get a specific port to run a specific protocol?
This differs quite a bit between OS's. For example, to change the port number Remote Desktop Protocol runs on in Windows, you need to edit the registry. On Linux, many of the network services can be configured directly from a .conf file for a particular network service.
How do you define or invent a new protocol?
See https://journal.paul.querna.org/articles/2012/02/22/designing-network-protocols/ for a blog post by someone who has recently developed a new networking protocol and what sort of things he had to do along the way.
Solution 3:
Hm. I would think that the best place to start would be to look at the IP Suite, aka the TCP/IP model. (Ignoring the other layers of the OSI model for purposes of simplicity.)
Basically, it's a series of layers:
Application Layer - HTTP, FTP, POP, SSH, etc.
Transport Layer - TCP, UDP, etc.
Internet Layer - IP, ICMP, etc.
Link Layer - Ethernet, etc.
Ports are, for the most part, implemented at the transport layer (layer 4 - Yes, the numbering is correct.)
The majority of TCP and/or UDP stacks implemented in OSes use a basic system of assigning ports to programs and simply raising events in these programs as packets come in over the network. However, any implementation could theoretically work; there may even by hypothetical alternative stacks pondered in academia.
TCP has been defined as having a total of 65535 ports, which any program can use to do whatever they want with (although many OSes limit the use of ports under 1024, giving them an ad hoc special status). Although there are some limited lists, there is no real standard for defining who gets what port and what port runs what program. A port is, thus, more or less a random number that different implementations of a program decide to agree to communicate on. Of course, the designers of such programs try to avoid ports that other popular programs have chosen already.
Granted that it's in no way required to run using TCP. Some protocols run on the bare internet layer, or even the bare link layer, mainly for purposes of efficiency, or because these protocols were invented before even TCP or IP existed. Of course, when doing so, you trade away the simplicity and the extensive bug-checking of OS networking libraries.
For more details, check the Wikipedia pages on the OSI model, TCP, and IP.