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 (5 Blogs)
  • Java/J2EE and SOA (346 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

Java Networking: What is Networking in Java?

Last updated on Jul 26,2023 13.8K 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. 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.

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

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

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

  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 Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
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?

edureka.co