0% found this document useful (0 votes)
27 views12 pages

Ajp 4th Unit

Java

Uploaded by

a02861805
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)
27 views12 pages

Ajp 4th Unit

Java

Uploaded by

a02861805
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/ 12

Unit 4

Networking Basics
Advanced java programming
INTRODUCTION

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

Java socket programming provides facility to share data between different computing devices.

Advantage of Java Networking

1. sharing resources
2. centralize software management
3. Communication Facility
4. Backups and Fail over

The widely used java networking terminologies are given below:

1. IP Address
2. Protocol
3. Port Number
4. MAC Address
5. Connection-oriented and connection-less protocol
6. Socket

1) IP Address

IP address is a unique number assigned to a node of a network e.g. 192.168.0.1 . It is


composed of octets that range from 0 to 255.

It is a logical address that can be changed.

2) Protocol

A protocol is a set of rules basically that is followed for communication. For example:

● TCP

Transmission Control Protocol (TCP) – a connection-oriented communications


protocol that facilitates the exchange of messages between computing devices
in a network. It is the most common protocol in networks that use the Internet
Protocol (IP)

● FTP

The File Transfer Protocol (FTP) is a standard network protocol used for the
transfer of computer files between a client and server on a computer network.
FTP is built on a client-server model architecture using separate control and data
connections between the client and the server.

● Telnet

Telnet is a network protocol used to virtually access a computer and to provide


a two-way, collaborative and text-based communication channel between two
machines. It follows a user command Transmission Control Protocol/Internet
Protocol (TCP/IP) networking protocol for creating remote sessions.

● SMTP

SMTP stands for Simple Mail Transfer Protocol. SMTP is a set of


communication guidelines that allow software to transmit an electronic mail over
the internet is called Simple Mail Transfer Protocol. It is a program used for
sending messages to other computer users based on e-mail addresses.

● POP etc.

The post office protocol (POP) is the most commonly used message request
protocol in the Internet world for transferring messages from an e-mail server to
an e-mail client.

3) Port Number

The port number is used to uniquely identify different applications. It acts as a


communication endpoint between applications.

The port number is associated with the IP address for communication between two
applications.

4) MAC Address
MAC (Media Access Control)Address is a unique identifier of NIC (Network Interface
Controller). A network node can have multiple NIC but each with a unique MAC.

5) Connection-oriented and connection-less protocol

In connection-oriented protocol, acknowledgement is sent by the receiver. So it is


reliable but slow. The example of connection-oriented protocol is TCP.

But, in connection-less protocol, acknowledgement is not sent by the receiver. So it is


not reliable but fast. The example of connection-less protocol is UDP.

6) Socket

A socket is an endpoint between two way communication.

java.net package
The java.net package provides many classes to deal with networking applications in
Java.

Java URL

The Java URL class represents an URL. URL is an acronym for Uniform Resource
Locator. It points to a resource on the World Wide Web. For example:

1. https://www.javatpoint.com/java-tutorial

A URL contains many information:

1. Protocol: In this case, http is the protocol.


2. Server name or IP Address: In this case, www.javatpoint.com is the server
name.
3. Port Number: It is an optional attribute. If we write
http//ww.javatpoint.com:80/sonoojaiswal/ , 80 is the port number. If port number
is not mentioned in the URL, it returns -1.
4. File Name or directory name: In this case, index.jsp is the file name.
Commonly used methods of Java URL class
The java.net.URL class provides many methods. The important methods of URL class
are given below.

Method Description

public String getProtocol() it returns the protocol of the URL.

public String getHost() it returns the host name of the URL.

public String getPort() it returns the Port Number of the URL.

public String getFile() it returns the file name of the URL.

Example of Java URL class

//URLDemo.java

import java.net.*;

public class URLDemo{

public static void main(String[] args)

try{

URL url=new URL("http://www.javatpoint.com/java-tutorial");

System.out.println("Protocol: "+url.getProtocol());

System.out.println("Host Name: "+url.getHost());

System.out.println("Port Number: "+url.getPort());

System.out.println("File Name: "+url.getFile());

}
catch(Exception e)

{System.out.println(e);

} Output:

Protocol: http

Host Name: www.javatpoint.com

Port Number: -1

File Name: /java-tutorial

Java URLConnection class

The Java URLConnection 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 referred by the URL.

How to get the object of URLConnection class

The openConnection() method of URL class returns the object of URLConnection class.
Syntax:

1. public URLConnection openConnection()throws IOException{}

Example of Java URLConnection class

import java.io.*;

import java.net.*;

public class URLConnectionExample {

public static void main(String[] args){

try{
URL url=new URL("http://www.javatpoint.com/java-tutorial");

URLConnection urlcon=url.openConnection();

InputStream stream=urlcon.getInputStream();

int i;

while((i=stream.read())!=-1){

System.out.print((char)i);

}catch(Exception e){System.out.println(e);}

Java InetAddress class

Java InetAddress class represents an IP address. The java.net.InetAddress class


provides methods to get the IP of any host name for example www.javatpoint.com,
www.google.com, www.facebook.com, etc.

An IP address is represented by 32-bit or 128-bit unsigned number. An instance of


InetAddress represents the IP address with its corresponding host name. There are two
types of address types: Unicast and Multicast. The Unicast is an identifier for a single
interface whereas Multicast is an identifier for a set of interfaces.

Moreover, InetAddress has a cache mechanism to store successful and unsuccessful


host name resolutions.

Java InetAddress class

Java InetAddress class represents an IP address. The java.net.InetAddress class


provides methods to get the IP of any host name for example www.javatpoint.com,
www.google.com, www.facebook.com, etc.

An IP address is represented by 32-bit or 128-bit unsigned number. An instance of


InetAddress represents the IP address with its corresponding host name. There are two
types of address types: Unicast and Multicast. The Unicast is an identifier for a single
interface whereas Multicast is an identifier for a set of interfaces.

Moreover, InetAddress has a cache mechanism to store successful and unsuccessful


host name resolutions.

Commonly used methods of InetAddress class


Method Description
public static InetAddress getByName(String host) it returns the instance of InetAddress
throws UnknownHostException containing LocalHost IP and name.

public static InetAddress getLocalHost() it returns the instance of InetAdddress


throws UnknownHostException containing local host name and address.

public String getHostName() it returns the host name of the


IPaddress.

public String getHostAddress() it returns the IP address in string format.

Example of Java InetAddress class

Let's see a simple example of InetAddress class to get ip address of


www.javatpoint.com website.

import java.io.*;

import java.net.*;

public class InetDemo

public static void main(String[] args){

try{

InetAddress ip=InetAddress.getByName("www.javatpoint.com");

System.out.println("Host Name: "+ip.getHostName());

System.out.println("IP Address: "+ip.getHostAddress());


}

catch(Exception e)

System.out.println(e);

Output:

Host Name: www.javatpoint.com

IP Address: 206.51.231.148

Factory Methods
The InetAddress class has no visible constructors. To create an InetAddress object,you have to
use one of the available factory methods. Factory methods are merely aconvention whereby
static methods in a class return an instance of that class.This is done in lieu of overloading a
constructor with various parameter lists whenhaving unique method names makes the results
much clearer.

Three commonly usedInetAddress factory methods are shown here.

->static InetAddress getLocalHost( )

throws UnknownHostException

->static InetAddress getByName(String hostName)

throws UnknownHostException

->static InetAddress[] getAllByName(StringhostName)

throws UnknownHostException
The getLocalHost( ) method simply returns the InetAddress object that representsthe local
host.

The getByName( ) method returns an InetAddress for a host namepassed to it. If these
methods are unable to resolve the host name, they throw anUnknownHostException.

On the Internet, it is common for a single name to be used to represent severalmachines.

In the world of web servers, this is one way to provide some degree ofscaling. The
getAllByName( ) factory method returns an array of InetAddresses that represent all of the
addresses that a particular name resolves to. It will also throw anUnknownHostException if it
can't resolve the name to at least one address.

Java 2, version 1.4 also includes the factory method getByAddress( ), which takes an
IPaddress and returns an InetAddress object. Either an IPv4 or an IPv6 address can be used.The
following example prints the addresses and names of the local machine and two well-known
Internet web sites:

Java Socket Programming

Java Socket programming is used for communication between the applications running
on different JRE.

Java Socket programming can be connection-oriented or connection-less.

Socket and ServerSocket classes are used for connection-oriented socket programming
and DatagramSocket and DatagramPacket classes are used for connection-less socket
programming.

The client in socket programming must know two information:

1. IP Address of Server, and


2. Port number.

Here, we are going to make one-way client and server communication. In this
application, client sends a message to the server, server reads the message and prints
it. Here, two classes are being used: Socket and ServerSocket. The Socket class is
used to communicate client and server. Through this class, we can read and write
message. The ServerSocket class is used at server-side. The accept() method of
ServerSocket class blocks the console until the client is connected. After the successful
connection of client, it returns the instance of Socket at server-side.
Socket class
A socket is simply an endpoint for communications between the machines. The Socket
class can be used to create a socket.

IMPORTANT METHODS

Method Description

1) public InputStream getInputStream() returns the InputStream attached with this socket.

2) public OutputStream getOutputStream() returns the OutputStream attached with this socket.

3) public synchronized void close() closes this socket

ServerSocket class
The ServerSocket class can be used to create a server socket. This object is used to establish
communication with the clients.

Important methods

Method Description

1) public Socket accept() returns the socket and establish a connection between

server and client.

2) public synchronized void close() closes the server socket.

Example of Java Socket Programming

Creating Server:

To create the server application, we need to create the instance of ServerSocket class. Here, we are
using 6666 port number for the communication between the client and server. You may also choose any
other port number. The accept() method waits for the client. If clients connects with the given port number,
it returns an instance of Socket.

1. ServerSocket ss=new ServerSocket(6666);


2. Socket s=ss.accept();//establishes connection and waits for the client

Creating Client: To create the client application, we need to create the instance of Socket class. Here,
we need to pass the IP address or hostname of the Server and a port number. Here, we are using
"localhost" because our server is running on same system.

1. Socket s=new Socket("localhost",6666);

Java DatagramSocket and

DatagramPacket

Java DatagramSocket and DatagramPacket classes are used for connection-less socket programming.

Java DatagramSocket class


Java DatagramSocket class represents a connection-less socket for sending and receiving datagram
packets.

A datagram is basically an information but there is no guarantee of its content, arrival or arrival time.

Java DatagramPacket class


Java DatagramPacket is a message that can be sent or received. If you send multiple packet, it may
arrive in any order. Additionally, packet delivery is not guaranteed.

Commonly used Constructors of DatagramPacket class

● DatagramPacket(byte[] barr, int length): it creates a datagram packet. This constructor is used
to receive the packets.
● DatagramPacket(byte[] barr, int length, InetAddress address, int port): it creates a datagram
packet. This constructor is used to send the packets.

Video link:

https://www.youtube.com/watch?v=DuFyhu5_GPs

You might also like