Categories: Java

Java Networking

What is Java Networking?

Connecting two or more computing devices so that we can share resources is the idea behind Java networking.

The Java socket programming language offers the ability to transfer data among many computing systems.

The application layer is where all network communications for Java programs take place. The J2SE APIs’ java.net package contains a number of classes and interfaces that execute the low-level communication features, allowing the user to create programs that are specifically focused on fixing the issue.

Java Networking Classes
  • Authenticator
  • CacheRequest
  • CacheResponse
  • ContentHandler
  • CookieHandler
  • CookieManager
  • DatagramPacket
  • DatagramSocket
  • DatagramSocketImpl
  • InterfaceAddress
  • JarURLConnection
  • MulticastSocket
  • InetSocketAddress
  • InetAddress
  • Inet4Address
  • Inet6Address
  • IDN
  • HttpURLConnection
  • HttpCookie
  • NetPermission
  • NetworkInterface
  • PasswordAuthentication
  • Proxy
  • ProxySelector
  • ResponseCache
  • SecureCacheResponse
  • ServerSocket
  • Socket
  • SocketAddress
  • SocketImpl
  • SocketPermission
  • StandardSocketOptions
  • URI
  • URL
  • URLClassLoader
  • URLConnection
  • URLDecoder
  • URLEncoder
  • URLStreamHandler
Java Networking Interfaces
  • ContentHandlerFactory
  • CookiePolicy
  • CookieStore
  • DatagramSocketImplFactory
  • FileNameMap
  • SocketOption<T>
  • SocketOptions
  • SocketImplFactory
  • URLStreamHandlerFactory
  • ProtocolFamily
InetAddress

Inet Address contains both the domain name and the IP address in one package. Both IPv4 and IPv6 addresses are supported by Inet addresses. Inet Address class has no visible Constructor. It is necessary to use Factory methods in order to generate an inet Address object.

There are three popular Inet Address factory methods.

  • static InetAddress getLocalHost() throws UnknownHostException
  • static InetAddress getByName (String hostname) throws UnknownHostException
  • static InetAddress[ ] getAllByName (String hostname) throws UnknownHostException
Example
import java.net.*;
class Coderz
{
  public static void main(String[] args) throws UnknownHostException
  {
    InetAddress address = InetAddress.getLocalHost();
    System.out.println(address);
    address = InetAddress.getByName("www.coderzpy.com");
    System.out.println(address);
    InetAddress sw[] = InetAddress.getAllByName("www.google.com");
    for(int i=0; i< sw.length; i++)
    {
      System.out.println(sw[i]);
    }
  }
}
Output
compiler-deployment-7cf697c54b-kd2bj/10.8.0.19
www.coderzpy.com/46.17.172.15
www.google.com/173.194.74.99
www.google.com/173.194.74.104
www.google.com/173.194.74.147
www.google.com/173.194.74.106
www.google.com/173.194.74.103
www.google.com/173.194.74.105
ServerSocket and Socket class

Modern networking is built on sockets, which enable a single computer to serve multiple clients simultaneously. A port, which is a numbered socket on a specific system, is how a socket creates a connection. A protocol underlies socket communication. TCP-based sockets offer a means of communication between two machines. TCP sockets come in two varieties in Java. Both are for clients and servers, respectively.

  • Servers use ServerSocket.
  • Socket class is for client.
URL class

The Java URL Class in the java.net package deals with URLs (Uniform Resource Locators), which are used to locate or identify resources on the internet uniquely.

Some Methods of URL class
  • getProtocol() : Returns protocol of URL
  • getHost() : Returns hostname(domain name) of URL
  • getPort() : Returns port number of URL
  • getFile() : Returns filename of URL
Example
import java.net.*;
class Coderz
{
  public static void main(String[] arg) throws MalformedURLException
  {
    URL hp = new URL("https://coderzpy.com/category/c/");
    System.out.println(hp.getProtocol());
    System.out.println(hp.getFile());
  }
}
Output
https
/category/c/

Note: also read about the Serialization and Deserialization in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago