Ex No 6 CN
Ex No 6 CN
Aim:
To use Socket programming for developing client/server application for Customer Management
service
Theory:
Reliable Unreliable
1
22IT012
3. IP Address:
● An IP address (Internet Protocol address) is a numerical label assigned to each
device connected to a computer network that uses the Internet Protocol for
communication.IP addresses are used for various purposes, including identifying
the source and destination of data packets on a network, routing data across the
Internet, and providing location information for devices. They are essential for
the functioning of the Internet and other computer networks.
4. Port number:
● A port number is a 16-bit unsigned integer used to uniquely identify specific
communication endpoints, or network services, within a host in a computer
network. In the Internet Protocol (IP) context, port numbers are combined with
IP addresses to specify the destination or source of data packets.
5. Any 6 methods available in Java for TCP/Connection oriented sockets with explanations
○ ServerSocket:
■ The ServerSocket class is used to create a server-side socket that
listens for incoming TCP connections on a specific port.
■ You can use the accept() method of ServerSocket to accept
incoming connections and obtain a Socket object for
communication with the client.
○ Socket:
2
22IT012
6. Any 6 methods available in Java for UDP/Connection less sockets with explanations
o DatagramSocket:
The DatagramSocket class is used to create a socket for
sending and receiving datagrams (packets) over a UDP
connection.
You can create a DatagramSocket object to send and
receive datagrams on a specific port.
o DatagramPacket:
The DatagramPacket class represents a packet of data to
be sent or received over a UDP connection.
You can create a DatagramPacket object to encapsulate
data, along with the destination IP address and port.
o send() Method:
The send() method of DatagramSocket is used to send a
DatagramPacket over the UDP connection.
You can use this method to send datagrams to a specific
destination.
3
22IT012
o receive() Method:
The receive() method of DatagramSocket is used to receive a
DatagramPacket over the UDP connection.
You can use this method to receive datagrams sent by remote
hosts.
o getData() and setData() Methods:
The getData() method of DatagramPacket retrieves the data
from the packet.
The setData() method of DatagramPacket sets the data to be
sent in the packet.
o Close Method:
As with TCP, always call the close() method on sockets to
release system resources after you're done using them.
Algorithm:
Server Initialization:
Client Initialization:
4
22IT012
4. Extract the message from the packet and print it to the console.
Repeat:
Go back to step 3 for the client and step 4 for the server.
Close Sockets:
Server Initialization:
Client Initialization:
1. Create a Socket object to connect to the server's IP address and port number.
2. Start a loop to continuously send and receive messages.
Repeat:
5
22IT012
Close Sockets:
1. Server Initialization:
Create a ServerSocket object and bind it to a specific port (12345 in this
case).
Print a message indicating that the server is running.
2. Accepting Client Connections:
Inside an infinite loop, wait for incoming client connections using the
accept() method of ServerSocket.
Once a client connection is established, print the client's IP address.
3. Object Streams Initialization:
Create ObjectInputStream and ObjectOutputStream for the client
socket's input and output streams, respectively.
4. Receiving and Processing Data:
Inside an inner loop (for each client connection), continuously receive
Customer objects from the client using the ObjectInputStream's
readObject() method.
Print the received Customer object to the console.
Echo back the received Customer object to the client by writing it to
the ObjectOutputStream's output stream using the writeObject()
method.
5. Error Handling and Cleanup:
Catch and handle IOException and ClassNotFoundException if they
occur during socket operations or object deserialization.
Close the ServerSocket in a finally block to release the port after the
server finishes execution.
1. Server Initialization:
Create a DatagramSocket object and bind it to a specific port (e.g.,
12345).
Print a message indicating that the server is running.
2. Receiving and Processing Data:
Inside an infinite loop, wait to receive DatagramPackets containing data
from clients using the receive() method of DatagramSocket.
Extract the data from the received DatagramPacket and process it (in
this case, echoing back the received message).
6
22IT012
3. Sending Response:
Create a new DatagramPacket containing the response message.
Set the packet's destination address and port to match the address and
port of the client from which the original packet was received.
Send the response packet back to the client using the
DatagramSocket's send() method.
4. Error Handling and Cleanup:
Catch and handle any IOExceptions that may occur during socket
operations.
Optionally, handle malformed or unexpected packets received from
clients.
Close the DatagramSocket in a finally block to release the port after the
server finishes execution.
Coding:
// Server.java
import java.io.*;
import java.net.*;
try {
System.out.println("Server is running...");
while (true) {
DatagramPacket(receiveData, receiveData.length);
socket.receive(receivePacket);
InetAddress clientAddress =
7
22IT012
receivePacket.getAddress();
String(receivePacket.getData()).trim();
BufferedReader(new InputStreamReader(System.in));
sendData = replyMessage.getBytes();
DatagramPacket(sendData, sendData.length,
clientAddress, clientPort);
socket.send(sendPacket);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (socket != null)
socket.close();
// Client.java
import java.io.*;
import java.net.*;
8
22IT012
try {
InetAddress serverAddress =
System.out.println("Client is running...");
while (true) {
BufferedReader(new InputStreamReader(System.in));
sendData = message.getBytes();
DatagramPacket(sendData, sendData.length,
serverAddress, serverPort);
socket.send(sendPacket);
DatagramPacket(receiveData, receiveData.length);
socket.receive(receivePacket);
String(receivePacket.getData()).trim();
replyMessage);
} catch (IOException e) {
e.printStackTrace();
9
22IT012
} finally {
if (socket != null)
socket.close();
//Server2.java
import java.io.*;
import java.net.*;
BufferedReader in = null;
try {
System.out.println("Server is running...");
in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
String inputLine;
BufferedReader(new InputStreamReader(System.in));
10
22IT012
out.println(replyMessage);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null)
out.close();
if (in != null)
in.close();
if (clientSocket != null)
clientSocket.close();
if (serverSocket != null)
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
//Client2.java
import java.io.*;
import java.net.*;
11
22IT012
BufferedReader in = null;
try {
String userInput;
out.println(userInput);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null)
out.close();
if (in != null)
in.close();
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
12
22IT012
//Client.java
import java.io.*;
import java.net.*;
try {
System.out.println("Client is running...");
while (true) {
System.out.print("Name: ");
13
22IT012
System.out.print("Address: ");
customer.input(amount);
oos.writeObject(customer);
oos.flush();
e.printStackTrace();
} finally {
try {
} catch (IOException e) {
e.printStackTrace();
14
22IT012
//Server.java
import java.io.*;
import java.net.*;
try {
System.out.println("Server is running...");
while (true) {
while (true) {
15
22IT012
oos.writeObject(replyMessage);
oos.flush();
e.printStackTrace();
} finally {
try {
} catch (IOException e) {
e.printStackTrace();
//Customer.java
import java.io.Serializable;
String name;
String address;
double phoneNumber;
this.name = name;
16
22IT012
this.address = address;
this.phoneNumber = phoneNumber;
this.amount = amount;
// For example:
@Override
return "Name: " + name + ", Address: " + address + ", Phone Number: " + phoneNumber + ",
Points: " + points;
//Server.java
import java.io.*;
import java.net.*;
17
22IT012
try {
System.out.println("Server is running...");
while (true) {
while (true) {
oos.writeObject(replyMessage);
oos.flush();
e.printStackTrace();
} finally {
18
22IT012
try {
} catch (IOException e) {
e.printStackTrace();
//Client.java
import java.io.*;
import java.net.*;
try {
System.out.println("Client is running...");
19
22IT012
while (true) {
System.out.print("Name: ");
System.out.print("Address: ");
customer.input(amount);
oos.writeObject(customer);
oos.flush();
20
22IT012
e.printStackTrace();
} finally {
try {
} catch (IOException e) {
e.printStackTrace();
//Customer.java
import java.io.Serializable;
String name;
String address;
double phoneNumber;
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
21
22IT012
this.amount = amount;
@Override
return "Name: " + name + ", Address: " + address + ", Phone Number: " + phoneNumber + ",
Points: " + points;
Output:
22
22IT012
23
22IT012
Result:
The client- server using TCP and UDP protocols are executed successfully and the outputs
are recorded.
24
22IT012
Viva 5
Sub Total 20
Documentation 7
Sub Total 10
25