[go: up one dir, main page]

0% found this document useful (0 votes)
236 views23 pages

Unit 2 Java Networking

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 23

JAVA NETWORKING

Jatin Ambasana
INDEX

1. Java Networking
2. Java Networking Terminology
3. Java Socket Programming
4. Java URL
5. Java Urlconnection Class
6. Java Inetaddress Class
7. Datagram
1. 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
2. Java Networking Terminology:

 The widely used java networking terminologies are given below:


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:
I. TCP
II. FTP
III. Telnet
IV. SMTP
V. POP etc.
Java Networking Terminology:

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 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
3. 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.
3.1 Socket class

 A socket is simply an endpoint for communications between the machines.


The Socket class can be used to create a socket.

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

2) public OutputStream returns the OutputStream attached with


getOutputStream() this socket.

3) public synchronized void close() closes this socket


3.2 ServerSocket class

 The ServerSocket class can be used to create a server socket.


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

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.


Program 1: Sending data from client to server

MyServu.java
import java.io.*;
import java.net.*;
public class MyServu {
public static void main(String[] args){
try{
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept();//establishes connection
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("message= "+str);
ss.close();
}catch(Exception e){System.out.println(e);}
}
}
Program 1: Sending data from client to server

MyClientu.java

import java.io.*;
import java.net.*;
public class MyClientu {
public static void main(String[] args) {
try{
Socket s=new Socket("localhost",6666);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello Server");
dout.flush();
dout.close();
s.close();
}catch(Exception e){System.out.println(e);}
}
}
4. 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 http://srpec.org.in
 A URL contains many information:
 Protocol: In this case, http is the protocol.
 Server name or IP Address: In this case, www.srpec.org.in is the
server name.
 Port Number: It is an optional attribute. If we write
http//srpec.org.in:80/contacts/ , 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.php is the file
name.
Commonly used methods of Java URL
class
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.

public URLConnection openConnection() it returns the instance of URLConnection i.e.


associated with this URL.
Example of Java URL class
import java.io.*; System.out.println("Host Name: "+url.get
import java.net.*; Host());

public class URLDemo{ System.out.println("Port Number: "+url.


getPort());
public static void main(String[] args){
System.out.println("File Name: "+url.ge
try{ tFile());
URL url=new URL("http://srpec.org.in/co
ntacts");
}catch(Exception e){System.out.println
(e);}
System.out.println("Protocol: "+url.getPro }
tocol());
}
5 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.
 The openConnection() method of URL class returns the object of
URLConnection class.
6 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.google.com, www.facebook.com etc.

Method Description
public static InetAddress it returns the instance of
getByName(String host) throws InetAddress containing LocalHost
UnknownHostException IP and name.
public String getHostName() it returns the host name of the IP
address.
public String getHostAddress() it returns the IP address in string
format.
6 Datagram:
 A datagram is an independent self-contained message
sent over the network whose arrival, arrival time and
content are not guaranteed.
 It is a basic transfer unit associated with a packet-
switched network.
 The java.net package has 3 classes to work with datagram
1. DatagramSocket
2. DatagramPacket
3. MulticastSocket
 An application can send or recive DatagramPacket using
DatagramSocket. In addition the DatagramPacket can be
broadcasted using MulticastSocket.
UDP Socket Programming or datagram:
 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.

Commonly used Constructors of DatagramSocket class


1. DatagramSocket() throws SocketEeption: it creates a datagram socket and
binds it with the available Port Number on the localhost machine.
2. DatagramSocket(int port) throws SocketEeption: it creates a datagram
socket and binds it with the given Port Number.
3. DatagramSocket(int port, InetAddress address) throws SocketEeption: it
creates a datagram socket and binds it with the specified port number and
host address.
RMI Architecture
 RMI stands for Remote Method Invocation.
 It is a mechanism that allows an object residing in one
system (JVM) to access/invoke an object running on
another JVM.
 RMI is used to build distributed applications.
 It provides remote communication between Java
programs.
 It is provided in the package java.rmi.
Client Server Application using RMI

 In an RMI application, we write two programs, a server


program (resides on the server) and a client
program (resides on the client).
 Inside the server program, a remote object is created and
reference of that object is made available for the client
(using the registry).
 The client program requests the remote objects on the
server and tries to invoke its methods.
Architecture of an RMI Application
The components of this architecture.
 Transport Layer − This layer connects the client
and the server. It manages the existing connection
and also sets up new connections.
 Stub − A stub is a representation (proxy) of the
remote object at client. It resides in the client
system; it acts as a gateway for the client
program.
 Skeleton − This is the object which resides on the
server side. Stub communicates with this skeleton
to pass request to the remote object.
 RRL(Remote Reference Layer) − It is the layer
which manages the references made by the client
to the remote object.
STUB
 The stub is an object, acts as a gateway for the client side. All
the outgoing requests are routed through it. It resides at the
client side and represents the remote object. When the caller
invokes method on the stub object, it does the following tasks:
 It initiates a connection with remote Virtual Machine (JVM),
 It writes and transmits (marshals) the parameters to the remote
Virtual Machine (JVM),
 It waits for the result
 It reads (unmarshals) the return value or exception, and
 It finally, returns the value to the caller.
SKELETON
 The skeleton is an object, acts as a gateway for the server side
object. All the incoming requests are routed through it. When
the skeleton receives the incoming request, it does the following
tasks:
 It reads the parameter for the remote method
 It invokes the method on the actual remote object, and
 It writes and transmits (marshals) the result to the caller.
 In the Java 2 SDK, an stub protocol was introduced that
eliminates the need for skeletons.
Running an RMI Application
For running an RMI example,
1) Compile all the java files
javac *.java
2) Create stub and skeleton object by rmic tool
rmic AdderRemote
3) Start rmi registry in one command prompt
rmiregistry 5000
4) Start the server in another command prompt
java MyServer
5) Start the client application in another command prompt
java MyClient

You might also like