0% found this document useful (0 votes)
16 views51 pages

CH No 4 Networking Basics13124

Notes for good for manage study

Uploaded by

sohamastane077
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views51 pages

CH No 4 Networking Basics13124

Notes for good for manage study

Uploaded by

sohamastane077
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

UNIT 4 NETWORKING

BASICS
10 MARKS
COURSE OUTCOME
 Develop a java programs using networking
concepts
JAVA NETWORKING
 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

sharing resources
centralize software management
JAVA NETWORKING TERMINOLOGY

 Server
A Server is a machine that has some
resources that can be shared. For example, a
computing server, which provides computing
capability; a print server which provides
network printing facility, a disk server which
provide networked disk space; and web server
which stores web pages.
 Client
A Client is simply any machine that wants to
use the services of a particular server. The
server is a permanently available resource,
while the client is free to disconnect after its
job is done.
Client server communication.
 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.
 Protocol
 In networking, a protocol is a set of rules for

formatting and processing data.


 For example:

TCP
FTP
Telnet
SMTP
POP etc.
PORT NUMBER

 A port number identifies a specific application


running in the machine.
 A port is a number in the range 165535.
 It is a transport layer address used by TCP
(Transmission Control Protocol) and UDP (User
Datagram Protocol) to handle communication
between applications.
 For example, an email application on one machine
wants to communicate with an email application on
another machine.
 The machines are identified by the IP address
but the email applications are identified by the
port number at which they communicate i.e. Port 25.
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 unique MAC.
 The NIC is essentially a computer circuit card

that makes it possible for your computer to


connect to a network.
 An NIC turns data into an electrical signal

that can be transmitted over the network.


 Every NIC has a hardware address that's known
as a MAC, for Media Access Control. Where IP
addresses are associated with TCP/IP (networking
software), MAC addresses are linked to the
hardware of network adapters.
 A MAC address is given to a network adapter
when it is manufactured.
 It is hardwired or hard-coded onto your
computer's network interface card (NIC) and is
unique to it.
 Something called the ARP (Address Resolution
Protocol) translates an IP address into a MAC
address.
SOCKET
 Java Socket programming is used for
communication between the applications running
on different JRE.
 A socket represents the endpoint of a network
communication.
 Sockets provide a simple read/write interface and

hide the implementation details network and


transport layer protocols.
 Java Socket programming can be connection-

oriented or connection-less.
 Socket and ServerSocket classes are used for

connection-oriented socket programming


 DatagramSocket and DatagramPacket classes are

used for connection-less socket programming.


 Reserved Socket
The socket which are reserved by specific
protocols for communication are reserved
sockets
The port No from 0-1023 are reserved for
services like http,ftp,telnet etc
Ex.
Ftp-21,Telnet-23,email-25,Http-80
DOMAIN NAMING SERVICE(DNS)

 It is difficult to remember a set of IP address


to connect to the internet i.e
http://225.220.30.2.
 So DNS is used to overcome this problem

Domain Name System is a hierarchical naming


system for computers, services or any
resources connected to the internet or a
private network
 Proxy Server
 A proxy server is a server that is between a

client application such as web browser and


real server.
 This software hides the actual server from

client that communicate with it.


 It intercepts all request to the real server to

check if it can fulfill the request itself. If Not,it


forwards the request to the real server.
THE JAVA.NET PACKAGE

 java.net package encapsulate large number


of classes and interface that provides an
easy-to use means to access network
resources. Here are some important classes
and interfaces of java.net package.
Important Classes
 URL, URLConnection, Inet Address, Socket,

ServerSocket, Datagrampacket,
DatagramSocket
Important Interface
 SocketOption, SocketImplFactory,

CookiePolicy
URL CLASS
 Java URL Class present in java.net package,
deals with URL (Uniform Resource Locator)
which uniquely identify or locate resources
on internet.
 Ex
 A URL contains many information:
 Protocol: In this case, http is the protocol.

 Server name or IP Address: In this case,

www.studytonight.com is the server name.


 Port Number: It is an optional attribute. If we

write http//www.studyto
night.com:80/index.html/ , 80 is the port
number. If port number is not mentioned in
the URL, it returns -1.
 File Name or directory name: In this case,

index.html is the file name.


CONSTRUCTOR OF URL CLASS
 public URL(String u) throws
MalformedURLException

 public URL(String protocol, String host, String


file) throws MalformedURLException

 public URL(String protocol, String host, int


port, String file) throws
MalformedURLException
METHODS OF URL CLASS
import java.io.*;
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);}
}
}
JAVA URLCONNECTION CLASS
The Java URLConnection class represents a
communication link between the URL and the
application.
 It can be used to read and write data to the

specified resource referred by the URL.


FEATURES OF URLCONNECTION
CLASS
 URLConnection is an abstract class.
 The two subclasses

 1.HttpURLConnection :If we are connecting

to any url which uses “http” as its protocol,


then HttpURLConnection class is used.

 2. JarURLConnection- makes the connetion


between the client Java program and URL
resource on the internet.
 however, we are trying to establish a

connection to a jar file on the web, then


JarURLConnection is used.
 With the help of URLConnection class, a
user can read and write to and from any
resource referenced by an URL object.
 Once a connection is established and the

Java program has an URLConnection object,


we can use it to read or write or get further
information like content length, etc.
URLCONNECTION
Constructor Description

1) Protected URLConnection(URL It constructs a URL connection to


url) the specified URL.

 The openConnection() method of URL class


returns the object of URLConnection class.
 Syntax:

public URLConnection openConnection()throws


IOException{}
METHODS OF URLCONNECTION
 int getContentLength():returns the length of the content
header field.
 String getContentType():Returns the value of the content-
type header field.
 int getLastModified():Returns the value of the last-modified
header field.
 long getExpiration():Returns the value of the expired header
field.
 URLConnection openConnection(): opens the connection to
the specified URL.
 Object getContent(): retrieves the content of the
URLConnection.
 OutputStream getOutputStream(): returns the output
stream to this connection.
 InputStream getInputStream(): returns the input stream to
this open connection.
import java.io.*;
import java.net.*;
import java.util.Date;
public class URLConnectionExample
{
public static void main(String[] args)throws IOException
{
URL url=new URL("http://www.msbte.com/mainsite");
URLConnection urlcon=url.openConnection();
System.out.println("contenet type="+urlcon.getContentType());
System.out.println("contenet length="+urlcon.getContentLength());
System.out.println("Date="+ new Date(urlcon.getDate()));
}
}
INETADDRESS

 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.
 Inet Address encapsulates both numerical IP

address and the domain name for that address.


 Inet address can handle both IPv4 and Ipv6

addresses.
 Inet Address class has no visible constructor. To

create an inet Address object, you have to


use Factory methods.
 Three commonly used Inet Address factory
methods are.
 static InetAddress getLocalHost() throws Unkno

wnHostException
 static InetAddress getByName (String hostname)

throws UnknownHostException
 static InetAddress[ ] getAllByName (String

hostname) throws UnknownHostException


INSTANCE METHODS
 public boolean equals(Object obj)
 public byte[] getAddress()
 public String getHostAddress()
 public String getHostName()
 public boolean

isMulticastAddress()
 public String toString()
import java.net.*;
import java.util.*;

public class Exp14a


{
public static void main(String[] args)throws
UnknownHostException
{
String host;
Scanner sc = new Scanner(System.in);
System.out.print("\n Enter host name: ");
host = sc.nextLine();

InetAddress ia = InetAddress.getByName(host);
System.out.println("IP address: " + ia.getHostAddress());
System.out.println("Host name : " + ia.getHostName());
System.out.println("Host name and IP address: " + ia.toString());

}
}
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


 DatagramSocket and DatagramPacket

classes are used for connection-less socket


programming.
CLIENT-SIDE PROGRAMMING
 To connect to another machine we need a socket connection.
 A socket connection means the two machines have information
about each other’s network location (IP Address) and TCP port.
 The java.net.Socket class represents a Socket.
 To open a socket:
 Socket socket = newSocket(“127.0.0.1”, 5000)

 The first argument – IP address of Server. ( 127.0.0.1 is the IP


address of localhost, where code will run on the single stand-alone
machine).
 The second argument – TCP Port. (Just a number representing
which application to run on a server. For example, HTTP runs on port
80.
 Port number can be from 0 to 65535)
 Communication
To communicate over a socket connection, streams are used to both
input and output the data.
 Closing the connection
 The socket connection is closed explicitly once the message to the
server is sent.
THE TCP/IP CLIENT SOCKET OR SOCKET CLASS

 A TCP/IP client socket is used to create a reliable,


bi-directional, stream-based connection between
two computers on a network.
 A socket is simply an endpoint for

communications between the machines


 Socket class is used to connect to server sockets

& initiate protocol exchange


 It Establish connection between client and

server.
Constructor
Socket (String hostname, int port)
Socket(InetAddress ipaddr, int port)
METHODS OF THE SOCKET CLASS
 InetAddress getInetAddress() -- Returns the InetAddress that is
associated with the socket object.

int getPort() -- Returns the port number on which the socket is


connected

int getLocalPort() -- Returns the local port number on which the


socket is created

InputStream getInputStream() -- Returns the InputStream


associated with the calling object.

OutputStream getOutputStream() -- Returns the OutputStream


associated with the calling object.

void close() -- Closes the InputStream() and OutputStream() of


the socket.
TCP/IP SERVER SOCKET
 The TCP/IP server socket creates a socket
that listens for incoming connections.
 The server socket is implemented by creating

an instance of the ServerSocket class.


 The server socket creates a server on the

system to detect client connections.


 When you create server socket,an object of

this class register itself with a system having


interest in client connection.
CONSTRUCTORS

 ServerSocket(int port)
 ServerSocket(int port, int maxQ)
 ServerSocket(int port, int maxQ, InetAddress
address)
METHODS
 Public socket accept() -- Accepts an incoming
connection.
 Public int getLocalPort() -- Returns the port

number on which the server socket is


listening.
 Public void close() -- Closes the server

socket.
 public void bind(SocketAddress host)

 Public InetAddress getInetAddress()—Returns

local address of server socket.


 Public Boolean isClosed():-Return true if

socket has been closed.


THE FOLLOWING STEPS OCCUR WHEN
ESTABLISHING A TCP CONNECTION BETWEEN
TWO COMPUTERS USING SOCKETS −
 The server instantiates a ServerSocket object,
denoting which port number communication is to
occur on.
 The server invokes the accept() method of the

ServerSocket class. This method waits until a


client connects to the server on the given port.
 After the server is waiting, a client instantiates a

Socket object, specifying the server name and the


port number to connect to.
 The constructor of the Socket class attempts
to connect the client to the specified server
and the port number. If communication is
established, the client now has a Socket
object capable of communicating with the
server.
 On the server side, the accept() method

returns a reference to a new socket on the


server that is connected to the client's
socket.
 Once aconnection are

established,communication occurs using I/O


Streams ie InputStream & OutputStream.
UDP DATAGRAMSOCKETS&
DATAGRAMPACKET
 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.
CONSTRUCTORS OF DATAGRAMSOCKET CLASS

 DatagramSocket() throws
SocketException: it creates a datagram
socket and binds it with the available Port
Number on the localhost machine.
 DatagramSocket(int port) throws

SocketEeption: it creates a datagram


socket and binds it with the given Port
Number.
 DatagramSocket(int port, InetAddress

address) throws SocketEeption: it


creates a datagram socket and binds it with
the specified port number and host address.
METHODS
 public void send(DatagramPacket p) :Sends a
datagram packet from this socket.
 public void receive(DatagramPacket p)

Receives a datagram packet from this socket.


 public int getLocalPort():Returns the port

number on the local host to which this socket


is bound.
 public void close():Closes this datagram

socket.
 public InetAddress getLocalAddress():Gets

the local address to which the socket is


bound.
DATAGRAMPACKET CLASS

 It acts as data container(packet Information)


 It represent a data packet intended for

transmission using UDP


 Packets are container for a small sequence of

bytes & include addressing information such


as IP Address & Port.
 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.
CONSTRUCTORS
 DatagramPacket(byte[] data, int
length): it creates a datagram packet. This
constructor is used to receive the packets.
 DatagramPacket(byte[] data, int length,

InetAddress address, int port): it creates


a datagram packet. This constructor is used
to send the packets.
 DatagramPacket(byte[] buf, int offset,
int length):Constructs a DatagramPacket for
receiving packets of length length, specifying
an offset into the buffer.
 DatagramPacket(byte[] buf, int offset,

int length, InetAddress address,


int port):Constructs a datagram packet for
sending packets of length length with offset
ioffsetto the specified port number on the
specified host.
METHODS
 public byte[] getData():Returns the data
received or the data to be sent.
 public int getLength():Returns the length of

the data to be sent or the length of the data


received.
 public int getPort():Returns the port number

on the remote host to which this datagram is


being sent or from which the datagram was
received.
 public InetAddress getAddress():Returns the IP

address of the machine to which this


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

You might also like