0% found this document useful (0 votes)
7 views11 pages

Module 7

The document provides an overview of Java networking, covering key concepts such as IP addresses, port numbers, TCP and UDP protocols, and the java.net package. It explains client-server communication using Socket and ServerSocket classes, as well as UDP communication with DatagramSocket. Additionally, it introduces Remote Method Invocation (RMI) for distributed computing, detailing its architecture and implementation steps.

Uploaded by

sahooasis676
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)
7 views11 pages

Module 7

The document provides an overview of Java networking, covering key concepts such as IP addresses, port numbers, TCP and UDP protocols, and the java.net package. It explains client-server communication using Socket and ServerSocket classes, as well as UDP communication with DatagramSocket. Additionally, it introduces Remote Method Invocation (RMI) for distributed computing, detailing its architecture and implementation steps.

Uploaded by

sahooasis676
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

Java Programming Notes

Module 7: Networking and Advanced

7.1 Networking Fundamentals

Introduction

Computer networking allows multiple devices to communicate and share data over a network. In
Java, networking is primarily handled using the [Link] package.

ˆ Enables communication between computers


ˆ Used in web applications, distributed systems, and APIs
ˆ Based on protocols like TCP and UDP
IP Address

An IP address is a unique identier assigned to each device on a network.

ˆ IPv4: 32-bit address (e.g., [Link])


ˆ IPv6: 128-bit address (e.g., 2001:db8::1)
It helps identify the device and its location in the network.

Port Numbers

Ports are communication endpoints used to identify specic services on a device.

ˆ Range: 0 to 65535
ˆ Well-known ports (01023): HTTP (80), HTTPS (443)
ˆ Registered ports (102449151)
ˆ Dynamic ports (4915265535)
TCP vs UDP

ˆ TCP (Transmission Control Protocol)


 Connection-oriented
 Reliable and ordered delivery
 Slower due to error checking
ˆ UDP (User Datagram Protocol)
 Connectionless
 Faster but unreliable
 No guarantee of delivery

1
Connection Types

ˆ Connection-Oriented
 Requires connection setup
 Example: TCP
ˆ Connectionless
 No connection required
 Example: UDP

Streams in Networking

Streams are used for data transmission.

ˆ InputStream  Reads data


ˆ OutputStream  Writes data
Example: Simple Stream Usage

import [Link].*;

class StreamDemo {
public static void main(String[] args) throws Exception {
FileInputStream in = new FileInputStream("[Link]");
FileOutputStream out = new FileOutputStream("[Link]");

int data;
while((data = [Link]()) != -1) {
[Link](data);
}

[Link]();
[Link]();
}
}

Output:

File copied successfully

Important Points

ˆ IP address identies device


ˆ Port identies application
ˆ TCP is reliable, UDP is fast
ˆ Streams are used for communication

2
7.2 Java Networking Basics ([Link] Package)

Introduction

Java provides networking capabilities through the [Link] package. It allows applications to com-
municate over networks using TCP and UDP protocols.

ˆ Provides classes for networking operations


ˆ Supports both client and server communication
ˆ Used for building distributed applications
Important Classes in [Link]

ˆ InetAddress
ˆ Socket
ˆ ServerSocket
ˆ URL
ˆ URLConnection
InetAddress

ˆ Represents IP address of a host


ˆ Used to get host information
Example:
import [Link].*;

class Demo {
public static void main(String[] args) throws Exception {
InetAddress ip = [Link]("[Link]");

[Link]("Host Name: " + [Link]());


[Link]("IP Address: " + [Link]());
}
}

Socket (Client)

A Socket is used to connect a client to a server.

ˆ Establishes connection to server


ˆ Uses TCP protocol
Syntax:
Socket s = new Socket("localhost", 5000);

ServerSocket (Server)

ˆ Used to create server applications


3
ˆ Listens for client requests
Syntax:
ServerSocket ss = new ServerSocket(5000);
Socket s = [Link]();

URL and URLConnection

ˆ URL  Represents web address


ˆ URLConnection  Used to communicate with resource
Example:
import [Link].*;

class Demo {
public static void main(String[] args) throws Exception {
URL url = new URL("[Link]

[Link]("Protocol: " + [Link]());


[Link]("Host: " + [Link]());
}
}

Networking Exceptions

ˆ UnknownHostException
ˆ IOException
ˆ SocketException
These exceptions occur during network failures.

Important Points

ˆ Socket is used by client


ˆ ServerSocket is used by server
ˆ InetAddress provides IP details
ˆ URL is used for web resources
7.3 Client-Server Communication (TCP/IP)

Introduction

Client-server communication is a model where a client sends a request and a server responds to it.
In Java, this is implemented using Socket and ServerSocket classes.

ˆ Server waits for client request


ˆ Client initiates communication
ˆ Communication occurs using streams

4
Steps in TCP Communication

1. Server creates ServerSocket

2. Server waits using accept()

3. Client creates Socket

4. Streams are created for communication

5. Data is sent and received

One-Way Communication

In one-way communication, data ows only from client to server.


Server Program:
import [Link].*;
import [Link].*;

class Server {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(5000);
Socket s = [Link]();

DataInputStream dis =
new DataInputStream([Link]());

String msg = [Link]();


[Link]("Client: " + msg);

[Link]();
}
}

Client Program:
import [Link].*;
import [Link].*;

class Client {
public static void main(String[] args) throws Exception {
Socket s = new Socket("localhost", 5000);

DataOutputStream dos =
new DataOutputStream([Link]());

[Link]("Hello Server");

[Link]();
}
}

Output (Server):

Client: Hello Server

Two-Way Communication

In two-way communication, both client and server send and receive messages.
Server Program:

5
import [Link].*;
import [Link].*;

class Server {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(5000);
Socket s = [Link]();

DataInputStream dis =
new DataInputStream([Link]());
DataOutputStream dos =
new DataOutputStream([Link]());

String msg = [Link]();


[Link]("Client: " + msg);

[Link]("Hello Client");

[Link]();
}
}

Client Program:
import [Link].*;
import [Link].*;

class Client {
public static void main(String[] args) throws Exception {
Socket s = new Socket("localhost", 5000);

DataInputStream dis =
new DataInputStream([Link]());
DataOutputStream dos =
new DataOutputStream([Link]());

[Link]("Hello Server");

String reply = [Link]();


[Link]("Server: " + reply);

[Link]();
}
}

Output:

Server: Hello Client

Important Points

ˆ Server must start before client


ˆ Port number must match
ˆ TCP ensures reliable communication
ˆ Streams are used for data transfer

6
7.4 UDP Communication (DatagramSocket)

Introduction

UDP (User Datagram Protocol) is a connectionless communication protocol. Unlike TCP, it does
not guarantee delivery, ordering, or error checking.

ˆ Faster than TCP


ˆ No connection setup required
ˆ Used in real-time applications (video streaming, gaming)
DatagramSocket and DatagramPacket

ˆ DatagramSocket
 Used to send and receive packets
ˆ DatagramPacket
 Contains data, address, and port

Steps in UDP Communication

1. Create DatagramSocket

2. Create DatagramPacket

3. Send or receive data

UDP Sender Program

import [Link].*;

class Sender {
public static void main(String[] args) throws Exception {

DatagramSocket ds = new DatagramSocket();

String msg = "Hello UDP";

InetAddress ip = [Link]("localhost");

DatagramPacket dp =
new DatagramPacket([Link](),
[Link](), ip, 5000);

[Link](dp);

[Link]();
}
}

UDP Receiver Program

7
import [Link].*;

class Receiver {
public static void main(String[] args) throws Exception {

DatagramSocket ds = new DatagramSocket(5000);

byte[] buffer = new byte[1024];

DatagramPacket dp =
new DatagramPacket(buffer, [Link]);

[Link](dp);

String msg = new String([Link](), 0, [Link]());

[Link]("Received: " + msg);

[Link]();
}
}

Output:

Received: Hello UDP

TCP vs UDP

Feature TCP UDP


Connection Connection-oriented Connectionless
Reliability Reliable Unreliable
Speed Slower Faster
Data Order Maintained Not guaranteed

Important Points

ˆ UDP is faster but unreliable


ˆ No connection establishment required
ˆ Used in real-time applications
ˆ Uses DatagramSocket and DatagramPacket
7.5 Remote Method Invocation (RMI)

Introduction

Remote Method Invocation (RMI) allows a Java program to invoke methods on an object located on
another machine (remote system).

ˆ Enables distributed computing


ˆ Works on client-server model
ˆ Uses object-oriented communication
8
RMI Architecture

RMI consists of:

ˆ Client  invokes remote method


ˆ Server  provides remote object
ˆ Stub  proxy for remote object
ˆ RMI Registry  stores remote object reference
Working of RMI

1. Server creates remote object

2. Object is registered in RMI registry

3. Client looks up registry

4. Client invokes method via stub

Steps to Implement RMI

1. Create remote interface

2. Implement interface

3. Create server program

4. Create client program

5. Start RMI registry

6. Run server and client

Step 1: Remote Interface

import [Link].*;

public interface Add extends Remote {


int sum(int a, int b) throws RemoteException;
}

Step 2: Implementation

import [Link].*;
import [Link].*;

public class AddImpl extends UnicastRemoteObject


implements Add {

AddImpl() throws RemoteException {}

public int sum(int a, int b) {


return a + b;
}
}

9
Step 3: Server Program

import [Link].*;

public class Server {


public static void main(String[] args) {
try {
Add obj = new AddImpl();

[Link]("rmi://localhost/AddService", obj);

[Link]("Server ready");
} catch(Exception e) {
[Link]();
}
}
}

Step 4: Client Program

import [Link].*;

public class Client {


public static void main(String[] args) {
try {
Add obj = (Add) [Link](
"rmi://localhost/AddService");

int result = [Link](5, 3);

[Link]("Result: " + result);


} catch(Exception e) {
[Link]();
}
}
}

Execution Steps

1. Compile all files


2. Start RMI registry:
rmiregistry
3. Run server:
java Server
4. Run client:
java Client

Output:

Result: 8

Advantages of RMI

ˆ Supports distributed applications


ˆ Object-oriented communication
10
ˆ Simplies remote interaction
Important Points

ˆ Remote interface must extend Remote


ˆ Methods must throw RemoteException
ˆ RMI registry must be running
ˆ Stub handles communication

11

You might also like