Ds Labfile Final
Ds Labfile Final
Raipur
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
tcpserver.java:
import java.io.*;
import.java.net.*;
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);
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
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;
/**
* @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;
/**
* @param args
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
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;
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();
}
}
}
Output:
DS lab file 7
DS lab file 8
Practical 3
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;
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);
Output:
DS lab file 12