Hello dev, welcome 👋. I couldn't find an article that directly addresses this topic, so I decided to write one myself. Dear devs, I hope you find it helpful and enjoyable. I believe you will, as it has been very useful to me, and we are colleagues after all.
I will cover basic network concepts and knowledge here. It's not overly advanced. If a network expert reads this, they might think it's very basic. However, as Cantinflas would say, that's the point. This article is aimed at software developers, not network experts. Amazingly, many developers, even at the senior level, don't know half of what I'm about to share. However, as I always say, let's be pragmatic, acknowledge our shortcomings, and work on them.
After understanding this article, you'll feel more comfortable working with ports and talking about protocols. The confidence and security that come from knowing the background of something are invaluable. Knowing these topics is what makes you a thorough developer and contributes to the engineering knowledge that a software engineer should have. Let's begin!
Why is it important?
As a software developer, it's essential to have basic knowledge about ports and network protocols, especially if you're working on applications that interact with networks or online services.
Integration with services or APIs Many modern applications communicate with external services over the network using different protocols and ports. For example, integrating a payment gateway (which might use HTTPS over port 443) or an API (which could use HTTP over port 80).
Security Understanding which ports and protocols your applications use is crucial for correctly configuring firewall rules and ensuring secure and efficient communications on your server.
For example, if you have a VPS server where you will host your application, you need to know how to minimally configure the firewall to avoid attacks.
In this entry, you can find out how to configure the firewall on an Ubuntu server with BASH.
Optimization Knowing which protocols and ports each service or application uses allows you to optimize your application's performance by choosing the most efficient and appropriate communication methods for your specific needs. This is part of the difference between an amateur and a professional.
Network troubleshooting In situations where your application faces connectivity or performance issues, understanding how ports and protocols work will help you diagnose and resolve network problems more effectively.
Compliance with standards and best practices Finally, when developing applications that interact with networks, it's important to follow industry best practices and standards regarding protocol and port usage to ensure interoperability and security.
This will also allow you to understand what you're talking about and, of course, increase your professionalism when working.
The OSI Model
The OSI (Open Systems Interconnection) Model is a conceptual framework that defines how computer systems should communicate with each other. It is divided into layers representing specific network communication functions, from physical connection to application interaction.
The OSI model divides network communication functions into seven distinct layers, each responsible for specific functions that contribute to data exchange between computer systems.
Dear dev, here I will break down each layer and provide some examples of protocols found in each:
Physical Layer: This layer deals with the transmission and reception of data at the physical level. It defines the electrical, mechanical, and functional characteristics of network devices.
- Examples:
- Ethernet: A widely used wired network protocol.
- Wi-Fi (IEEE 802.11): A protocol for wireless networks.
- Fiber Optics (SONET/SDH): A data transmission technology using light, increasingly replacing old ADSL connections.
Data Link Layer: Handles reliable data transfer across a physical link.
- Examples:
- Ethernet (IEEE 802.3): A common protocol for wired networks.
- Wi-Fi (IEEE 802.11): Operates in this layer for wireless networks as well.
- PPP (Point-to-Point Protocol): Used for direct point-to-point connections.
- HDLC (High-Level Data Link Control): A bit-oriented data link protocol.
Network Layer: Responsible for routing data packets through the network.
- Examples:
- IP (Internet Protocol): THE PROTOCOL for addressing and routing packets on the Internet.
- ICMP (Internet Control Message Protocol): Used for network diagnostics.
- ARP (Address Resolution Protocol): Maps IP addresses to physical (MAC) addresses.
- OSPF (Open Shortest Path First) and BGP (Border Gateway Protocol): Routing protocols.
Transport Layer: Provides end-to-end communication between devices on the network.
- Examples:
- TCP (Transmission Control Protocol): Ensures ordered and reliable data delivery.
- UDP (User Datagram Protocol): Provides a simple connectionless transfer method.
- SCTP (Stream Control Transmission Protocol): Supports both reliable and unreliable data transmission.
Session Layer: Establishes, manages, and terminates sessions between network applications.
- Examples:
- NetBIOS (Network Basic Input/Output System): Provides session services to network applications.
- RPC (Remote Procedure Call): Allows a program to request a service from a program on another computer in a network.
- SIP (Session Initiation Protocol): Used to establish, modify, and terminate multimedia sessions.
Presentation Layer: Handles data representation, syntax, and encryption.
- Examples:
- SSL/TLS (Secure Sockets Layer/Transport Layer Security): Provides secure communications over the Internet.
- JPEG (Joint Photographic Experts Group): A file format for images.
- ASCII (American Standard Code for Information Interchange): Standard character encoding.
Application Layer: The layer closest to the end user, providing network services directly accessible to applications.
- Examples:
- HTTP (Hypertext Transfer Protocol): Used for transferring hypertext on the World Wide Web.
- FTP (File Transfer Protocol): Used for transferring files.
- SMTP (Simple Mail Transfer Protocol): Standard protocol for sending email.
- DNS (Domain Name System): Translates domain names to IP addresses.
- SNMP (Simple Network Management Protocol): Used for managing network devices.
Protocols
A protocol is a set of rules and standards that allow communication between devices on a network. These protocols define how data should be sent, received, and processed, ensuring that devices can understand and communicate effectively.
Protocols are essential for interoperability and communication between different devices and networks. Without them, it would not be possible to establish reliable connections or ensure that data is interpreted correctly at each end of the communication.
Types of Network Protocols There are many protocols used in communication networks, each with a specific purpose. Some of the most important are:
- Transmission Control Protocol (TCP): A connection-oriented protocol that guarantees the ordered and reliable delivery of a sequence of bytes from one program on a computer to another program on another computer.
- Internet Protocol (IP): Responsible for addressing and routing data packets between devices on different networks.
- Hypertext Transfer Protocol (HTTP/HTTPS): Used for transmitting hypermedia documents, such as web pages, on the World Wide Web.
- File Transfer Protocol (FTP): A standard protocol used for transferring files between a client and a server on a network.
- Simple Mail Transfer Protocol (SMTP): Used for the transfer of email between mail servers.
- Address Resolution Protocol (ARP): Used to map an IP address to a physical hardware address in a local network.
Example of Communication with Protocols When a user accesses a website, several protocols work together to make communication possible:
- DNS (Domain Name System) translates the website's domain name into an IP address.
- TCP/IP establishes the connection and ensures that data is sent reliably between the client and the server.
- HTTP/HTTPS handles the request and response of web pages.
The HTTP Protocol
Given its importance, I will delve deeper into this particular protocol.
The Hypertext Transfer Protocol (HTTP) is a simple client-server protocol that articulates information exchanges between web clients and HTTP servers.
It consists of three fundamental elements:
- HTTP Request
- HTTP Response
- HTTP Methods
Http Request
This is the message the client sends to the server using the HTTP protocol.
It consists of three elements: Request line, header, and optional body.
Http Response
This is the message the server sends back in response to a request from the client, also using the HTTP protocol.
It also consists of three parts: Status line or status code, header, and optional body.
HTTP status codes are responses issued by a server in response to a client's HTTP request (such as a web browser or application). These codes indicate the outcome of the request and are divided into five main categories, each represented by a range of numbers.
Here is a brief explanation of each category ✌️:
- 1xx (Informational):
- 100 Continue: The server has received the initial request, and the client can continue with its request.
- 101 Switching Protocols: The server accepts switching to a different protocol as requested by the client.
- 2xx (Success):
- 200 OK: The request was successful, and the server returned the requested data.
- 201 Created: The request was successful, and a new resource was created.
- 204 No Content: The request was successful, but the server did not return any content.
- 3xx (Redirection):
- 301 Moved Permanently: The requested resource has been permanently moved to a new URL.
- 302 Found: The requested resource is temporarily located at a different URL.
- 304 Not Modified: The resource has not changed since the last request, so the client can use the cached version.
- 4xx (Client Errors):
- 400 Bad Request: The client's request is incorrect or cannot be processed.
- 401 Unauthorized: The client must authenticate to obtain the requested resource.
- 403 Forbidden: The client does not have permission to access the resource.
- 404 Not Found: The requested resource was not found on the server.
- 5xx (Server Errors):
- 500 Internal Server Error: The server encountered an unexpected error while processing the request.
- 502 Bad Gateway: The server received an invalid response from an upstream server.
- 503 Service Unavailable: The server is temporarily unavailable, usually due to maintenance or overload.
- 504 Gateway Timeout: The server did not receive a timely response from an upstream server.
You might be wondering, how does knowing these codes help me? Of course, they help, and here’s why:
Diagnosis and Debugging: Status codes help you quickly identify issues. For example, a 404 indicates that the resource was not found, such as when you enter an invalid URL or one that no longer exists, while a 500 indicates a server problem that you, as a developer, should resolve.
Error Handling: They allow developers to manage and display appropriate error messages to the user. For example, showing a "Not Found" page for a 404.
Flow Control: When designing APIs, status codes indicate the outcome of client requests, allowing you to handle responses appropriately.
Security and Authentication: Codes like 401 and 403 help implement and verify access controls and authentication in applications.
Http Methods
Colloquially, they are also called HTTP verbs and there are 9 of them:
GET: Requests the representation of a specific resource. GET requests should only retrieve data and should not have any other purpose.
POST: Used to send data to the server, such as the content of a form, and can cause the creation of a new resource or the update of an existing one.
PUT: Replaces all current representations of the target resource with the request payload. It is used to update or create a specific resource.
DELETE: Deletes the specified resource.
HEAD: Similar to GET but requests only the response headers without the message body. It is used to obtain metadata about a resource.
OPTIONS: Describes the communication options for the target resource, allowing the client to determine the server's capabilities.
PATCH: Applies partial modifications to a resource. Unlike PUT, which completely replaces the resource, PATCH applies incremental or partial changes.
TRACE: Performs a loop-back test along the path to the target resource, allowing you to see what changes or additions are made to the request by intermediate servers.
CONNECT: Establishes a tunnel for secure communication through a proxy.
A software developer should know the HTTP protocol because it is the foundation of web communication, and understanding how it works is essential for building effective and efficient web applications. Here’s why it's important and how it's used day-to-day in simple terms:
Why is it important to know HTTP?
Web Communication: HTTP is the protocol that allows browsers and web servers to communicate. Every time you visit a webpage, HTTP is used to transfer data between your browser and the server.
Interacting with APIs: Many modern applications depend on APIs (Application Programming Interfaces) to function. These APIs often use HTTP to send and receive data. Knowing how HTTP requests and responses work is crucial for integrating and using these APIs correctly.
Debugging and Problem Solving: When something goes wrong in a web application, knowing HTTP helps you understand and fix issues. For example, if a request to a server fails, HTTP status codes (like 404 for "Not Found" or 500 for "Server Error") can give you clues about what went wrong.
Performance Optimization: Understanding how data is transferred over HTTP allows you to optimize the speed and efficiency of your application. You can improve page load performance, better manage caches, and minimize latency.
How are HTTP methods used day-to-day?
Sending Requests and Receiving Responses: When developing a web application, you often need to send HTTP requests (GET, POST, PUT, DELETE) to interact with a server. For example, a GET request to fetch data from an API, or a POST request to submit form data.
Handling Forms: When working with forms in a web application, data is sent to the server via HTTP requests. Knowing HTTP helps you manage how this data is sent and how to handle server responses.
Configuring Routes and Endpoints: When creating APIs, you set up routes and endpoints that respond to different HTTP methods. For example, you might have an endpoint that responds to GET requests to return user data and another that responds to POST requests to create new users.
Authentication and Security: HTTP is fundamental for implementing and understanding authentication and security mechanisms, such as using authorization tokens in request headers to access protected resources.
Using Development Tools: Tools like Postman for testing APIs, or the "Network" tab in browser developer tools, rely on HTTP. These tools show how HTTP requests and responses are sent and received, helping you debug and improve your application, so it's better to know about this, dev 🐿️.
In summary, knowing HTTP allows you as a web developer to build applications that can communicate with servers and other applications, solve problems more efficiently, and optimize the performance and security of your applications. It's an essential skill for any software developer working in web development.
Ports
Port numbers in the TCP/IP protocol are represented using 16 bits, which is why there are 2^16 combinations, or in other words, 65,536 possible ports.
Ports range from 0 to 65535 and are classified into 3 categories:
Well-Known Ports: This refers to the range of ports from 0 to 1023. These are reserved for specific services and standard protocols that are commonly used on the Internet. They are called "well-known" because they are established by the Internet Assigned Numbers Authority (IANA) and are widely recognized.
Registered Ports: This refers to the range of ports from 1024 to 49151. These ports are not specifically reserved for any standard protocol but can be registered with IANA by companies, organizations, or developers for their specific applications or services that are not in the well-known port range.
Dynamic and/or Private Ports: The port range here is from 49152 to 65535. Also known as ephemeral ports, these are available for general use and are used by applications and services as needed. They are not specifically reserved by any entity and do not have a standard assignment like well-known and registered ports. They are primarily used for outbound and temporary connections.
Some commonly used ports...
As mentioned, well-known ports are reserved for services. Here is a list of some ports and their services:
- 20: FTP (File Transfer Protocol) - Data Transfer
- 21: FTP (File Transfer Protocol) - Command Control
- 22: SSH (Secure Shell)
- 23: Telnet
- 25: SMTP (Simple Mail Transfer Protocol)
- 53: DNS (Domain Name System)
- 67: DHCP (Dynamic Host Configuration Protocol) - Server
- 68: DHCP (Dynamic Host Configuration Protocol) - Client
- 69: TFTP (Trivial File Transfer Protocol)
- 80: HTTP (HyperText Transfer Protocol)
- 110: POP3 (Post Office Protocol v3)
- 119: NNTP (Network News Transfer Protocol)
- 123: NTP (Network Time Protocol)
- 135: MS RPC (Microsoft Remote Procedure Call)
- 137-139: NetBIOS
- 143: IMAP (Internet Message Access Protocol)
- 161: SNMP (Simple Network Management Protocol)
- 194: IRC (Internet Relay Chat)
- 389: LDAP (Lightweight Directory Access Protocol)
- 443: HTTPS (HyperText Transfer Protocol Secure)
- 445: Microsoft-DS (Directory Services)
- 465: SMTPS (Simple Mail Transfer Protocol Secure)
- 514: Syslog
- 515: LPD (Line Printer Daemon)
- 587: SMTP (Submission)
- 636: LDAPS (Lightweight Directory Access Protocol over SSL)
- 989/990: FTPS (File Transfer Protocol Secure)
- 993: IMAPS (Internet Message Access Protocol Secure)
- 995: POP3S (Post Office Protocol 3 Secure)
On the other hand, registered ports are used for certain services or products. Here is a small list:
- 1433: Microsoft SQL Server
- 1723: PPTP (Point-to-Point Tunneling Protocol)
- 1812: RADIUS (Remote Authentication Dial-In User Service)
- 3306: MySQL
- 3389: RDP (Remote Desktop Protocol)
- 5432: PostgreSQL
- 5900: VNC (Virtual Network Computing)
- 6379: Redis
- 8080: HTTP Alternate (commonly used for proxy and caching)
- 8443: HTTPS Alternate
- 27017: MongoDB
Recommendations
To minimize the likelihood of conflicts and ensure your application works correctly, it is generally recommended to use ports in the registered and dynamic range (1024 to 65535) except for those mentioned above. Especially, dynamic ports (49152 to 65535) are less likely to be permanently assigned to other services or applications.
Example
If your application needs a specific port and you want to minimize possible conflicts, you could choose a port in the range of 49152-65535, for example, port 50000.
Although less recommended, you can also assign ports within the registered port range to your projects. For instance, it is common to use port 4000 or 5000 for your Angular application and ports 20000, 21000, and 22000 for your backend services. The ports mentioned in this paragraph are just examples to give you an idea; the important thing is not to use any already reserved or in the well-known port range.
Additional considerations about ports
- Ensure that the port you choose is not in use by another service on the same system before assigning it to your application.
- If your application will be deployed in production environments, it is good practice to document the ports you are using to avoid future conflicts.
- If your application requires multiple ports or has components that interact with each other, make sure to plan and document the range of ports you will use adequately.
Other concepts
Networking is an entire profession in itself, so there will always be more concepts to learn about it. However, some additional concepts you should be familiar with are the following:
- IP Addresses: These are unique identifiers assigned to devices on a network (IPv4 and IPv6).
- Routing: This is the process of sending data across a network from the source to the destination using routers and gateways.
- Firewalls: These are hardware or software devices that control network traffic based on security rules.
- DNS (Domain Name System): A system that translates domain names (URLs) into IP addresses.
Phew, dev 🔥 that was a thorough read, but with the information mentioned above, you should be able to navigate networking concepts smoothly. Applying Pareto, this is the 20% of the knowledge that will allow you to handle 80% of the cases in your work as a software developer. If you need to understand any other concepts, you can always research them on your own and add them to your skill set, which is pretty cool! 🐿️😎✌️
If you loved this post, then share it, buddy, it would help me a lot! 🦊