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
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.
Leave a Comment