Networking
Networking
Networking
java.lang.Object java.net.URL
java.net.InetAddress
java.net.Socket
java.net.ServerSocket
java.net.DatagramSocket
java.net.DatagramPacket
DNS
• DNS (Domain Name Service) is used to map IP address to the
host name.
• DNS is a service which is provided by the Operating System.
Default port number for DNS is 53.
• When user sends request through browser or through an
application, it is received by DNS which maps the host name
in the requested URL to particular IP address and redirect the
request to that address.
• When host name for example; www.yahoo.com is mapped to
its corresponding IP address, it is called forward lookup.
When IP address is mapped to a host name, it is called
backward lookup.
InetAddress Class
• Internet addresses are manipulated in Java by the use of the
InetAddress class.
• This class is provided to represent any machine address connected
on the Internet.
• An instance of InetAddess consists of a host name and its
corresponding IP addresses.
• This class has no explicit constructors because its an abstract class.
• To create an InetAddress object, we have to use one of the
following factory methods.
• Factory method are static methods in a class returns an instance of
that class. This is done by overloading the constructor.
Factory Methods of InetAddress Class
• String getHostName()
It returns the host name associated with the InetAddress object.
• String getHostAddress()
It returns the IP address associated with the invoking InetAddress
object as a string.
Instance Methods of InetAddress Class
• byte[] getAddress()
It returns the IP address in the form of byte array.
• String toString()
It returns the description of invoking InetAddress object.
• boolean isMulticastAddress()
It returns true if the host is multicasted.
Note: IP multicast address is a Class D address i.e. first four bits of the
address are 1110.
URL
• For example:
http://www.yahoo.com:80/index.html
• In Java, URL class provides a simple API to access information
across the Internet using URLs.
Constructors of URL Class
1. public URL(String u) throws MalformedURLException
• It creates a URL object from the given string url.
• long getDate()
It returns the date of creation of remote resources in milliseconds.
Java calculates thime from Midnight 1st Jan 1970.
• long getContentLength()
It returns the size of remote resource in bytes.
• long getLastModified()
It returns the time in milliseconds represents in last modification
date of remote resource.
• InputStream getInputStream()
The method creates InputStream connection between remote
resource in bytes.
• OutputStraem getOutputStream()
It returns output stream connected to remote resource.
URLConnection Class
• String getContentType()
4. It is bidirectional. 4. It is unidirectional.
5. It is point to point or 5. It is connection less
connection oriented protocol.
protocol.
6. It is stream based protocol. 6. It is not based on stream.
7. Data transmission speed is 7. Data transmission speed is
less. high.
TCP/IP Protocol
• To support TCP/IP protocol, Java provides two classes.
1. ServerSocket class
2. Socket class
UDP Protocol
• To support UDP protocol, Java provides two classes.
1. DatagramSocket class
2. DatagramPacket class
ServerSocket Class
• Socket accept()
• int getPort()
It returns the remote port associated with invoking socket object.
• int getLocalPort()
It returns the local port number of invoking object.
• InputStream getInputStream()
It returns the InputStream associated with invoking Socket object.
• OutputStraem getOutputStream()
It returns the OutputStream associated with invoking Socket
object.
• void close()
It closes the I/P and O/P straems associated with invoking Socket
object.
DatagramSocket class
• Constructor is
• int getPort()
- It returns the port no. on the remote host to which the
datagram is being sent or from which the datagram was
received.
• void setPort()
- It sets the port for the packet.
• int getLength()
- It returns the length of the datato be sent or the length of
data received.
• int setLength()
- It sets the port number on the the remote host to which this
datagram is being sent.
Methods of DatagramPacket Class
• byte[] getData()