Networking in Java
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.Networking in Java
Java is a premier language for network programming. java.net package encapsulates a large number of classes and interfaces that provide an easy-to-use means to access network resources. Here are some important classes and interfaces of java.net package.
Some Important Classes
| CLASSES | |
|---|---|
| CacheRequest | CookieHandler |
| CookieManager | Datagram packet |
| Inet Address | ServerSocket |
| Socket | DatagramSocket |
| Proxy | URL |
| URLConnection | |
Some Important Interfaces
| INTERFACES | |
|---|---|
| CookiePolicy | CookieStore |
| FileNameMap | SocketOption |
| InetAddress | ServerSocket |
| SocketImplFactory | ProtocolFamily |
InetAddress
Inet Address encapsulates both numerical IP address and the domain name for that address. Inet address can handle both IPv4 and Ipv6 addresses. Inet Address class has no visible constructor. To create an init Address object, you have to use Factory methods.
Three commonly used Inet Address factory methods are.
- static InetAddress getLocalHost() throws UnknownHostException
- static InetAddress getByName (String hostname) throws UnknownHostException
- static InetAddress[ ] getAllByName (String hostname) throws UnknownHostException
Example using InetAddress class
import java.net.*;
class Test
{
public static void main(String[] args)
{
InetAddress address = InetAddress.getLocalHost();
System.out.println(address);
address = InetAddress.getByName("www.studytonight.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:
Welcome-PC/59.161.87.227 www.studytonight.com/208.91.198.55 www.google.com/74.125.236.115 www.google.com/74.125.236.116 www.google.com/74.125.236.112 www.google.com/74.125.236.113 www.google.com/74.125.236.114 www.google.com/2404:6800:4009:802:0:0:0:1014
Socket and ServerSocket Class
The socket is a foundation of modern networking, a socket allows a single computer to serve many different clients at once. Socket establishes a connection through the use of the port, which is a numbered socket on a particular machine. Socket communication takes place via a protocol. The socket provides a communication mechanism between two computers using TCP. There are two kinds of TCP sockets in Java. One is for server and other is for the client.
- ServerSocket is for servers.
- Socket class is for the client.
URL class
Java URL Class present in java.net package deals with URL (Uniform Resource Locator) which uniquely identify or locate resources on the internet
Important 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
Program using URL class
import java.net.*;
class Test
{
public static void main(String[] arg) throws MalFormedURLException
{
URL hp = New URL("http://www.studytonight.com/index");
system.out.println(hp.getProtocol[]);
System.out.println(hp.getFile[]);
}
}
Ouput:
http -1 www.studytonight.com /index



Comments
Post a Comment