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.
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.
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]);
}
}
}
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
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.
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.
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());
}
}
https
/category/c/
Note: also read about the Serialization and Deserialization in Java
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
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
You manage an e-commerce website and need to keep track of the last N order…
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…