0% found this document useful (0 votes)
8 views10 pages

MT Exp 1 Sunil Yadav

Uploaded by

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

MT Exp 1 Sunil Yadav

Uploaded by

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

Experiment No.

1
Middleware Technologies

Name: Sunilkumar Yadav


Class: DSE Working Professional B.Tech (Computer Engineering)
Batch: WP 2024-25
Roll No.: 124WPCM2007
Subject: Middleware Technologies(MT)

Aim:- To implement inter-process communication (IPC) using TCP in


Python, allowing a client and server to communicate over a network.

Algorithm / Procedure:

Server-Side:

1. Create a TCP socket.


2. Bind the socket to a specific port and listen for incoming client
connections.
3. Accept a connection from the client.
4. Receive data from the client.
5. Send a response back to the client.
6. Close the connection.

Client-Side:

1. Create a TCP socket.


2. Connect to the server using the server's IP address and port.
3. Send data to the server.
4. Receive the server's response.
5. Close the connection.
Program codes:

TCPServer.java

import java.io.*;

import java.net.*;

public class TCPServer {

public static void main(String[] args) {

try {

// Create a ServerSocket on port 65432

ServerSocket serverSocket = new ServerSocket(65432);

System.out.println("Server is waiting for a connection...");

// Wait for a client connection

Socket socket = serverSocket.accept();

System.out.println("Connection established with: " +


socket.getInetAddress());

// Set up input and output streams

BufferedReader in = new BufferedReader(new


InputStreamReader(socket.getInputStream()));

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);


// Receive data from the client

String clientMessage = in.readLine();

System.out.println("Received message from client: " +


clientMessage);

// Send a response to the client

String response = "Message received";

out.println(response);

// Close the connection

socket.close();

serverSocket.close();

} catch (IOException e) {

System.err.println("Server exception: " + e.toString());

e.printStackTrace();

TCPClient.java

import java.io.*;

import java.net.*;

public class TCPClient {

public static void main(String[] args) {


try {

// Create a socket and connect to the server at localhost on port


65432

Socket socket = new Socket("localhost", 65432);

// Set up input and output streams

PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

BufferedReader in = new BufferedReader(new


InputStreamReader(socket.getInputStream()));

// Send a message to the server

String message = "Hello, Server!";

out.println(message);

// Receive a response from the server

String serverResponse = in.readLine();

System.out.println("Received from server: " + serverResponse);

// Close the connection

socket.close();

} catch (IOException e) {

System.err.println("Client exception: " + e.toString());

e.printStackTrace();

UDPServer.java
import java.net.*;

public class UDPServer {

public static void main(String[] args) {

try {

DatagramSocket socket = new DatagramSocket(65432);

byte[] buffer = new byte[1024];

System.out.println("UDP Server is waiting for messages...");

while (true) {

DatagramPacket packet = new DatagramPacket(buffer,


buffer.length);

socket.receive(packet);

String message = new String(packet.getData(), 0,


packet.getLength());

System.out.println("Received message: " + message);

String response = "Message received";

DatagramPacket responsePacket = new


DatagramPacket(response.getBytes(), response.length(),

packet.getAddress(), packet.getPort());

socket.send(responsePacket);

} catch (Exception e) {

e.printStackTrace();

}
UDPClient.java

import java.net.*;

public class UDPClient {

public static void main(String[] args) {

try {

DatagramSocket socket = new DatagramSocket();

String message = "Hello, UDP Server!";

DatagramPacket packet = new


DatagramPacket(message.getBytes(), message.length(),

InetAddress.getByName("localhost"), 65432);

socket.send(packet);

byte[] buffer = new byte[1024];

DatagramPacket responsePacket = new DatagramPacket(buffer,


buffer.length);

socket.receive(responsePacket);

String response = new String(responsePacket.getData(), 0,


responsePacket.getLength());

System.out.println("Received from server: " + response);

socket.close();

} catch (Exception e) {

e.printStackTrace();

}
}

Output:

TCP
UDP
Conclusion:

In this experiment, we have successfully demonstrated how to implement


Inter-process Communication (IPC) using TCP sockets in Java. The
Server listens for incoming client connections, receives a message, and
sends a response. The Client connects to the server, sends a message,
and waits for a response. This experiment helps us understand how
processes can communicate over a network using TCP.
Review Questions:

1) What is Inter-Process Communication (IPC) in the context of


TCP/UDP?

· Inter-Process Communication (IPC) allows processes to exchange data,


either on the same machine or across a network. In TCP or UDP, IPC uses
networking protocols to enable communication between different
processes:

 TCP provides reliable, connection-based communication.


 UDP offers a connectionless, lightweight communication method,
without guaranteed delivery or order of packets.

2) ) What are some common challenges in implementing IPC


using TCP/UDP?

· Some common challenges include:

 Network latency: Delays in transmission.


 Reliability: Ensuring that messages are delivered correctly
(especially with UDP).
 Data synchronization: Ensuring that the client and server are in
sync during communication.
 Error handling: Gracefully managing connection drops, timeouts,
or malformed messages.
 Security: Preventing unauthorized access or tampering with the
communication.

3) Justify and explain whether you can implement UDP-based IPC


in Java?

Yes, UDP-based IPC can be implemented in Java using DatagramSocket


and DatagramPacket classes. Unlike TCP, UDP does not establish a
connection between the sender and receiver. It sends data in the form of
packets without guaranteed delivery.

You might also like