Super Easy Way To Learn Everything About Java Networking

Java Network programming is the process of writing programs that run on multiple computers connected to each other through a network. Java uses classes and interfaces to hide the low-level details of how programs talk to each other.

In this article, I’ll give you a quick overview of the basics of Java Networking. This is a continuance of the previous topic, entitled Java IO Serialization.

This article covers the following topics:

Introduction To Java Networking

Java was the first programming language that was created from the start with networking. Java was originally made for private cable TV networks instead of the Internet, but it has always been designed with the network in mind. One of the first two real Java programs was a browser for the Internet.

As the Internet grows around the world, Java is the best way to make the next generation of network programs. Java solves a number of problems that are important for Internet applications but hard to solve in other languages.

Java Networking is the idea of linking two or more computers together so that they can share resources. At the application layer, Java programs talk to each other over the network. The java.net package is useful for all the classes and interfaces in Java that deal with networking.

Two protocols can be used with the java.net package. Here’s what they are:

  • TCP
  • UDP

TCP – TCP stands for “Transmission Control Protocol.” It is a standard for communication that lets application programs and computers share messages over a network. It is made to send packets across the internet and make sure that data and messages are delivered successfully over networks.

Transmission Control Protocol makes it possible for two applications to talk to each other in a reliable communication. Most of the time, TCP is typically used over the Internet Protocol. This is called TCP/IP.

UDPUser Datagram Protocol (UDP) is a communication protocol that is mostly used to connect applications on the internet with low-latency and loss-tolerant connections. UDP speeds up transmissions by letting data be sent before the receiving party has agreed to it.

User Datagram Protocol is a protocol that allows packets of data to be sent from one application to another without the need for a connection.

Note: Networking in Java is mostly used to share resources and manage software from a single place.

Now that we know the introduction, let’s move on and learn about the different terminologies used in networking.

Networking Terminologies

Most of the time, the following Java networking terms are used:

  • IP Address
  • Protocol
  • Port Number
  • MAC Address
  • Connection-Oriented and Connection-Less Protocol
  • Socket

Now, let’s talk about how each of these methods works in more depth.

IP Address

An Internet Protocol (IP) address is a unique number that is given to each device that is connected to the internet. Every computer network-connected mobile phone, laptop, cable box, tablet, server, and thousands of other types of devices (IoT) have one.

For example, 192.168.0.1 is a valid IP address. It is made up of octets with values between 0 and 255.

Protocol

A set of rules for communicating is called a protocol. As an example:

  • TCP
  • FTP
  • TelNet
  • SMTP
  • POP etc.

Port Number

A port number is a way to tell a server that a message from the internet or another network should be sent to a certain process.

It acts as a place where two applications can talk to each other. Along with an IP Address, the port number is used to let two applications talk to each other.

MAC Address

A MAC (Media Access Control) address, sometimes called a hardware or physical address, is a unique 12-character alphanumeric string that is used to identify each electronic device on a network.

One example of a MAC address is: 00-B0-D0-63-C2-26.

Connection-Oriented and Connection-Less Protocol

Connection oriented protocol makes a connection, checks to see if the message was received, and sends it again if there was an error. Connectionless service protocol does not guarantee that a message will be delivered. Connection-based service is more reliable than connectionless service.

One example of a connection-oriented protocol is TCP. But in the connection-less protocol, the receiver does not send a confirmation. So it is not reliable but quick. UDP is an example of a connection-less protocol.

Socket

In Java, a socket is one end of a two-way connection between two programs that are running on the network.

A port number is linked to a socket so that the TCP layer can figure out which application the data is going to. An IP address and a port number together make up an endpoint.

Now that you know some of the terminologies used in Java Networking, let’s learn about some of the most important classes it supports.

Inet Address

InetAddress class is Java’s way of wrapping up an IP address. Most of the other common networking classes, like Socket, ServerSocket, URL, DatagramSocket, DatagramPacket, and more, use it. This class represents an Internet address with two fields: hostName (a String) and address (an int ).

It can deal with both IPv4 addresses and IPv6 addresses. The subclasses of the Inet Address class are shown in the figure below.

Inet Address
Inet Address

Factory methods are needed to make an Inet Address object. In general, there are three Inet Address factory methods that are often used. These are what they are:

Factory method is a creational design pattern that lets you make product objects without telling the computer what their concrete classes are.

  • static InetAddress getLocalHost() throws UnknownHostException
  • static InetAddress getByName (String hostname) throws UnknownHostException
  • static InetAddress[ ] getAllByName (String hostname) throws UnknownHostException

Now, let’s look at an example to see how the Inet Address class works.

Example

import java.net.*;

public class inetAddress {

    public static void main(String[] args) throws UnknownHostException {
        InetAddress address = InetAddress.getLocalHost(); // returns the system details i.e. Inet Address
        System.out.println(address);
        address = InetAddress.getByName("www.sourcecodehero.com"); // returns the address of the website
        System.out.println(address);
        InetAddress ia[] = InetAddress.getAllByName("www.itsourcecode.com");
        for (int i = 0; i < ia.length; i++) {
            System.out.println(ia[i]);
        }
    }
}

Output

DESKTOP-F31G9N7/192.168.1.8
www.sourcecodehero.com/172.67.195.64
www.itsourcecode.com/172.67.139.205
www.itsourcecode.com/104.21.8.170
www.itsourcecode.com/2606:4700:3030:0:0:0:ac43:8bcd
www.itsourcecode.com/2606:4700:3031:0:0:0:6815:8aa

That’s basically how it works. Now, let’s learn about one more important class about socket programming.

Socket and Socket server class

The ServerSocket class is used to implement the server side of a client that connects via Socket Connection in a way that doesn’t depend on the system. If ServerSocket can’t listen on the port that was given, the constructor throws an exception (for example, if the port is already being used).

Socket basically gives two computers a way to talk to each other using Transmission Control Protocol. There are two kinds of sockets, as follows:

  • ServerSocket is for servers
  • The socket class is for the client

Now, let’s figure out what URL Class means in the world of networking.

URL Class

A URL is shown by the Java URL class. Uniform Resource Locator is what URL stands for. It tells you where to find something on the World Wide Web.

Example

https://itsourcecode.com/
URL Class
URL Class

A URL contains a lot of information: 

  • Protocol: In this case, http is the protocol.
  • Server name or IP Address: In this case, www.itsourcecode.com is the server name.
  • Port Number: It is an optional attribute. If we write http//ww.itsourcecode.com:90/GlennAzuelo/ , 90 is the port number. If port number is not mentioned in the URL, it returns -1.
  • File Name or directory name: In this case, index.html is the file name.

URL Class is made up of different ways to get the URL of a certain website. Now let’s learn about the different methods of the Java URL Class.

  • getProtocol() : returns protocol of URL
  • getHost() : returns hostname(domain name) of the specified URL
  • getPort() : returns port number of the URL specified
  • getFile() : returns filename of the URL

Summary

In summary, you have learned about Java Networking. This article discusses Introduction networking, networking terminologies, socket and socket server class and URL class.

I hope this lesson has helped you learn a lot. Check out my previous and latest articles for more life-changing tutorials which could help you a lot.


Leave a Comment