You are on page 1of 62

Networking

Networking:-
• Networking is a concept of connecting two or more
computing devices together so that we can share
resources.

• Advantage:-

• 1)sharing of resources(Printer Sharing)


• 2)Sharing Of information

We provide skills for international certification 1


• Basic Concept Of Networking:-

• 1)IP Address
• 2)Protocol
• 3)Port Number
• 4)Connection-oriented and connectionless protocol
• 5)Socket

We provide skills for international certification 2


• 1)IP Address:-

• Conceptually similar to the traditional mailing address

• -> Each house has a traditional mailing address


• -> Each computer connected to the Internet has a unique
IP address

We provide skills for international certification 3


• A 32-bit number (IPv4) or 128-bit number (Ipv6) used
to
• uniquely identify each computer connected to the
Internet

• > 192.1.1.1 (IP address)


• > Yahoo.com (Hostname)
• Each number can be 0 to 255
• Default ip Address of Computer is 127.0.0.1

We provide skills for international certification 4


• Communication Protocol:-
• • Why protocols?
• > Different types of communication occurring over the
Internet
• > Each type of communication requires a specific and
unique protocol

We provide skills for international certification 5


• • Definition(What is Protocol)
• ->A protocol is a set of rules basically that is followed for
communication.

• -> Set of rules and standards that define a certain type of


Internet communication

We provide skills for international certification 6


• Describes the following information:-

• > Format of data being sent over the Internet


• > How it is sent
• > When it is sent
• Not entirely new to us. Consider this type of
conversation:
• "Hello."
• "Hello. Good afternoon. May I please speak to Dev"
• "Okay, please wait for a while."
• "Thanks."

We provide skills for international certification 7


• > Social protocol used in a telephone conversation
• > Gives us confidence and familiarity of knowing what to
do

• Some important protocols used over the Internet

• > Hypertext Transfer Protocol (HTTP)


• > Used to transfer HTML documents on the Web

We provide skills for international certification 8


• > File Transfer Protocol (FTP)
• -> More general compared to HTTP
• > Allows you to transfer binary files over the Internet
• > Both protocols have their own set of rules and
standards on how data is transferred

• > Java provides support for both protocols

We provide skills for international certification 9


• 3)Ports
• • Protocols only make sense when used in the context of
a service
• > HTTP protocol is used when you are providing Web
content through an HTTP service

• > Each computer on the Internet can provide a variety of


services

We provide skills for international certification 10


• • Why Ports?
• > The type and address of service must be known before
• information can be transferred
• Definition:-
• > A 16-bit number(0 to 65535) that identifies each
service offered by a network server

• • Using a particular service to establish a line of


• communication through a specific protocol
• > Need to connect to the appropriate port

We provide skills for international certification 11


• • Standard ports
• > Numbers specifically associated with a particular type
of service
• > Examples:-
• > The FTP service is located on port 21
• > The HTTP service is located on port 80 or 8080
• > All Ports are reserved till 0 to 1023 for predefined
services.

We provide skills for international certification 12


• • Port values above 1024
• > Available for custom communication
• > If already in use by some custom communication, you
must look for other unused values.

We provide skills for international certification 13


• The Client/Server Architecture:-
• • It Involves two major elements:
• -> Client
• > Machine in need of some type of service
• > Server
• > Machine providing service and waiting for a request

• • Scenario:-
• > Client connects to a server and queries for certain
• information
• > Server considers the query and returns information
• on it to the client

We provide skills for international certification 14


We provide skills for international certification 15
• Networking
• Networking can be of two types-

• Connection-Oriented
• Connection-Less

We provide skills for international certification 16


Difference between Connection Less &
Connection Oriented Networking-
• In Connection oriented networking a dedicated path is
establish between sender & receiver and all the data
packets are transmitted over this dedicated path.

• Order & delivery of packets are Guaranteed.


• Acknowledgement is received for each delivered packet.
TCP/IP protocol is used.

• Connection oriented networking is used when


• bi-directional synchronous communication is required.

We provide skills for international certification 17


• In case of Connection less networking no dedicated path
is establish between sender and receiver.

• Data packets are transmitted directly on the available


network which are routed by the network routers.

• Order & delivery of packets are not guaranteed. No


acknowledgement is received.UDP protocol is used.
Connection less networking is used when uni-directional
asynchronous communication is required.

We provide skills for international certification 18


• Java Support Development of Connection Less as well as
Connection Oriented networking application to facilitate
development networking application in java.
• java.net package provides following classes-

1. InetAddress
2. Socket
3. ServerSocket
4. DatagramSocket
5. DatagramPacket
6. URL
7. URLConnection

We provide skills for international certification 19


• 2 & 3- Support Connection Oriented Networking
• 4 & 5- Support Connection Less Networking
• 6 & 7- Support HTTP based Networking

• InetAddress class is common which is used in all types of


networking

We provide skills for international certification 20


• First requirement of networking is the identification of
hosts or machines.Hosts are identified with the help of
IP addresses.

• InetAddress class provides object representation of IP


addresses i.e. an InetAddress object represents the IP
address of a host.

We provide skills for international certification 21


• InetAddress class does not provide public Constructors.
Rather it provides factory methods for the creation of its
object.

• Factory is a design pattern which is used to solve the
problems relative to object creation. Implementation of
this design pattern is provided with the help of factory
methods. A factory method is a method which creates
and returns objects.

• Following problem demonstrate the need & use of


factory methods-

We provide skills for international certification 22


Define a singleton class named A. A
singleton class is a class which can be
instantiated only once.

We provide skills for international certification 23


• Object of class A can’t be directly created from other
class i.e.
• A x = new A(); 
• A y = new A(); 
• Reference of object of A can only be obtained with the
help of getA() factory method as follows

• A x = A.getA();
• A y = A.getA();
• S.o.p(x==y) //true.

We provide skills for international certification 24


• Factory methods of InetAddress class-

• getLocalHost()-
• is used to obtain an InetAddress object for the IP address
of current machine.

• public static InetAddress getLocalHost()throws


UnknownHostException;

We provide skills for international certification 25


• getByName()-
• is used to obtain the IP address of the specified host.

• public static InetAddress getByName(String


HostName)throws UnknownHostException

We provide skills for international certification 26


• getAllByName()-
• is used to obtain all the IP address of the specified host.
• public static InetAddress[] getAllByName(String
HostName)throws UnknownHostException

We provide skills for international certification 27


• Non-factory methods of InetAddress class-
• getHostAddress()-
• is used to obtain the IP address as a String.
• public String getHostAddress();

• getHostName()-
• is used to obtain the host name.
• public String getHostName();

• (Demos)

We provide skills for international certification 28


What is a Socket

We provide skills for international certification 29


• Let App1 & App2 are using one protocol and App3 is
using another protocol.

• Components involved in the Communication of the


Applications over the network.

We provide skills for international certification 30


• What is a Socket:-
• -> Software abstraction for an input or output medium of
communication

• ->Communication channels that enable you to


transfer data through a particular port

• -> An endpoint for communication between two


machines

We provide skills for international certification 31


• A Socket represents a logical end point of a network
connection.
• From a programmers point of view a Socket is a process
which is used by an application for sending & receiving
data over the network.

• Socket manages protocol specific details on behalf of


applications.

We provide skills for international certification 32


• ->A particular type of network communication used in
most Java network programming

• -> Java performs all of its low-level network


communication through sockets

We provide skills for international certification 33


• Socket Programming:-

• Socket programming is performed for communication


between the machines.

• A socket is simply an endpoint for communications


between the machines. The Socket class can be used to
create a socket.

We provide skills for international certification 34


• Java.net.Socket class provides object representation of
TCP/IP Socket.

• An object of Socket class act as a mediator between java


application and OS specific TCP/IP Socket.

We provide skills for international certification 35


We provide skills for international certification 36
• 1.0- Java application creates a Socket object and uses it
as end point for Sending & Receiving Data.

• 1.1- Socket object starts O/S specific TCP/IP Socket and


provides application data to it and receive data from it on
behalf of Application.

We provide skills for international certification 37


• 1.2- TCP/IP Socket wraps application data into TCP/IP
packets, writes them into Buffer and reads TCP/IP
packet from the Buffer, unwraps them.

• 1.3- Driver manages transfer of TCP/IP packets between


Buffer & NIC.

We provide skills for international certification 38


• Socket object can be created using either of the following
Constructors-
• public Socket(String HostName, int port)throws
UnknownHostException, IOException;

• public Socket(InetAddress HostAddress, int


port)throws UnknownHostException,
IOException;

We provide skills for international certification 39


We provide skills for international certification 40
• Following example demonstrate the steps involved in the
creation of a Socket object

• Let there be two Host A & B.


• On host B TCP/IP server is running and is configured for
port 2000. Host A can create a TCP/IP connection on
this port as follows-

We provide skills for international certification 41


We provide skills for international certification 42
The Socket Class: Methods

We provide skills for international certification 43


• Java.net.ServerSocket class:-

• The ServerSocket class can be used to create a server


socket. This object is used to establish communication
with the clients.

• ServerSocket class represents the functionality of a


TCP/IP server.

We provide skills for international certification 44


• An object of type ServerSocket can be created using
following Constructors

• public ServerSocket(int port);

• public ServerSocket(int port, int maxQueueLength)

• Note- Default queue size is 50.

We provide skills for international certification 45


Commonly used methods of
ServerSocket class:

We provide skills for international certification 46


We provide skills for international certification 47
Using UDP for asynchronous
Communication:-
• DatagramSocket and DatagramPacket :-
• DatagramSocket:-

• this class represents a connection less socket for


sending and receiving datagram packets.

We provide skills for international certification 48


• The DatagramSocket and DatagramPacket classes are
basically used for connection less socket programing.

• Commonly used Constructor of DatagramSocket class:-


• 1)DatagramSocket() throws SocketException

• it creates a datagram socket and binds it with the


available Port number on the localhost machine.

We provide skills for international certification 49


• 2)DatagramSocket(int port)throws SocketException

• it creates a datagram socket and binds it with the given


port number.

• 3) DatagramSocket(int port,InetAddress address)throws


SocketException

• it creates a datagram socket and binds it with the


specified port number and host address.

We provide skills for international certification 50


• Method of DatagramSocket class-
• send()- is used to send a UDP packet.
• public void send(DatagramPacket packet);
• receive()- is used to receive a UDP packet.
• public void receive(DatagramPacket packet);
• close()- closes the Socket.
• public void close();

We provide skills for international certification 51


DatagramPacket class
• The DatagramPacket is message that can be sent or
received .If u send multiple packet ,it may arrive in any
order.More over packet delivery is not guaranteed.

• Commonly used constructor:-


• 1)DatagramPacket(byte[] barr,int length)
• it creates a datagram packet .This constructor is used to
receive the packets.

• 2) DatagramPacket(byte[] barr,int length,InetAddress


address,int port)
• It creates a datagram packet.This constructor is used to
send the packets.

We provide skills for international certification 52


We provide skills for international certification 53
• ServerSocket and Socket class use TCP
Protocol(Connection Oriented)

• DatagramSocket and DatagramPacket use UDP (


Connection less Protocol)

We provide skills for international certification 54


• URL class:-
• URL class represents a functionality of a Http client. Each
URL object act as client for a resource which can be accessed
from a web server using Http protocol.

• The Url class represents a URL.URL is an acronym for


Uniform resource locator.
• it points to a resource on the world wide web.

• ex:-

• http://www.infomatics.info/AboutUs.aspx

We provide skills for international certification 55


• A URL contains the following information:-
• Protocol:- in this case http is a protocol
• Server Name of IP Address:- in this case
www.infomatics.info is the server name
• Port no:- it is an optional attribute if we write
• http://www.infomatics.info:80/AboutUs.aspx
• here 80 is the port no.
• File name or directory name :- in this case
AboutUs.aspx is the file name

We provide skills for international certification 56


• A URL object can be created as follows-

• public URL(String URL of the Http resource);

We provide skills for international certification 57


• Commonly used methods of URL class:-

• 1) getProtocol()
• is used to returns the protocol of the URL.

• public String getProtocol()

• 2)getHost()
• it returns the Host name of the URL.

• public String getHost()

We provide skills for international certification 58


• 3) getPort()
• it returns the port no.

• public int getPort()


• 4) getFile()
• it returns the file name of the URL.

• Demo

We provide skills for international certification 59


• URLConnection:-
• this class represents a communication link
between the URL and the application.This class can be
used to read and write data to the specified resource .
• that is reffered by the URL.

We provide skills for international certification 60


• openConnection():-

• this method of the URL class it returns the


URLConnection type value.
• syntx:-

• public URLConnection openConnection() throws


IOException

We provide skills for international certification 61


• URLConnection class provides methods to fetch information
about the response data and to read the response data.
Commonly used methods of this class are-

• getContentLength()- returns the number of bytes received
as response.
• public int getContentLength();

• getInputStream()- returns an InputStream to read the
response data.
• public InputStream getInputStream();

• getContentType()- returns the content type of response.

We provide skills for international certification 62

You might also like