0% found this document useful (0 votes)
11 views29 pages

Unit3

The document covers Java Network Programming, focusing on the java.net package, including classes like InetAddress, Socket, and ServerSocket for TCP communication. It explains connection-oriented and connection-less socket programming, as well as the structure and components of URLs. Additionally, it provides examples of using the InetAddress and URL classes to retrieve host information and manage network resources.

Uploaded by

kibat40952
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views29 pages

Unit3

The document covers Java Network Programming, focusing on the java.net package, including classes like InetAddress, Socket, and ServerSocket for TCP communication. It explains connection-oriented and connection-less socket programming, as well as the structure and components of URLs. Additionally, it provides examples of using the InetAddress and URL classes to retrieve host information and manage network resources.

Uploaded by

kibat40952
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Unit 3

Java Network Programming

The java.net package, Connection oriented


transmission – Stream, Socket Class, Creating a
Socket to a remote host on a port, (creating TCP
client and server), Simple Socket Program Example
InetAddress class
• InetAddress class represents an IP address.
• The java.net.InetAddress class provides methods to get the IP of
any host name for example www.google.com,www.facebook.com,
etc.
• An IP address is represented by 32-bit or 128-bit unsigned
number.
• An instance of InetAddress represents the IP address with its
corresponding host name.
• There are two types of address types:
1. Unicast
2. Multicast.
• The Unicast is an identifier for a single interface whereas
Multicast is an identifier for a set of interfaces.
• Moreover, InetAddress has a cache mechanism to store successful
and unsuccessful host name resolution
Example of InetAddress class
import java.io.*;
import java.net.*;
public class InetDemo{
public static void main(String[] args){
try{
InetAddress ip=InetAddress.getByName("www.abc.com");
System.out.println("Host Name: "+ip.getHostName());
System.out.println("IP Address: "+ip.getHostAddress());
}catch(Exception e){System.out.println(e);}
}
}

Output:
Host Name: www.abc.com
IP Address: 206.51.231.148
• The Java class CacheRequest stores resources in ResponseCache,
while the CookieHandler implements an HTTP state management
policy.

• The CookieManager class separates cookie storage from policy,


containing a CookieStore and a CookiePolicy.

• The DatagramPacket class facilitates connectionless message


transfer, while the InetAddress class provides methods to get
hostname IP addresses, handling both IPv4 and IPv6 addresses.

• The ServerSocket class implements system-independent server-


side client/server Socket Connections, causing exceptions if the
specified port is already used.
• The Socket class in Java enables users to create socket objects for
basic socket operations, such as sending, reading data, and closing
connections.

• DatagramSocket is a network socket that provides a connection-less


point for sending and receiving packets, allowing UDP
communication instead of TCP.

• A proxy is a changeless object that preserves user and computer


data, acting as a wall between computers and internet users.

• The URL class is the entry point to internet sources, and the
URLConnection class provides control on server interaction and
allows users to verify headers and configure header fields in client
requests.
The Java.net package includes several interfaces for easy access to
network resources.

These include the CookiePolicy interface, which determines cookie


acceptance and rejection, the CookieStore interface, which describes a
cookie storage space, and the FileNameMap interface, which outlines a file
name and MIME type string.

These interfaces provide a user-friendly way to access network resources.

The SocketOption interface allows users to control socket behavior, while


the SocketImplFactory defines a factory for SocketImpl instances.

The ProtocolFamily interface represents a family of communication


protocols, with a method called "name()".
• Java socket programming is used for communication
between applications on different JREs using TCP.

• Connection-oriented socket programming involves


managing Socket and ServerSocket classes, while
connection-less socket programming uses DatagramSocket
and DatagramPacket classes.

• A client application creates a socket and connects to a


server, which creates a socket object for communication.

• The java.net.Socket class describes a socket, while the


java.net.ServerSocket class enables server programs to
host clients and build connections.
To establish a TCP connection between two computing devices using
socket programming, follow these steps:

1. Server instantiates a ServerSocket object, indicating the port number


for communication.
2. Server requests the accept() method of the ServerSocket class,
paused until a client connects.
3. Client instantiates a Socket class object, defining the server name
and port number.
4. The constructor of the Socket class connects the client to the
designated server and port number. If communication is authenticated,
the client has a Socket object capable of interacting with the server.
5. On the server side, the accept() method returns a reference to a new
socket on the server connected to the client's socket.

Communication can occur using I/O streams, with each object of a


socket class having both an OutputStream and an InputStream.
Java URL

The Java URL class represents an URL. URL is an


acronym for Uniform Resource Locator. It points to
a resource on the World Wide Web. For example:

https://www.abc.com/java-tutorial
A URL contains many information:

Protocol: In this case, http is the protocol.

Server name or IP Address: In this case, www.abc.com is the


server name.

Port Number: It is an optional attribute. If we write


http//www.abc.com:80/xyz/ , 80 is the port number. If port
number is not mentioned in the URL, it returns -1.

File Name or directory name: In this case, index.jsp is the file


name.
Example of URL class
//URLDemo.java
import java.net.*;
public class URLDemo{
public static void main(String[] args){
try{ URL url=new
URL("http://www.javatpoint.com/java-tutorial");
System.out.println("Protocol: "+url.getProtocol());
System.out.println("Host Name: "+url.getHost());
System.out.println("Port Number: "+url.getPort());
System.out.println("File Name: "+url.getFile());
}catch(Exception e){System.out.println(e);}
}
}
Output:
Protocol: http
Host Name: www.abc.com
Port Number: -1
File Name: /java-tutorial

You might also like