Networking

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Networking and Socket Programming

Important Class from java.net package

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

1. public static InetAddress getLocalHost() throws


UnKnownHostException
- It returns the host name and IP address of local machine and in
case of any error it throws UnKnownHostException.

2. public static InetAddress getByName(String hostname) throws


UnKnownHostException
- It returns information about the argument host name.

3. public static InetAddress[] getAllByName(String hostname)


throws UnKnownHostException
- It returns the array of InetAddress object containing
information about all the IP addresses of argument host name.
Instance Methods of InetAddress Class

• boolean equals(InetAddress Object)


It compares the evoking object with argument object and returns
true if they are same.

• 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

• URL (Uniform Resource Locator) is used to uniquely identify


or address information on the Internet.

• It has four components:

1. Protocol – it indicates the set of rules to be followed for


communication.
2. Host name – it is alias assign to the computer to identify
3. Port – it indicates the socket to be used for communication.
4. Filename – it indicates the resource to be used.
URL

• Protocol and hostname are delimited by ‘://’, hostname and


port no. are delimited by ‘:’ and port no. and filename are
delimited by ‘/’.
• Its standard syntax is
protocal://hostname:port no./filename

• 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.

2. public URL(String protocol, String host, int port, String file)


throws MalformedURLException
• It creates the URL object from the specified protocol name, host
name , port no. and file name.
• Host can be expressed as host name or literal IP address.
• Specifying the port number of -1 indicates that the URL should
use the default port for the protocol.
• If an unknown protocol is specified , it throws
MalformedURLException
Constructors of URL Class

3. public URL(String protocol, String host, String file) throws


MalformedURLException
- It creates the URL from the specified protocol name, host name
and file name.
- The default port for the specified protocol is used.

4. public URL(URL object, String file) throws


MalformedURLException
- It is used to create a new URL object based on the existing one.
URLConnection Class

• This class is used to collect the properties of remote resource


such as its creation date, modification date, content type,
content length, etc.

• It is general purpose class which also allows you to establish


stream based connection between the resourse on remote
server and Java program; using which it can be downloaded
to the client.
URLConnection Class

• 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()

• It returns the type of data present in remote resource.

• To indicate the type of data , Internet uses Multipurpose


Internet Mail Protocol (MIME Protocol).
• It returns output in the format “general/specific”.
• For example:
for image -> img /gif , img /jpg , img /bmp
for text -> text/doc , text/txt , text/html
Socket Programming

• Socket is an imaginary entity & is the basis of communication.


• For communication between computers , we need Operating
System, Protocol and imaginary concept Socket.
• Socket are of two types:
1. Physical Socket
2. Logical Socket
• Physical sockets are used to connect the various input output
devices with CPU. For example: socket for mouse, keyboard,
monitor etc.
Socket Programming

• Logical sockets are used for communication between the


software.
• The range of logical socket is from 1 to 65536.
• Out of which first 1023 sockets are reserved for OS used.
• The sockets are used by Operating System and its protocols to
provide different services.
• For example: HTTP Service, FTP Service, etc.
• To identify the socket, number is attached with it and such
numbered socket is called Port.
Socket Programming

• Some protocols and its default port number are as follows:

Protocol Port Number


HTTP 80
FTP 21
TelNet 23
SMTP 25
POP 110
Difference between TCP/IP & UDP

1. Transmission Control 1. User Datagram Protocol


Protocol / Internet Protocol which works on Transport
which works on Transport layer of OSI model.
and Network layer of OSI
Model.
2. It is a reliable protocol as it 2. It is not reliable as it does
assures that data packets not guarantee to transport
will reach successfully to its the data packet
destination. successfully.
3. It is capable of 3. It is not capable of
retransmission. retransmission.
Difference between TCP/IP & UDP

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

• This class is design to create socket on server.


• The class is designer to be listener which provides all its
services on a publish port, allows the clients to connect to it
and after accepting the connection allow them to access all its
services.
• The constructors of ServerSocket class are
1. ServerSocket( int port no.)
It reserve the given port no. and waits for client to connect.
The default queue length is of 50 clients.
2. ServerSocket( int port no., int queue length)
It waits for the client on the published port number and
allows given queue length clients to wait.
Method of ServerSocket class

• Socket accept()

- It returns a Socket object that has connected to the port.

- This method keeps the server in waiting state on the port


no. associated with invoking ServerSocket object.

- When client gets connected , accept the connection and


store the client socket information in object of Socket class
which is its return type.
Socket class

• This class is designed to create a client socket. When its object


is created it implicitly tries to connect to the server whose
information is passed to it.

• Its constructors are

1. Socket(String hostname, int port)


2. Socket(InetAddress obj, int port)
Methods of Socket class

• 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

• It is used to reserve a socket for sending and receiving the


datagram packet.

• Constructor is

DatagramSocket(int port no.)


- It reserves the given port.
Instance methods

• void send(DatagramPacket object)

It sends the argument packet through the invoking socket.

• void receive(Datagram object)


It waits for the incoming packet on invoking socket. On
receiving packet from network, copy its content into the
argument packet.
DatagramPacket class

• It is used to encapsulate the information to be send.


• Constructor:
1. DatagramPacket(byte[], int size)
- It creates an datagram packet containing information in the byte
array having size equal to the second argument.
2. DatagramPacket(byte[], int size, InetAddress object, int port)
- It creates a packet containing information in the byte array and
having address of its destination port number.
- First constructor is used to receive the packet and 2nd
constructor is used to send the packet.
Methods of DatagramPacket Class

• 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()

- It returns the data in the form of byte array.

• void setData(byte[] buf)


- It sets the data buffer for this packet.
• InetAddress getAddress()

- Returns the IP address of the machine to which this


datagram is being sent or form which the datagram was
received.

You might also like