The labs on this site need JavaScript to run. The same content is written out below.
Fundamentals › Networking
What do an IP address and a port each point to?
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
// 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);
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 on this PC 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 on this PC 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 on this PC, 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 on this PC. 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
Struck-through text was removed from the first version of the code; highlighted text was added.