Unit 5 Networking Notes
Unit 5 Networking Notes
Networking Basics:
Computer network provides different ways for reliable and fast communication. The users of
networks can share data & other information & communication among the nodes. Java
provides capabilities to interact with network resources.
Some basic terms used in network are stated as follows:
2) Port Numbers/ Port : IP addresses would be sufficient if each computer is doing only 1 task
at a time over the net. But modern computers do many different things at once. For example,
email service & FTP request can be two simultaneous activities at one node. This is
accomplished through ports. Each computer with an IP address has several 1000 logical ports
(65535). They do not represent any physical port like serial or parallel port. Each port is
identified by a number between 1 to 65535. Each port can be allocated to a particular service.
3) Protocols: Computers follow certain rules to communicate with each other. Rules that
govern packaging of data into packets, speed of transmission & reassembly of data into its
original form are called as network protocols.
4) TCP: It’s a connection oriented protocol. When two applications communicate with each
other, they establish a link between a host and destination port to transmit data. TCP
guarantees that data has been successfully sent to the destination. If the data does not reach
the destination TCP reports an error and tries to resend data.
5) UDP: It’s a connection less transfer protocol. It does not establish a link while sending data to
the destination. It uses datagram to send independent packets of data over the network.
6) Datagram: It is a self contained unit that has all the information needed to attempt
transmission.
Page 1 of 6
IF4K/JPR/SMJ
7) Proxy server: It is a server which can be viewed as a bridge between client application such
as a network browser and a real web server. It interprets all requests coming from client end
and sees that it can fulfill the request itself. If not, it forwards the request to the real server.
There are two major goals served by proxy server.
Improved performance: It is because of the property that proxy server saves the result of all
the requests for certain amount of time. It can improve performance for group of users.
A proxy server has the additional ability to filter certain requests or cache the results of
those requests for future use. When a popular web site is being hit by hundreds of users, a
proxy server can get the contents of the web server’s popular pages once, saving expensive
internetwork transfers while providing faster access to those pages to the clients.
InetAddress Class:
All the classes required for network programming in java can be imported through ‘java.net’
package. For identifying the IP address of a computer. ‘java.net’ package provides a major class
as InetAddress. This class can be used to obtain IP address and domain name. This IP address
can then be used by other networking classes, while specifying the address of destination and
source data packets.
There are several other methods which can be used on the objects returned by factory
methods of InetAddress. These are as follows:
1) byte [] InetAddress(): It returns byte array containing IP addresses from network.
2) String getHostAddress() : It returns the host name associated with InetAddress object.
3) String getHostName() : It returns the host name associated with InetAddress object.
4) Boolean isMulticastAddress() : It returns true if InetAddress is multicasting.
5) String toString() : It converts InetAddress object to String format.
Programs:
1) WAP to get IP Address of local host and the given domain name.
Page 2 of 6
IF4K/JPR/SMJ
URL class: URL class does not have any subclass obj of URL class can be created by its
constructor as:
URL U= new URL (http://www.vpmthane.org):
Exception to be nandled is Malformed URL exception
Methods are:
1 getFile ( )
Display the file name
2 getHost ( )
Display domain name
3 getPort ( )
Display port no.
4 getProtocol ( )
Display Protocol
Programs:
2) Develop a program to print protocol, host port & file of http://www.msbte.com
URLConnection Class
This class of java.net package enables to implement stream to read the contents of URL.
It’s a super class & represents the link between URL & application over the network. The major
methods of this class are as follows:
An obj of URLConnection can be created using openConnection ( )’ method of URL obj.
1) getContentEncoding ( )
It retrieves the data encoding used for transport
2) getExpiration ( )
Returns URL expiration time.
3) getLastModified ( )
Returns the time & date when the URL was last modified.
4) getInputstream ( )
Opens a stream for reading data
5) getOutputstream ( )
Opens a stream for writing data
Programs:
3) WAP to read content of HTML file from the URL & display it on the screen.
HttpURLConnection class
Java provides a subclass of URLConnection that provides support for HttpConnection. It
is available with a name as HttpURLConnection. It can be obtained in the same way as URL
Connection i.e. by calling open connection ( )’ method of URL object. But the result should be
Page 3 of 6
IF4K/JPR/SMJ
type cased to HttpURLConnection. Once the object is created, you can use any method
inherited from URLConnection class. Some of the methods of this class are:
String getResponseMessage ( )-Returns response message associated with code. Result null
if no message.
Socket:
A socket is an end point of a 2 way communication link between 2 applications on a
network. Sockets are based on client server model. When a client sends a request to the server
a connection is made & the server creates a new socket for that client at a particular address &
a port. Client & server can read & write data using sockets. Reading & writing can be done with
the help of Buffered Reader, Input stream Reader, Print Writer class. Any computer that
supports TCP/IP can communicate with other component with the help of sockets. In java
sockets are implemented by socket classes included in java.net package. The two main socket
classes are:
1) Socket: It provides methods for IO streams which make reading & writing to a socket
easy.
2) ServerSocket: It is used to create server socket for listening to client request.
Page 4 of 6
IF4K/JPR/SMJ
Constructors
1) Socket ( ) – default constructor
2) Socket (ip add, port) - e.g. Socket (“127.0.0.1”, 2200);
3) Socket (hostname, port) - e.g. Socket (“poly1”,8766);
Methods
1) getInputStream() – it opens an I/P stream to read data from socket.
2) getOutputStream () – it opens an O/P stream to write data to the socket.
3) getLocalAddress ()- it returns the address to which socket is bound.
4) close () – used to close the socket connection.
Methods
1) Socket accept () - makes server socket to listen the client connection & accept it.
2) close () - used to close the server socket connection
Programs:
4) WAP to send used mane from a client and display it on server
5) WAP to accept a username from the client and server will send a welcome message to
the user.
6) WAP to accept a no. from server and check whether its prime or not on client.
Datagram :-
Datagrams are bundles of information passed between machines. It is a independent
self content message sent over the network whose arrival, arrival time and contents are not
guaranteed. ‘java.net’ package provides DatagramSocket and DatagramPacket to implement
system independent datagram communication using UDP Datagram Packet object is Data
container and Datagram Socket Provides a mechanism for send and receive datagram packet.
1) DatagramSocket Class
Constructors
DatagramSocket ( )
DatagramSocket (port)
DatagramSocket (port, ip)
DatagramSocket (SocketAddress Obj) – where obj encapsulates port & ip
Page 5 of 6
IF4K/JPR/SMJ
Methods
Void send (DatagramPacket Packetobj);
To send data packet in the form of data gram packet object
Int getLocalPort ( )
Returns the port no. of Local port.
Int getPort ( )
Returns current port no. if no port is given it returns ‘-|’
Inetaddress getAddress ( )
Returns the ip address otherwise null
Boolean isConnected ( )
Returns Boolean true if connected to the specified port otherwise false.
Boolean isBound ( )
Returns Boolean true if bound to specified address otherwise false.
2) DatagramPacket Class
Constructors
DatagramPacket ( ) – default
DatagramPacket (byte data [ ], int size)
DatagramPacket (byte data [ ], int offset, int size)- where offset defines start of
data
DatagramPacket (byte data [ ], int size, ip address, port)
DatagramPacket (byte data [ ], int offset, int size, ip address, port)
Methods
InetAddress getAddress ( ) -Returns the ip address of data packets.
Page 6 of 6