0% found this document useful (0 votes)
5 views

Unit 5 Networking Notes

The document provides an overview of network programming basics, including key concepts such as IP addresses, port numbers, protocols (TCP and UDP), and the role of proxy servers. It also introduces Java's InetAddress, URL, URLConnection, and HttpURLConnection classes for handling network connections, along with socket and datagram communication methods. Several programming exercises are included to illustrate the practical application of these concepts in Java.

Uploaded by

adityapandji1
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)
5 views

Unit 5 Networking Notes

The document provides an overview of network programming basics, including key concepts such as IP addresses, port numbers, protocols (TCP and UDP), and the role of proxy servers. It also introduces Java's InetAddress, URL, URLConnection, and HttpURLConnection classes for handling network connections, along with socket and datagram communication methods. Several programming exercises are included to illustrate the practical application of these concepts in Java.

Uploaded by

adityapandji1
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/ 6

IF4K/JPR/SMJ

UNIT V: Basics of Network Programming

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:

1) IP Address: Computers communicate by sending packets of data over the network. IP


protocol is the network protocol used to send information from one computer to another over
internet. To send packets using IP there must be a method for uniquely identifying the
computers on networks. All computers on network are identified by IP addresses. IP address
provided by IP protocol takes a specific for such as 192.168.1.201. Each address is a 32 bit
number represented as a series of four bit numbers ranging from 0 to 255. An IP address is not
easy to remember, to solve this problem each computer is associated with a domain name. A
domain name is computer name mapped to IP address.

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.

For eg: who is -43 Finger -79


Echo -7 telnet service -23
FTP – 21 Web server – 80
1 to 1023 is reserved for already existing services.

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.

Factory methods and Instance methods


InetAddress class has no visible constructor. For creating an object of InetAddress class one of
the available factory methods can be used. Factory methods are the conventions where by
static methods in class return an instance of class.
Commonly used InetAddress factory methods are:
1) Static InetAddress getLocalHost() throws UnknownHostException : returns InetAddress
object that represents local host.
2) Static InetAddress getLocalByName(String hostname) throws UnknownHostException :
returns InetAddress object for a host name that is passed to it.
3) static InetAddress[] getAllByName(String hostname) throws UnknownHostException :
returns an array of all InetAddress objects containing IP addresses for a host name that is
passed to it.
Whenever no host is found, it raises ‘UnknownHostException’.

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

Classes for URL:


There are two major classes provided by URL in java.net package as :
1) URL class
2) URLConnection class

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 getRequestMethod ( )-Returns ‘get’ or ‘post’

 Int getResponseCode ( )-Returns response code if no response code it return -1

 String getResponseMessage ( )-Returns response message associated with code. Result null
if no message.

 Void setRequestMethod (string how)-Request method can be set as get or post.

To create object of HttpURLConnection:


URL Ul = new URL (http://www.msbte.com);

URLConnection hpcon = (HttpURLCOnnection)Ul.openConnection);

Client Server Application


In client server application client requests for services and server services there
requests. The requests are transferred from client to the server over the network. The server
processing is hidden from the client. In java communication between client & server can be
established using sockets or datagram.
Sockets use TCP & datagrams use UDP. Both sockets & datagram transfer data to the
application at a particular ip address & a port. TCP establishes a link between a client & server.
Each application binds a socket to its end of the connection. The client & server can read &
write data from & to the socket.

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.

Major methods of Socket Class

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.

Major methods of ServerSocket Class :-


Constructors
1) ServerSocket () – default constructor
2) ServerSocket(port) – constructor accepting port no. e.g. Server Socket (1100)
3) ServerSocket(port,int backlog) - port is port no & backlog is max no. Of client
connections that the port is accepting.

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

 Void receive (DatagramPacket Packetobj);


To receive data packed 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.

 Int getPort ( ) -Returns the port no. specified in data packet.

 Int getLength ( )-Returns Length of valid data.

 Byte [ ] getData ( ) - Returns data in bytes array form.


Programs:
7) WAP to display information about datagram packet containing a string inside it.
8) WAP to send a string from server to client using Datagram Socket.

Page 6 of 6

You might also like