The labs on this site need JavaScript to run. The same content is written out below.
Fundamentals › Networking
IP addresses and ports
What do an IP address and a port each point to?
Why it exists — On one computer, several programs that use the network run together, such as a web server, a database, and a messenger. With only an IP address you can reach only the computer, so the operating system cannot know to which program to hand an arriving connection. A port gives each program a number, and a pair of an IP address and a port number decides which program to connect to.
Where it's used today — A web server built with Spring Boot waits for connections on port 8080 by default, MySQL on port 3306, and PostgreSQL on port 5432. When you visit a site with a browser, it also connects to port 443 for https and port 80 for http. If a Spring Boot server does not start because the port overlaps, give a different number with the server.port setting.
To connect to another program over a network, you need to know two things. An IP address is a number that points to "which computer", and a port is a number that points to "which of the programs running on that computer". In the code box, the server program asks the operating system for port 8080 with new ServerSocket(8080), opens it, and waits for a connection, and the client program connects with new Socket("127.0.0.1", 8080) to port 8080 of the computer 127.0.0.1 (this computer itself). In this lab, you do not start the server program, or connect to port 9090 that nobody has opened, or let another program open port 8080 that is already open, and you see what happens to the connection. The server and the client are different programs, so the code box shows the code of both programs together.
Checked against real execution · On JDK 21 (Windows 11), started a server, another program, and a client as separate processes and ran 8 combinations on 127.0.0.1 and checked them
Concepts in this lab
- IP address (IPv4)
- Why it exists — On the internet, where many networks are joined, each device needs a common address from which it is possible to know which way to send to. An IP address is divided into a network part and a device part, so that a router can decide where to send next by looking only at the front part of the destination.
- Where it's used today — You use it when you connect to a server or set a DB address, and when you check in a log who sent a request. 32 bits are not enough addresses, so addresses are shared out using private addresses and NAT, and IPv6, which uses 128-bit addresses, is also used alongside.
- This is a 32-bit numeric address used to find a device on the internet. To make it easy for people to read, it is cut into 8-bit groups and written as four decimal numbers from 0 to 255, such as 192.168.0.10. The front part of the address shows which network the device is on, and the back part shows which device it is within that network. Where the front part ends cannot be known from the address alone; it is decided only together with a subnet mask.
- Port
- Why it exists — An IP address alone can find only the computer, so without a number you could not decide which program should get the data that arrived at a computer. Thanks to port numbers, even if you run a web server and a DB server together on one computer, their connections do not get mixed up.
- Where it's used today — Each server has a default number, such as 8080 for Spring Boot and 3306 for MySQL, and the DB connection settings write this number together with the host name. When you run two Spring Boot applications on one computer, you separate the numbers with the server.port setting.
- A port is a number from 0 to 65535 that tells apart which of the several programs running on one computer should receive the data. If an IP address points to the computer, a port points to the program inside that computer, so the place to connect to is written as "IP address:port". A server program waits for connections on a port it has chosen, and some numbers are widely agreed upon, such as 80 (HTTP) and 443 (HTTPS) for web servers. Normally only one program can wait for connections on a port, so starting a server on a port that is already in use causes an "address already in use" error. The ports of TCP and the ports of UDP are managed separately, so the same number in each is a different thing.
- Socket
- Where it's used today — A developer rarely opens a socket directly, but each connection that a JDBC driver makes to the DB or that an HTTP client makes when calling another server uses one socket at this end. A connection pool reuses these connections without closing them, reducing the time of making a new one each time.
- A socket is the endpoint of communication that a program uses to send and receive data over a network, and the operating system creates it and hands it to the program. The program only writes to and reads from the socket, and the operating system does the actual work of splitting up, sending, and receiving the data. One TCP connection consists of a pair of sockets, one at each end, and the operating system tells connections apart by four values: the source IP address, the source port, the destination IP address, and the destination port. This is why a server can receive connections from several clients at once on a single port, with a separate socket created for each connection. If you do not close a socket after using it, the resources of the operating system stay tied up.
- 127.0.0.1 (localhost)
- Why it exists — Without a fixed address that points to my own computer, every time you test you would have to find and write the IP address you have just been given, and that address changes when the network changes. 127.0.0.1 is an address fixed so that on any computer it points to itself, so you can always use the same address.
- Where it's used today — It is used when you open a Spring Boot server running on your own computer in a browser at http://localhost:8080, or connect to MySQL on the same computer at localhost:3306. When connecting from another computer, you use the actual IP address of the server computer.
- 127.0.0.1 is the IP address that points to "the computer this code is running on". Every address that starts with 127 is a loopback address internal to the host, and RFC 1122 says that such an address must not appear outside the host (section 3.2.1.3). So data sent to this address does not leave the computer and reaches a program on the same computer. localhost is the name that points to this address, and RFC 6761 recommends that software that turns names into addresses turn localhost into the loopback address (section 6.3). You use it when you run a server and a client together on one computer, and the 127.0.0.1 of your computer cannot be reached from another computer.
Try these
- Default settings — The server has opened port 8080 and the client connects to the same port. The operating system hands the connection to the server, and the client receives the text.
- Connecting without the server program — No program has opened port 8080, so the operating system refuses the connection.
- Connecting to port 9090 — The server opened only port 8080, so a connection to port 9090 is refused.
- Opening the same port again — The server has already opened that port, so the second program cannot open it. The communication between the server and the client stays the same.
Running with the default settings
- The server program starts and runs the line new ServerSocket(8080). This line asks the operating system, "give me the connections that come to port 8080". The operating system accepts the request, so now the program that has opened port 8080 on this computer is the server program.
- It runs the line server.accept(). accept is a method that waits until a connection comes, so the server program stops at this line and waits for a connection.
- The client program runs the line new Socket("127.0.0.1", 8080). 127.0.0.1 is the address that points to the computer this code is running on (the name localhost means this address), and 8080 is the port number of the program on that computer that it wants to connect to. An IP address points to which computer and a port points to which program on that computer, so the client asks to connect to port 8080 of this computer.
- The operating system looks for the program that has opened port 8080. The server program opened it, so the operating system hands the connection to the server program. The server's server.accept() line finishes, and it returns a socket object for exchanging text over this connection, and the variable fromClient refers to that object.
- The socket on the client side also has a port number of its own. The code never set it, yet it had a number different from the server's port (all the numbers we saw in the checking runs were 49152 or higher). One connection is told apart by the pair of IP addresses and ports at its two ends.
- The server program writes "Hello" to fromClient (write) and closes this connection with fromClient.close().
- The client's readAllBytes reads all the text that comes until the connection is closed. The variable text refers to the string "Hello" made from the text that was read, and println prints it.
Result of this run
- An IP address is a number that points to "which computer", and a port is a number that points to "which of the programs running on that computer". While the server program had opened port 8080 and was waiting, the client connected to port 8080 of 127.0.0.1 (this computer), and the operating system handed the connection to the server program and the text was received.
- The client code does not have the name of the server program, only the address and the port. It is the operating system that finds the program that opened the port and hands the connection to it.
Code
// For each program, only the lines inside main are shown (class, main declaration, and imports omitted)
// Server program: open port 8080 and wait for a connection
ServerSocket server = new ServerSocket(8080);
Socket fromClient = server.accept();
fromClient.getOutputStream().write("Hello".getBytes("UTF-8"));
fromClient.close();
// Client program: connect to 127.0.0.1 (this computer)
Socket socket = new Socket("127.0.0.1", 8080);
String text = new String(socket.getInputStream().readAllBytes(), "UTF-8");
System.out.println(text);
Assumptions of this model
Java is JDK 21.0.12.1 and the operating system is Windows 11. For all 8 combinations, we made the sources of the server program, another program, and the client program from the same text as the code on the screen, compiled them, started the java of this JDK as separate processes, and checked against the result of actually connecting on 127.0.0.1. What we checked is whether the client's connection succeeded and the kind of exception thrown, the text the client received, and whether the program that tried to open the port again opened it and the kind of exception thrown. 8080 and 9090 on the screen are example numbers, and in the actual check we picked two other numbers that were free in our test run for each combination and used them in the 8080 slot and the 9090 slot (for the 9090 slot, we tried to connect and confirmed that nobody had opened it). The code on the screen leaves out the import lines, the main method, and the exception handling (passed on with throws Exception), and the real programs have one more output line that tells that the process is ready. The only connection between the server and the client that we really made was on 127.0.0.1, and we did not connect to an external network. However, new ServerSocket(port) opens in a form that accepts connections on all addresses of this computer (netstat also showed 0.0.0.0), so during the check the server program was not bound to 127.0.0.1 only for a few seconds. The connection refusal and the "port already in use" error were returned by the operating system (Windows 11), and Java reported them as exceptions. When the connection was refused, what Java threw was java.net.ConnectException (message Connection refused: connect), and when it opened an already open port again, what it threw was java.net.BindException (message Address already in use: bind), and these messages were observed in our test run (Windows 11) and may differ from one operating system to another. What we checked by execution is that right after the server opens the port, the list of the operating system (netstat) shows that port as belonging to the server process, that the own port of the client-side socket is a number of 49152 or higher different from the server port, that if only another program opens the port without the server, the client's connection succeeds but no text comes within 3 seconds and the client does not finish (we did not look at what happens after 3 seconds), and that even if the client connects to a port the server did not open, the server process keeps waiting. We did not capture packets, so we could not check with which signal the operating system actually refused the TCP connection request. For the documents, we opened the following directly and checked them. Section 2.2 of RFC 9293 (TCP) says that TCP tells application services apart by port numbers, section 3.1 says that the header has a source port and a destination port, section 3.4.1 says that a connection is identified by a pair of sockets (the two ends), and section 3.10.7.1 says that in the state with no connection (CLOSED), when a segment that does not carry RST arrives, it responds with RST (we could not confirm whether the refusal above is this). Section 3.2.1.3 (g) of RFC 1122 sets addresses that start with 127 as the internal host loopback address and says that they must not appear outside the host, and Table 4 in section 2.2.2 of RFC 6890 lists 127.0.0.0/8 as Loopback and marks it as an address that is not forwarded. Section 6.3 of RFC 6761 recommends that the name localhost be resolved to the loopback address (when we resolved localhost in our test run, we got 127.0.0.1 and ::1, the IPv6 loopback address, and this lab dealt with 127.0.0.1 only). The java.net.ServerSocket documentation (Java SE 21) says that it creates a server socket with a port number, that the queue length for connection requests that have not been accepted yet is 50, and that accept blocks until a connection comes. This documentation has no explanation of BindException or of a port already in use, and the constructor description in the java.net.Socket documentation does not mention ConnectException either, so the two exceptions are observations from our test run. A port here is a port for connecting over TCP, and we did not cover UDP, IPv6, a computer with several IP addresses, or connecting to another computer on the internet. The details of sockets and ports (ephemeral ports, the range of port numbers and well-known numbers, and so on) are covered in the in-depth lab "Sockets and ports".
Previous lab: Storage and durability · Next lab: TCP connections