0% found this document useful (0 votes)
20 views12 pages

Ds Labfile Final

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)
20 views12 pages

Ds Labfile Final

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
You are on page 1/ 12

National Institute of Technology

Raipur

Distributed Systems Lab File

By
Deepali Paswan
Roll no - 21115034
(7th Semester, CSE)

DS lab file 1
S.No Experiment /Practical Pg.No

1 3
Implement concurrent echo client-server application in
JAVA

2 5
Implement a Distributed Chat Server using TCP Sockets in
JAVA

3 9
Implement concurrent day -time client-server application in
JAVA

4 13
Con figure following options on server socket and tests
them: SO _KEEPA LIV E, SO_LINGER, SO_SNDBUF,
SO_RCV BUF, TCP_ NODELAY

5
Write a program to Incrementing a counter in shared
memory in JAVA

6
Write a program to Simulate the Distributed Mutual
Exclusion.

7
Write a program to Implement Java RMI mechanism for
accessing method

8
Write a program to Create CORBA based server-client
application

DS lab file 2
Practical 1

Aim: Implement concurrent echo client-server application in java.

Theory: TCP stands for Transmission Control Protocol, a communications


standard that enables application programs and computing devices to exchange
messages over a network. It is designed to send packets across the internet and
ensure the successful delivery of data and messages over networks. TCP
organizes data so that it can be transmitted between a server and a client. It
guarantees the integrity of the data being communicated over a network. Before it
transmits data, TCP establishes a connection between a source and its
destination, which it ensures remains live until communication begins. It then
breaks large amounts of data into smaller packets, while ensuring data integrity is
in place throughout the process.

Code: We make two files.

tcpserver.java:

import java.io.*;
import.java.net.*;

public class TcpServer {


public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(8088);
System.out.println("server is ready!");
Socket ls = ss.accept();
while (true) {
System.out.println("Client Port is " + ls.getPort());
//READING DATA FROM CLIENT
InputStream is = ls.getInputStream();
byte data[] = new byte[50];
is.read(data);
String mfc = new String(data);
//mfc: message from client
mfc = mfc.trim();
String mfs = "The message was: " + mfc;
//mfs: message from server
//SENDING MSG TO CLIENT
OutputStream os = ls.getOutputStream();
os.write(mfs.getBytes());
}
}
}

Tcpclient.java:
import java.net.*;

DS lab file 3
import java.io.*;

class TcpClient {
public static void main(String[] args) throws Exception {
System.out.println("connecting to server");
Socket cs = new Socket("localhost", 8088);

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("The Local Port " + cs.getLocalPort() + "\nThe Remote Port " + cs.getPort());
System.out.println("The Local socket is " + cs);
System.out.println("Enter your name");
String str = br.readLine();
//SENDING DATA TO SERVER
OutputStream os = cs.getOutputStream();
os.write(str.getBytes());
//READING DATA FROM SERVER
InputStream is = cs.getInputStream();
byte data[] = new byte[50];
is.read(data);
//PRINTING MESSAGE ON CLIENT CONSOLE
String mfs = new String(data);
mfs = mfs.trim();
System.out.println(mfs);

OUTPUT:

DS lab file 4
Practical 2

Aim: Implement a Distributed Chat Server using TCP Sockets in JAVA.

Theory: We first define a graphical user interface for the chat boxes in ChatGUT
java file. This is written using Java AWT. Next, we create the Server app, which
implements the Runnable interface and has a server socket to connect to the
client. It also creates the server-side chat GUI. Finally, we create Client App.java
which uses the Chat GUI defined earlier and creates the chat box for the client.

Code:
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerApp implements Runnable{

/**
* @param args
*/
public static Socket s=null;
public static int i=1;
public static String clientName = "";
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
ServerSocket ss = new ServerSocket(8089);
ServerApp sa = new ServerApp();
Thread t;
try{
while(true){
System.out.println("Waiting for client "+i);
s = ss.accept();
i++;
t = new Thread(sa);
t.start();

}
}catch (Exception e) {
// TODO: handle exception
}
finally{
ss.close();
}
}
@Override
public void run() {
// TODO Auto-generated method stub

try
{
InputStream is = s.getInputStream();
byte[] b = new byte[1024];
is.read(b);
clientName="";
clientName = new String(b).trim();
}
catch (Exception e)
{
e.printStackTrace();
}
new ChatGUI(s,clientName);

DS lab file 5
}
}

ClientApp.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class ClientApp {

/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub

System.out.print("Enter your name:");


BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name = br.readLine();
Socket s = new Socket("localhost",8089);
OutputStream os = s.getOutputStream();
os.write(name.getBytes());
new ChatGUI(s,"Admin");
}

ChatGUI.java
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.net.Socket;
import java.net.SocketException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class ChatGUI extends JFrame implements ActionListener {


private static final long serialVersionUID = 1L;
Socket s;
JButton button;
JTextArea ta1, ta2;
String msg = "", title;
JScrollPane scrollPane1, scrollPane2;
InputStream is;
OutputStream os;

ChatGUI(Socket x, String str) {


s = x;
title = str;
button = new JButton("SEND");
ta1 = new JTextArea(5, 20);
ta2 = new JTextArea(5, 20);
ta1.setEditable(false);
scrollPane1 = new JScrollPane(ta1);

DS lab file 6
scrollPane2 = new JScrollPane(ta2);
setLayout(new FlowLayout());
add(scrollPane1);
add(scrollPane2);
add(button);
button.addActionListener(this);
setSize(300, 300);
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setTitle("Messenger " + title);
try {
is = s.getInputStream();
os = s.getOutputStream();
} catch (IOException ioe) {
}

try {
chat();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@SuppressWarnings("deprecation")
public void chat() throws Exception {
while (true) {
try {
byte data[] = new byte[50];
is.read(data);
msg = new String(data).trim();
ta1.append(title+": " + msg + "\n");
} catch (SocketException se) {
JOptionPane.showMessageDialog(this, "Disconnected from "+title);
this.dispose();
Thread.currentThread().stop();
}
}
}

public void actionPerformed(ActionEvent e) {


// TODO Auto-generated method stub
msg = ta2.getText();
try {
os.write(msg.getBytes());
} catch (IOException ioe) {
// TODO Auto-generated catch block
ioe.printStackTrace();
}
ta1.append("I: " + msg + "\n");
ta2.setText("");
}
}

Output:

DS lab file 7
DS lab file 8
Practical 3

Aim: Implement concurrent day -time client-server application in JAVA.


Theory: Server-Side Code Summary
Create and Bind Server: Instantiate a 'ServerSocket' on a specific port (e.g., 5000).
Listen and Accept Connections: Enter loop to listen for and accept incoming client connections.
Handle Client Request: For each client, retrieve the client's IP address and send the current date and time.
Display the client's IP address in the server console.
Manage Resources: Close the output stream and client socket after sending the response.
Shutdown: Close the server socket when shutting down the server.
Client-Side Code Summary: Create and Connect Client Socket: Create a 'Socket' and connect it to the server's
hostname and port.Send Client's IP Address: Retrieve the client's own IP address and send it to the
server.Receive and Display Server Response: Read the server's response (date and time) and display it.
Manage Resources: Close the input/output streams and the client socket after the interaction.
Shutdown: End the client program after the connection is properly closed.

Code:

DS lab file 9
Output:

DS lab file 10
Practical 4
Aim: Configure following options on server socket and tests them: SO_KEEPA LIVE, SO_LINGER, SO_SNDBUF,
SO_RCV BUF, TCP_NODELAY

Theory: Application programs need methods to control socket behavior, such as setting time-outs, managing
buffer space, enabling broadcasts, and handling out-of-band data. This ensures the socket functions according to
the application's needs.

Code:
Client.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketException;

public class Client {


public static void main(String[] args) {
try {
// Connect to the server on localhost:12345
Socket socket = new Socket("localhost", 12345);

// Enable SO_KEEPALIVE (TCP keep-alive)


socket.setKeepAlive(true);

// Enable SO_LINGER with a linger time of 5 seconds


socket.setSoLinger(true, 5);

// Set SO_SNDBUF to 64KB (64 * 1024 bytes)


int sendBufferSize = 64 * 1024;
socket.setSendBufferSize(sendBufferSize);

// Get input and output streams


BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);

// Send data to the server


writer.println("Hello, server!");

// Receive response from the server


String response = reader.readLine();
System.out.println("Received from server: " + response);

// Close the socket when done


socket.close();
} catch (SocketException e) {
// Handle socket-related exceptions
e.printStackTrace();
} catch (IOException e) {
// Handle IO-related exceptions
e.printStackTrace();
}
}
}
Server.Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;

DS lab file 11
public class Server {
public static void main(String[] args) {
try {
// Create a server socket on port 12345
ServerSocket serverSocket = new ServerSocket(12345);

System.out.println("Server listening on port 12345...");

// Accept client connection


Socket clientSocket = serverSocket.accept();
System.out.println("Client connected: " + clientSocket.getInetAddress().getHostAddress());

// Enable SO_KEEPALIVE (TCP keep-alive)


clientSocket.setKeepAlive(true);

// Enable SO_LINGER with a linger time of 5 seconds


clientSocket.setSoLinger(true, 5);

// Set SO_SNDBUF to 64KB (64 * 1024 bytes)


int sendBufferSize = 64 * 1024;
clientSocket.setSendBufferSize(sendBufferSize);

// Get input and output streams


BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true);

// Read and send data


String message;
while ((message = reader.readLine()) != null) {
System.out.println("Received from client: " + message);

// Send response back to the client


writer.println("Server response: " + message);
}

// Close the socket and server socket when done


clientSocket.close();
serverSocket.close();
} catch (SocketException e) {
// Handle socket-related exceptions
e.printStackTrace();
} catch (IOException e) {
// Handle IO-related exceptions
e.printStackTrace();
}
}
}

Output:

DS lab file 12

You might also like