عناوين IP والمنافذ
في هذا الدرس 11 موضوعات
هذا الدرس متاح حاليًا باللغة الإنجليزية فقط، ولم تُنشر نسخته العربية بعد.
Lesson Overview
For one computer to talk to another across the internet, it must know which computer and which program on that computer. The first answer is an IP address. The second is a port.
Think of a large apartment building. The street address gets the postman to the building. The door number gets the parcel to the right flat. An IP address is the street address, and a port is the door number. The most common confusion in networking is mixing these two up, so this lesson spends time keeping them apart.
Why this matters: when you run your own backend service, you will tell it which port to listen on. When you connect to a database, you will write an address and a port. When something fails to connect, "wrong address" and "wrong port" are two of the first things you will check.
This lesson builds on Lesson 1 (the internet and protocols) and on BEG-001 (processes, which are programs that are running). You need no maths beyond counting.
Learning Objectives
By the end of this lesson you will be able to:
- Explain what an IP address is and what it is used for.
- Recognise an IPv4 address and describe the idea of an IPv6 address.
- Tell a public address from a private one, and say what
localhostmeans. - Explain what a port is and why one computer needs many of them.
- State the default ports for HTTP (80) and HTTPS (443).
- Read an address with a port, such as
192.0.2.10:8080, and say which part is which. - Explain why an IP address plus a port identifies one specific service on one specific computer.
Written Explanation
What is an IP address?
An IP address is a number that identifies a device on a network that uses the Internet Protocol (IP). MDN's glossary defines it as a number used to give each device on an IP network a unique address. Without addresses, data would have no destination.
Every packet you met in Lesson 1 carries two addresses in its header: where it came from and where it is going. Routers along the way read the destination address and pass the packet onward, like postal sorters reading the street name.
IPv4 addresses
The older and still very common format is IPv4. An IPv4 address is a 32-bit number. You almost never see it in binary (BEG-001-L03). Instead it is written as four numbers separated by dots, each between 0 and 255:
192.0.2.10Why 0 to 255? Each of the four numbers is one byte (8 bits), and a byte can hold 256 different values. Four bytes give 32 bits in total. That is about 4.3 billion possible addresses, which turned out not to be enough for a world full of phones, laptops, and sensors.
The addresses 192.0.2.0 to 192.0.2.255 are a special case: they are reserved for use in documentation and examples, so you can safely use them in teaching. This lesson uses them, which means they are illustrative: they do not belong to any real server.
IPv6 addresses
To solve the shortage, a newer format called IPv6 uses 128 bits. It is written as groups of hexadecimal digits separated by colons:
2001:db8:85a3::8a2e:370:7334You do not have to learn hexadecimal to understand the idea. IPv6 simply provides vastly more addresses. Two rules shorten it: leading zeros in a group can be dropped, and one run of all-zero groups can be replaced with ::. The address above uses the second rule. The prefix 2001:db8::/32 is reserved for documentation, like the 192.0.2.x block.
Both formats are in use today. A single domain can have both an IPv4 and an IPv6 address, and your computer picks whichever works.
Public and private addresses
Not every address is reachable from the whole internet.
- A public address is unique across the internet. Servers that serve the public have these.
- A private address is used inside a local network such as your home Wi-Fi or an office. The common private IPv4 ranges are
10.x.x.x,172.16.x.xto172.31.x.x, and192.168.x.x. Many homes have devices numbered192.168.1.x.
Your home router typically has one public address facing the internet and gives each device a private one inside. When your phone fetches a page, the router translates between the two (a technique called NAT, network address translation). You do not need to configure this, but it explains why the address shown in your phone's Wi-Fi settings and the address a website sees can be different.
The special address for "this computer"
The address 127.0.0.1 always means "this computer itself". It is called the loopback address, and its usual name is localhost. A program that connects to localhost talks to another program on the same machine, without leaving it. Developers use it constantly: when you start a web service on your laptop to test it, you open localhost in a browser.
For example, the address http://localhost:8080 means: this computer, port 8080.
What is a port?
One computer usually runs many network programs at once. A web server, a database, and a mail program may all live on the same machine, and they may all be waiting for connections. If the IP address only names the computer, how do incoming packets reach the right program?
A port is a number that picks one communication endpoint on a computer. MDN's glossary calls a port a communication endpoint designated by a number. Each program that wants to receive connections listens on a port, and each incoming connection says which port it wants.
Port numbers are whole numbers from 0 to 65535 (16 bits). The Internet Assigned Numbers Authority (IANA) divides them into ranges:
| Range | Name | Typical use |
|---|---|---|
| 0 to 1023 | System (well-known) ports | Standard services such as web and email |
| 1024 to 49151 | User (registered) ports | Applications and databases |
| 49152 to 65535 | Dynamic or private | Temporary use by clients |
The famous ones for this course:
| Port | Service |
|---|---|
| 80 | HTTP |
| 443 | HTTPS |
| 25 | SMTP (email between servers) |
IANA's registry confirms that port 443 is HTTPS, and MDN states that the defaults are 80 for HTTP and 443 for HTTPS.
Developer-made services commonly use higher numbers such as 3000 or 8080, which are conventions, not rules. A backend you write in a later module will probably start on a port like 8080 so that it does not need special permissions.
Putting address and port together
When you want to reach a service, you give both:
192.0.2.10:8080
| |
| +-- port: which service on that computer
+------------- IP address: which computerThe colon separates them. You write the address first, then a colon, then the port.
In a URL, the port comes after the host:
https://example.com:8443/pathIf the port is left out, the browser uses the default for the scheme: 80 for http and 443 for https. That is why https://example.com/ and https://example.com:443/ mean the same thing, and why you rarely see a port in everyday URLs.
The apartment building, again
| Building | Network |
|---|---|
| The building's street address | The IP address |
| Door number of a flat | The port |
| The person who lives in the flat | The program listening on that port |
| The postman | The routers that carry packets |
Two things follow from the analogy:
- Two different flats in the same building (two programs on one computer) have the same street address but different door numbers.
- The same door number in a different building (port 80 on another server) is a completely different place.
That is why it takes both parts to name a service.
A peek at the sender's side
The sender also has an address and a port. When your browser connects to a server on port 443, your computer picks a temporary port for its own side so that replies find their way back to the right program on your machine. You rarely see this, but it is why many browser tabs can talk to the same server at once without their answers getting mixed up.
Examples
Example 1: Reading an address (simple)
Read this: 192.0.2.45:3306.
- IP address:
192.0.2.45, the computer. - Port:
3306, the door on that computer.
Port 3306 is commonly associated with the MySQL database in its documentation, so a reader might guess a database is waiting there. Treat that as a hint, not a guarantee: any program can listen on any free port.
Example 2: Two services, one computer (practical)
A developer's laptop runs two programs at the same time:
- A web service waiting on port 8080.
- A second web service waiting on port 9090.
The laptop has a single address on the network, say 192.168.1.20. From another device in the same house:
192.168.1.20:8080reaches the first service.192.168.1.20:9090reaches the second service.
The computer is identical in both cases. Only the port changed, and so did the answer. If both programs tried to listen on 8080, the second would typically fail to start because the port is already in use.
Example 3: Debugging "connection refused" (realistic)
A team deploys a backend service to a server whose address is 192.0.2.10. From a browser the developer visits http://192.0.2.10/ and gets an error that the connection was refused. The developer reasons:
- The address reached the computer, because the computer answered with a refusal rather than silence. (The reasoning is a hint, not proof, since some systems silently drop traffic instead.)
- The browser used port 80, because the URL used
httpwith no port. - The service was started on port 8080, not 80.
The fix is to use the right port: http://192.0.2.10:8080/. The address was correct all along. The mistake was the door number. Many "it doesn't work" problems turn out to be exactly this.
Backend Insight
Every backend service you write has two network settings that matter on day one: which port it listens on and which addresses it accepts connections from. When you start a development server you will see something like "listening on port 8080". In production, a front door (a web server or load balancer) typically answers on 80 and 443 and passes requests to your service on its private port.
Other facts worth remembering:
- Only one program can listen on a given port on a given address at a time. Two backends cannot both use 8080 on the same machine.
- Firewalls decide which ports are reachable from outside. A service can be running fine yet unreachable because the port is blocked.
- Databases and caches have their own conventional ports. When you configure them later you will write the host address and the port together.
localhostis private to your machine. A friend cannot visit yourlocalhost. They need your real address, and your network must allow it.
Treat "address plus port" as the unit of reachability. It will return in HTTP, databases, containers, and cloud networking.
Common Mistakes
Mistake 1: Confusing the IP address with the port
- Incorrect: "The port is
192.0.2.10." - Correct: "The IP address is
192.0.2.10and the port is8080."
The address picks the computer. The port picks the program.
Mistake 2: Assuming a port number means a particular service
Port 80 normally carries HTTP, but that is a convention. A program can listen on any port, and an unusual service can use 80. Check what actually runs there before trusting the number.
Mistake 3: Thinking localhost is reachable by anyone
localhost means "this computer". On a friend's machine it means their computer. Share a real address if you want others to reach your service.
Mistake 4: Forgetting the default port exists
If a URL has no port, the browser still uses one: 80 for http, 443 for https. If your service is on 8080 you must include :8080.
Mistake 5: Believing private addresses are unique worldwide
Thousands of homes use 192.168.1.x. A private address only makes sense inside its own network.
Exercises
Try each exercise yourself before opening the solutions.
Level A: Foundation
A1. In the address 192.0.2.7:9000, which part is the IP address and which is the port?
أظهر الحل A1
A1. The IP address is 192.0.2.7. The port is 9000.
A2. What are the default ports for HTTP and HTTPS?
أظهر الحل A2
A2. HTTP uses port 80. HTTPS uses port 443.
A3. What does localhost (or 127.0.0.1) refer to?
أظهر الحل A3
A3. It refers to the same computer you are using. Connections to it never leave your machine.
Level B: Application
B1. A computer runs a web service on port 8080 and a database on port 5432. Both are reached at the address 192.0.2.30. Write the two address-and-port pairs.
أظهر الحل B1
B1. 192.0.2.30:8080 for the web service and 192.0.2.30:5432 for the database.
B2. The URL https://shop.example.com/ has no port. Which port will the browser use, and why?
أظهر الحل B2
B2. Port 443, because the scheme is https and the browser uses the scheme's default port when none is written.
B3. Why can a computer run many network programs even though it has only one IP address?
أظهر الحل B3
B3. The IP address identifies the computer, and ports identify which program on it should receive each connection. Each program listens on its own port, so many can share one address.
Level C: Challenge
C1. You start a second copy of your backend on the same port as the first and it fails to start. Explain why, and name two ways to fix it.
أظهر الحل C1
C1. Only one program can listen on a given port at a time, so the second cannot take the port. Fixes: stop the first copy, or start the second copy on a different port (for example 8081).
C2. Two flats in the building analogy have the same door number but belong to different buildings. Translate this situation into network terms, and say whether the two are the same service.
أظهر الحل C2
C2. It is the same port number on two different IP addresses (two different computers). They are not the same service. The address-and-port pair is what identifies a service, and the addresses differ.
Mini Challenge
Practise reading addresses and ports.
- Write five address-and-port pairs of your own, using only the documentation block
192.0.2.xand ports of your choice, for example for a web service, a second web service, and a database. - Next to each, write which part is the computer and which part is the program.
- Write one URL for each of
httpandhttpsthat uses a non-default port, and one URL for each that uses the default port without writing it. - Find out, without changing any settings, whether your computer lists a private address on your Wi-Fi. The place to look differs by operating system and version, so search for "find my IP address" for your system. Write down only the kind of address (private or public), not the number, if you prefer not to record it.
Check your work: your first list has no address without a port, and the default-port URLs omit the port while the others show it.
Summary & Next Lesson
Key takeaways
- An IP address identifies a device on a network. IPv4 has four numbers from 0 to 255 separated by dots. IPv6 is longer and written in hexadecimal with colons.
- Private addresses work inside a local network. Public addresses are unique on the internet.
127.0.0.1orlocalhostmeans "this computer". - A port (0 to 65535) selects which program on a computer receives a connection.
- HTTP defaults to port 80; HTTPS defaults to port 443.
- Address plus port, such as
192.0.2.10:8080, identifies one specific service on one specific computer. - Do not mix up the address and the port.
Next lesson
Typing 192.0.2.10:8080 is hard to remember. The next lesson, "Domains and DNS", explains how names like example.com stand in for addresses and how the internet's phone book finds them.
Self-check
- What does an IP address identify?
- What does a port identify?
- What are the default ports for HTTP and HTTPS?
- In
192.0.2.5:80, what is80? - Can two programs listen on the same port of one computer?
أظهر الإجابات
- A device (a computer) on a network.
- Which program (service) on that computer should receive the connection.
- 80 for HTTP and 443 for HTTPS.
- The port number, the standard HTTP port.
- Not at the same time on the same address. Only one program can listen on a given port at a time.
References
- Mozilla Developer Network, IP address (glossary). Supports the definition of an IP address as a number that uniquely addresses a device on an IP network. The details on IPv4 and IPv6 notation, private ranges, NAT, and the documentation blocks are standard networking background that was not on that page.
- Mozilla Developer Network, Port (glossary). Supports the definition of a port as a numbered communication endpoint and the defaults of 80 for HTTP, 443 for HTTPS, and 25 for SMTP.
- Mozilla Developer Network, Authority (URL). Supports how the port sits after the host in a URL and that omitted ports default to 80 or 443.
- IANA, Service Name and Transport Protocol Port Number Registry. Supports that port 443 is registered to HTTPS and the three port ranges. The meaning of port 3306 in Example 1 and the "connection refused" reasoning are hedged because they were not verified here.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.