Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
Network programming refers to writing programs that execute across multiple devices (computers), in which the devices are connected to each other via a network. Java encapsulates classes and interfaces to allow low-level communication details. In this article, I will give you a brief insight into the fundamentals of Java Networking.
Below topics are covered in this article:
Java Networking is a notion of connecting two or more computing devices together to share the resources. Java program communicates over the network at the application layer. java.net package is useful for all the Java networking classes and interfaces.
The java.net package provides support for two protocols. They are as follows:
TCP − Transmission Control Protocol allows reliable communication between two applications. TCP is typically used over the Internet Protocol, which is referred to as TCP/IP.
UDP − User Datagram Protocol is a connection-less protocol that allows packets of data to be transmitted between applications.
Note: Networking in Java is mainly used for sharing the resources and also for centralized software management.
With this, let’s move further and learn various terminologies used in Networking.
The widely used Java networking terminologies used are as follows:
Now let’s get into the details of each of these methods.
The IP address is a unique number assigned to a node of a network e.g. 192.168.0.1. It is composed of octets that range from 0 to 255.
A protocol is a set of rules followed for communication. For example:
The port number uniquely identifies different applications. It acts as a communication endpoint between applications. To communicate between two applications, the port number is used along with an IP Address.
A MAC address is basically a hardware identification number which uniquely identifies each device on a network. For example, an Ethernet card may have a MAC address of 00:0d:83:b1:c0:8e.
In the connection-oriented protocol, acknowledgment is sent by the receiver. So it is reliable but slow. The example of a connection-oriented protocol is TCP. But, in the connection-less protocol, acknowledgment is not sent by the receiver. So it is not reliable but fast. The example of a connection-less protocol is UDP.
A socket in Java is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to.
Now that you know various terminologies used in Java Networking, let’s move further and understand some of the important classes that it supports.
Inet Address is used to encapsulate both the numerical IP address and the domain name for that address. It can handle both IPv4 and Ipv6 addresses. Below figure depicts the subclasses of Inet Address class.
To create an Inet Address object, you have to use Factory methods. Basically, there are three commonly used Inet Address factory methods. They are as follows:
Now let’s take a small example to understand the working of Inet Address class.
import java.net.*; public class InetAddressExample { 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.facebook.com"); // returns the address of the website System.out.println(address); InetAddress ia[] = InetAddress.getAllByName("www.google.com"); for(int i=0; i< ia.length; i++) { System.out.println(ia[i]); } } }
When you execute the above code, it will return the Inet address of the system and the website as shown below:
Output:
DESKTOP-KN72TD3/192.168.0.215 www.facebook.com/31.13.79.35 www.google.com/172.217.163.132
Basically, that’s how it works. Now let’s move further and learn one more important class i.e Socket Class
A socket is used to establish a connection through the use of the port, which is a numbered socket on a particular machine. Socket basically provides a communication mechanism between two computers using Transmission Control Protocol. There are two types of sockets as follows:
ServerSocket is for servers
The socket class is for the client
If you wish to gain more insights on Socket Programming, kindly refer this article on Socket Programming in Java.
Now, let’s understand what is URL Class in Networking.
Java URL class mainly deals with URL(Uniform Resource Locator) which is used to identify the resources on the internet.
For Example: https://www.edureka.co/blog
Here, https: -> Protocol
www.edureka.co -> hostname
/blog - > filename
URL Class comprises of various methods to return the URL information of a particular website. Let’s now understand various methods of Java URL Class.
So this was all about the URL class in Java. With this, we come to an end of this article on Java Networking. I hope you found it informative.
Check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer.
Got a question for us? Please mention it in the comments section of this “Java Networking” article and we will get back to you as soon as possible.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 1st March,2025 1st March SAT&SUN (Weekend Batch) | View Details |
edureka.co