Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Java Networking
3.
Common Java Networking Terminology
3.1.
IP Address
3.2.
Protocol
3.3.
Port Number
3.4.
Mac Address
3.5.
Socket
3.6.
Connection-Oriented and Connection-Less Protocol 
4.
Java .net Package
4.1.
Low-level API
4.2.
High-level API
4.3.
TCP (Transmission Control Protocol )
4.4.
UDP (User Datagram Protocol )
5.
Java Networking Classes
6.
Java Networking Interface
7.
Frequently Asked Questions
8.
Key Takeaways
Last Updated: Mar 27, 2024

Introduction to Java Networking

Author Rhythm Jain
0 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

A computer network is a collection of computers that share resources on or provided by network nodes. Let us learn more about computer networking in java.

Must Read, Multithreading in java, Duck Number in Java

Java Networking

 As the name implies, Java Networking is the concept of linking two or more computing devices together using Java to share resources.

Java socket programming allows data to be shared between various computing devices.

The Java programming language's java.net package has several classes and interfaces that make it simple to access and work with network resources.

But first, let us learn about the standard networking terms to make us familiar with networking.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Common Java Networking Terminology

IP Address

An IP address is a unique number assigned to a network node. IP is an abbreviation for "Internet Protocol." It is made up of octets ranging from 0 to 255.

Example. 192.168.1.1

Protocol

A network protocol is a set of rules, standards, and data structures that govern how devices communicate data over networks.

There are many types of protocols, for example, TCP, UDP, FTP, TELNET, POP, etc.

Port Number

In computer networking, a port number is a piece of addressing information used to identify message senders and receivers. Several port numbers are used to define which protocol incoming communication should be directed to. When an Internet or other network message arrives at a server, the port number represents the exact process to which it is to be passed. Each protocol has its own port, which serves as a communication endpoint.

Mac Address

The term MAC address refers to the Media Access Control address. A MAC (Media Access Control) address is a unique identifier for a network interface card (NIC) (Network Interface Controller). A network node may have many NICs, each with a specific MAC address. A MAC address is a one-of-a-kind number used to track a device in a network.

For example, 00:0d:83::b1:c0:8e.

Socket

A socket is one endpoint of a two-way communication connection between two network-running programs. The socket mechanism implements inter-process communication (IPC) by establishing named contact points between which communication takes place. A socket is associated with a port number so that the TCP layer can identify the application to which data is to be transmitted.

Connection-Oriented and Connection-Less Protocol 

Before initiating communication in a connection-oriented protocol, the user must establish a connection. When the connection is established, the user can send the message or information, then disconnect. As a result, it is dependable yet slow.

The data in a connectionless protocol is delivered in a single path from source to destination without checking to see if the destination is still present or ready to accept the message. As a result, it is quick but unreliable. The connectionless protocol does not require authentication.

Java .net Package

Java.net package can be divided into two types based on the complexity of the java.net API.

Low-level API

It is concerned with the abstractions of addresses, sockets, and interfaces.

High-level API

It is concerned with the abstraction of URIs, URLs, and Connections.

The java.net Supports two well-known protocols such as:

TCP (Transmission Control Protocol )

TCP is used in the TCP IP model to transport data between applications and devices on a network. It is intended to break down a message, such as an email, into data packets for the message to reach its destination successfully and as rapidly as possible.

UDP (User Datagram Protocol )

UDP is a protocol that is datagram-centric. It is used for network transmissions such as broadcast and multicast. UDP is an abbreviation for User Datagram Protocol (A datagram is a transfer unit associated with a packet-switched network.) The UDP protocol is comparable to TCP in many ways, but it eliminates all error-checking, back-and-forth communication, and deliverability.

Java Networking Classes

Java.net API provides many classes that allow easy access to network resources.

Some of the commonly used Networking Classes are:

  • Authenticator
  • CacheRequest
  • CookieHandler
  • CookieManager
  • DatagramPacket
  • InetAddress
  • Server Socket
  • Socket
  • DatagramSocket
  • Proxy
  • URL
  • URLConnection

Java Networking Interface

Java.net API also provides many interfaces that allow easy access to network resources.

Some of the commonly used Networking Interface are:

  • CookiePolicy
  • ContentHandlerFactory
  • CookieStore
  • FileNameMap
  • DatagramSocketImplFactory
  • SocketOptions
  • SocketOption<T>
  • URLStreamHandlerFactory
  • SocketImplFactory
  • ProtocolFamily

Also read, Hashcode Method in Java

Frequently Asked Questions

  1. Which class is used to create sockets in java?
    The "java.net.Socket" class defines a socket, whereas the "java.net.ServerSocket" class implements a tool for the server application to host clients and establish connections with them.
     
  2. What is TCP/IP?
    TCP is an abbreviation for Transmission Control Protocol, whereas IP is an abbreviation for Internet Protocol. TCP is used in the TCP IP paradigm to transport data between applications and devices on a network. It is intended to break down communication, such as an email, into data packets for the message to reach its destination successfully and as rapidly as possible.
     
  3. How to include java.net API?
    Use this at the header. 
    ​​import java.net.*;

Key Takeaways

Today we learned about basic network terminologies and how to use them in Java.

We learned about java.net API and its various classes and interfaces.

If you wish to learn more about Java from experts, you can check out the Basics of Java with Data Structures and Algorithms course by coding ninjas.

Happy Coding!

Next article
Socket programming
Live masterclass