Java/J2EE and SOA (349 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)

Java Networking: What is Networking in Java?

Last updated on Jul 26,2023 12.7K Views

A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop. A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop.
13 / 22 Blog from Advance Java

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:

Introduction to Java Networking

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. package is useful for all the Java networking classes and interfaces. 

The 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.

Networking Terminologies

The widely used Java networking terminologies used are as follows:

  1. IP Address
  2. Protocol
  3. Port Number
  4. MAC Address
  5. Connection-oriented and connection-less protocol
  6. Socket

Now let’s get into the details of each of these methods.

1. IP Address

The IP address is a unique number assigned to a node of a network e.g. It is composed of octets that range from 0 to 255.

2. Protocol

A protocol is a set of rules followed for communication. For example:

  • TCP
  • FTP
  • Telnet
  • SMTP
  • POP etc.

3. Port Number

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.

4. MAC Address

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. 

5. Connection-oriented and connection-less protocol

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.

6. Socket

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

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.

Inet Address - Java Networking - EdurekaTo 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:

  1. static InetAddress getLocalHost() throws UnknownHostException
  2. static InetAddress getByName (String hostname) throws UnknownHostException
  3. static InetAddress[ ] getAllByName (String hostname) throws UnknownHostException

Now let’s take a small example to understand the working of Inet Address class.

public class InetAddressExample
public static void main(String[] args) throws UnknownHostException
InetAddress address = InetAddress.getLocalHost(); // returns the system details i.e. Inet Address
address = InetAddress.getByName(""); // returns the address of the website
InetAddress ia[] = InetAddress.getAllByName("");
for(int i=0; i< ia.length; i++)

When you execute the above code, it will return the Inet address of the system and the website as shown below:





Basically, that’s how it works. Now let’s move further and learn one more important class i.e Socket Class

Socket and Socket Server 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.

URL Class

Java URL class mainly deals with URL(Uniform Resource Locator) which is used to identify the resources on the internet.

For Example:

Here,   https: -> Protocol -> 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.

  1. getProtocol() : Returns protocol of URL
  2. getHost() : Returns hostname(domain name) of the specified URL
  3. getPort() : Returns port number of the URL specified
  4. getFile() : Returns filename of the URL

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.

Upcoming Batches For Java Certification Training Course
Course NameDate
Java Certification Training Course

Class Starts on 7th October,2023

7th October

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th November,2023

25th November

SAT&SUN (Weekend Batch)
View Details

Join the discussion

Browse Categories
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Java Networking: What is Networking in Java?