Header javaperspective.com
JavaPerspective.com  >   Intermediate Tutorials  >   4. Networking  >   4.2. IP addresses, DNS and ports

4.2. IP addresses, DNS and ports
Last updated: 1 February 2013.

4.2.1. IP addresses

IP addresses (Internet Protocol addresses) are defined at the layer 3 of the OSI model. An IP address looks something like in the current version of IP which is IPv4. It is a 32-bit number that uniquely identifies a computer in a network. As you can see, it is a series of 4 numbers separated by dots. Each number spans 8 bits (one byte) and therefore ranges from 0 to 255.

IP addresses ranging from to, from to and from to are private addresses which can only be used within a local area network (LAN). Routers do not forward packets whose destination address is private. Typically, a LAN has a single public IP address (that is routable) and uses network address translation (NAT) to provide communication between the private computers and the outside.

Although IPv4 is still being used today, a new version of IP (IPv6) is being progressively introduced to address several issues such as the shortage of available IP addresses. In the new version, IP addresses span 128 bits, allowing many more IP address assignments. An IPv6 address looks something like 2001:3e45:4f27:0000:0000:43ac:d369:0e31. It is a series of 8 hexadecimal numbers separated by colons. Each hexadecimal number spans 16 bits (two bytes).

An IP address can be assigned to a computer statically or dynamically. On the one hand, in case of a static assignment, a network administrator assigns a static IP address to a computer. On the other hand, a dynamic IP address can be assigned to a computer in many ways, such as DHCP (Dynamic Host Configuration Protocol). DHCP is a protocol that allows a computer to get at startup a new IP address from a DHCP server. Hence, unlike static IP addresses, dynamic IP addresses can change.

If you are a Linux or Mac user, you can display your IP address information by typing ifconfig in a terminal. If you are running Windows, type ipconfig in a command prompt.

4.2.2. Domain Name System (DNS)

Computers use IP addresses for routing packets across the Internet. For example, a web browser gets a page from a website by sending a HTTP request to the IP address of the computer that hosts the website. Such a request can be done by typing the server's IP address in the browser's address bar. However, because IP addresses are not easy to remember, domain names like www.google.com are mapped to IP addresses in DNS (Domain Name System) servers, allowing Internet users to communicate with remote computers by using domain names instead of IP addresses.

DNS servers are organized hierarchically throughout the Internet. Every computer that is connected to the Internet is aware of a nearby DNS server. When you type http://www.google.com in your browser's address bar, the browser sends a request to the DNS server to resolve the domain name www.google.com (translate the name into an IP address). Then the browser sends a HTTP request to the IP address that it received from the DNS server.

4.2.3. Port numbers

A port number ranges from 0 to 65535 (it is a 16-bit number). Together with an IP address and a transport protocol (TCP or UDP), a port number defines a computer endpoint in a network (an endpoint is also called a socket). For example, if you are connected with TCP to a web server and to a mail server at the same time, each connection uses two port numbers: a port number on your computer and a port number on the remote computer as shown in the following picture:

Port numbers

In the above picture, the red lines are TCP connections. Let's take a closer look at the first connection between the browser on your computer and the web server: before establishing the connection to the web server at http://something.com, the browser needed 3 things in order to know the exact endpoint to connect to:These 3 pieces of information defined the web server endpoint. Next, your operating system automatically allocated the port number 52437 to the connection on your side. Hence, after the connection is established, the web server knows the remote port to use when communicating with your browser through the connection.

As you can see, your computer initiated the connection towards the web server. There must be an initiator in every communication between computers. In fact, networking is all about clients and servers. In the example shown above, your computer is the client and the web server is, as its name implies, the server. The client is the computer which initiates the connection. In networking terminology, the client/server model is referred to as an architecture in which a server provides services to numerous clients. More precisely, a server is said to be listening to incoming connections on a given port number. For example, HTTP servers listen to incoming connections on the port number 80. Likewise, mail servers using the SMTP protocol (Simple Mail Transfer Protocol) listen to incoming connections on the port number 25.

Note that when browsing the web, you can specify the web server's port number in your browser's address bar. Just add to the server's domain name a colon followed by the web server's port number. For example, http://www.google.com:80 is the same as http://www.google.com. In the latter, the port number 80 is implicit. Some web servers run on port numbers other than the default port number 80. Such web servers are generally used within companies private networks.

Port numbers ranging from 0 to 1023 are termed the well known ports and you should not use them when doing network programming because they are reserved to well known services such as SMTP, DNS, HTTP and so forth. The table below lists several well known ports:

Port number Transport protocol Service
25 TCP SMTP (Simple mail transfer protocol)
53 TCP, UDP DNS (Domain name system)
80 TCP HTTP (Hypertext Transfer Protocol)
123 UDP NTP (Network Time Protocol)
992 TCP Telnet

As I said earlier, an endpoint is the association between an IP address, a transport protocol and a port number. Consequently, on a single computer, two servers relying on the same transport protocol (TCP or UDP) cannot listen to incoming connections on the same port number. However, a TCP server and a UDP server running on the same computer can listen to incoming connections on the same port number. For example, on the same computer, a DNS server relying on the TCP protocol can run alongside with a DNS server relying on the UDP protocol (both DNS servers run on the port number 53).

A key point to remember when deciding whether to use TCP or UDP is that TCP provides reliable connections between servers and clients whereas UDP does not. On the one hand, TCP is connection-based and thus guarantees that no packets will be lost between a client and a server. Once a connection is established, reliable bidirectional communication can take place through the connection. When the communication ends, the connection must be explicitly closed by both parties. On the other hand, no connection is established when you are using UDP. Rather, packets are sent without any prior connection establishment and they can be lost or arrive out of order. However, UDP is faster than TCP since UDP does not control the order and arrival of packets. For this reason, you may prefer UDP over TCP if your program can handle lost UDP packets (for example by resending them).

In the Java language, you can develop network servers and clients quite easily with the classes in the package java.net. The next tutorial will show you how to write a simple multithreaded server that relies on the TCP transport protocol and also how to write a client for that server.

You are here :  JavaPerspective.com  >   Intermediate Tutorials  >   4. Networking  >   4.2. IP addresses, DNS and ports
Next tutorial :  JavaPerspective.com  >   Intermediate Tutorials  >   4. Networking  >   4.3. TCP servers and clients

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7