Program No. # 1
To implement simple chat server
//ChatServer.java
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatServer
{
public static void main(String args[])
{
try
{
ServerSocket SS = new ServerSocket(8000);
Socket S = SS.accept();
DataInputStream dis = new DataInputStream(S.getInputStream());
DataOutputStream dos = new DataOutputStream(S.getOutputStream());
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader (isr);
while (true)
{
System.out.println("Enter Ur Message");
dos.writeUTF(br.readLine());
System.out.println(S.getInetAddress()+" :"+ dis.readUTF());
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
//ChatClient.java
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatClient
{
public static void main(String args[])
{
try
{
Socket S = new Socket("10.10.54.170", 8000);
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader (isr);
DataInputStream dis = new DataInputStream(S.getInputStream());
DataOutputStream dos = new DataOutputStream(S.getOutputStream());
while (true)
{
System.out.println("Server:" + dis.readUTF());
System.out.println("Enter Ur Message");
dos.writeUTF(br.readLine());
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT:
Program No. # 2
To implement simple chat server using encryption(Caesar Cipher)
//ChatServer.java
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatServer
{
public static void main(String args[])
{
try
{
ServerSocket SS = new ServerSocket(8000);
Socket S = SS.accept();
DataInputStream dis = new DataInputStream(S.getInputStream());
DataOutputStream dos = new DataOutputStream(S.getOutputStream());
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader (isr);
String ct1="";
String pt2="";
int current=0;
while (true)
{
System.out.println("Enter Ur Message");
String pt1=br.readLine();
int len1=pt1.length();
for(int i=0;i<len1;i++)
{
current = pt1.charAt(i);
current=current+3;
ct1=ct1+""+(char)(current);
}
dos.writeUTF(ct1);
String ct2=dis.readUTF();
int len2=ct2.length();
for(int i=0;i<len2;i++)
{
current=0;
current = ct2.charAt(i);
current =current-3;
pt2=pt2+""+(char)(current);
}
System.out.println(S.getInetAddress()+" :"+ pt2);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
//ChatClient.java
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatClient
{
public static void main(String args[])
{
try
{
Socket S = new Socket("10.10.54.170", 8000);
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader (isr);
DataInputStream dis = new DataInputStream(S.getInputStream());
DataOutputStream dos = new DataOutputStream(S.getOutputStream());
String ct1="";
String pt2="";
int current=0;
while (true)
{
String ct2=dis.readUTF();
int len2=ct2.length();
for(int i=0;i<len2;i++)
{
current = ct2.charAt(i);
current =current-3;
pt2=pt2+""+(char)(current);
}
System.out.println("Server:" + pt2);
System.out.println("Enter Ur Message");
String pt1=br.readLine();
int len1=pt1.length();
for(int i=0;i<len1;i++)
{
current =0;
current = pt1.charAt(i);
current=current+3;
ct1=ct1+""+(char)(current);
}
dos.writeUTF(ct1);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUTPUT:
Program No. # 3
To implement Echo server
//EchoServer.java
import java.net.*;
import java.io.*;
public class EchoServer
{
//Initialize Port number and Packet Size
static final int serverPort = 1026;
static final int packetSize = 1024;
public static void main(String args[])throws SocketException
{
DatagramPacket packet;
DatagramSocket socket;
byte[] data; // For data to be Sent in packets
int clientPort;
InetAddress address;
String str;
socket = new DatagramSocket(serverPort);
for(;;)
{
data = new byte[packetSize];
// Create packets to receive the message
packet = new DatagramPacket(data,packetSize);
System.out.println("Waiting to receive the packets");
try
{
// wait infinetely for arrive of the packet
socket.receive(packet);
}
catch(IOException ie)
{
System.out.println(" Could not Receive :"+ie.getMessage());
System.exit(0);
}
// get data about client in order to echo data back
address = packet.getAddress();
clientPort = packet.getPort();
// print string that was received on server's console
str = new String(data,0,0,packet.getLength());
System.out.println("Message :"+ str.trim());
System.out.println("From :"+address);
// echo data back to the client
// Create packets to send to the client
packet = new DatagramPacket(data,packetSize,address,clientPort);
try
{
// sends packet
socket.send(packet);
}
catch(IOException ex)
{
System.out.println("Could not Send : "+ex.getMessage());
System.exit(0);
}
} // for loop
} // main
} // class EchoServer
//EchoClient.java
import java.net.*;
import java.io.*;
public class EchoClient
{
static final int serverPort = 1026;
static final int packetSize = 1024;
public static void main(String args[]) throws UnknownHostException, SocketException
{
DatagramSocket socket; //How we send packets
DatagramPacket packet; //what we send it in
InetAddress address; //Where to send
String messageSend; //Message to be send
String messageReturn; //What we get back from the Server
byte[] data;
//Checks for the arguments that sent to the java interpreter
// Make sure command line parameters correctr
if(args.length != 2)
{
System.out.println("Usage Error :Java EchoClient < Server name> < Message>");
System.exit(0);
}
// Gets the IP address of the Server
address = InetAddress.getByName(args[0]);
socket = new DatagramSocket();
data = new byte[packetSize];
messageSend = new String(args[1]);
messageSend.getBytes(0,messageSend.length(),data,0);
// remember datagrams hold bytes
packet = new DatagramPacket(data,data.length,address,serverPort);
System.out.println(" Trying to Send the packet ");
try
{
// sends the packet
socket.send(packet);
}
catch(IOException ie)
{
System.out.println("Could not Send :"+ie.getMessage());
System.exit(0);
}
// packet is reinitialized to use it for recieving
packet = new DatagramPacket(data,data.length);
try
{
// Receives the packet from the server
socket.receive(packet);
}
catch(IOException iee)
{
System.out.println("Could not receive : "+iee.getMessage() );
System.exit(0);
}
// display message received
messageReturn = new String (packet.getData(),0);
System.out.println("Message Returned : "+ messageReturn.trim());
} // main
} // Class EchoClient
OUTPUT:
Program No. # 4
To implement UDP chat server (taking a reply from server)
//UDPServer.java
import java.net.*;
import java.io.*;
public class UDPServer
{
public static void main(String args[])
{
DatagramSocket aSocket = null;
try
{
aSocket = new DatagramSocket(8000);
byte[] buffer = new byte[1000];
while(true)
{
DatagramPacket request = new DatagramPacket(buffer, buffer.length);
aSocket.receive(request);
System.out.println("Client: " + new String(request.getData()));
System.out.println("Enter ur message:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
byte[] bs = s.getBytes();
DatagramPacket reply1 = new DatagramPacket(bs, bs.length, request.getAddress(), request.getPort());
aSocket.send(reply1);
}
}
catch(SocketException e)
{
System.out.println("Socket: " + e.getMessage());
}
catch(IOException e)
{
System.out.println("IO: " + e.getMessage());
}
finally
{
if(aSocket != null)
aSocket.close();
}
}
}
//UDPClient.java
import java.net.*;
import java.io.*;
public class UDPClient
{
public static void main(String args[])
{
DatagramSocket aSocket = null;
try
{
aSocket = new DatagramSocket();
byte[] m = args[0].getBytes();
InetAddress aHost = InetAddress.getByName(args[1]);
int server_port = 8000;
DatagramPacket request = new DatagramPacket(m, args[0].length(), aHost, server_port);
aSocket.send(request);
byte[] buffer = new byte[1000];
DatagramPacket reply =new DatagramPacket(buffer, buffer.length);
aSocket.receive(reply);
System.out.println("Reply: " + new String(reply.getData()));
}
catch(SocketException e)
{
System.out.println("Socket: " + e.getMessage());
}
catch(IOException e)
{
System.out.println("IO: " + e.getMessage());
}
finally
{
if(aSocket != null)
aSocket.close();
}
}
}
OUTPUT:
Program No. # 5
To implement simple chat server (sending and receiving data from a file)
//ChatServerFile.java
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatServerFile
{
public static void main(String args[])
{
try
{
ServerSocket SS = new ServerSocket(8000);
Socket S = SS.accept();
String strLine;
FileInputStream fstream = new FileInputStream("C:\\NS Prog\\server.txt");
DataInputStream in = new DataInputStream(fstream);
DataInputStream dis = new DataInputStream(S.getInputStream());
DataOutputStream dos = new DataOutputStream(S.getOutputStream());
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader (isr);
System.out.println("Msg sent to client.....");
while ((strLine = br.readLine()) != null)
dos.writeUTF(strLine);
System.out.println("Client:" + dis.readUTF());
}
catch(Exception e)
{
System.out.println(e);
}
}
}
//ChatClientFile.java
import java.net.*;
import java.io.*;
import java.util.*;
public class ChatClientFile
{
public static void main(String args[])
{
try
{
Socket S = new Socket("localhost", 8000);
FileInputStream fstream = new FileInputStream("C:\\NS Prog\\client.txt");
DataInputStream in = new DataInputStream(fstream);
InputStreamReader isr = new InputStreamReader(in);
BufferedReader br = new BufferedReader (isr);
DataInputStream dis = new DataInputStream(S.getInputStream());
DataOutputStream dos = new DataOutputStream(S.getOutputStream());
String strLine;
System.out.println("Server:" + dis.readUTF());
System.out.println("Msg sent to server.....");
while ((strLine = br.readLine()) != null)
dos.writeUTF(strLine);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Input Files:-
OUTPUT:
Program No. # 6
To implement passing of data to server through multipoint.
//SimpleServer.java
import java.io.*;
import java.net.*;
public class SimpleServer implements Runnable
{
Socket csocket;
SimpleServer(Socket csocket)
{
this.csocket = csocket;
}
public static void main(String[] args)
{
try
{
// First we create a server socket and bind it to port 9999.
ServerSocket myServerSocket = new ServerSocket(9999);
// server processes incoming client connections forever...
while (true)
{
// wait for an incoming connection...
System.out.println("Server is waiting for an incoming connection on host=");
System.out.print(InetAddress.getLocalHost().getCanonicalHostName()+" port="+ myServerSocket.getLocalPort());
Socket skt = myServerSocket.accept();
new Thread(new SimpleServer(skt)).start();
}
}
catch (IOException ex)
{
ex.printStackTrace();
System.out.println("Whoops, something bad happened! I'm outta here.");
}
}
public void run()
{
try
{
// ok, got a connection. Let's use java.io. niceties to read and write from the connection.
BufferedReader inputFromClient = new BufferedReader(new InputStreamReader(csocket.getInputStream()));
PrintStream outputToClient = new PrintStream(csocket.getOutputStream());
boolean connectionClosed = false;
while(!connectionClosed)
{
// attempt to read input from the stream.
String buf = inputFromClient.readLine();
// if we got input, print it out and write a message back to the remote client..
if (buf != null)
{
System.out.println(csocket.getInetAddress()+": ["+ buf + "]");
outputToClient.println(buf);
}
else
{
connectionClosed = true;
}
}
// close the connection.
csocket.close();
System.out.println("Connection to client closed. Server is now going to wait for another connection.");
}
catch (IOException ex)
{
ex.printStackTrace();
System.out.println("Whoops, something bad happened! I'm outta here.");
}
}
}
//SimpleClient.java
import java.io.*;
import java.net.*;
public class SimpleClient
{
public static void main(String[] args)
{
// create a socket and find it to the host/port server is listening on.
String host;
int port;
if(args.length==0)
{
host = "localhost";
port = 9999;
}
else
{
host = args[0];
String portStr = args[1];
try
{
port = Integer.parseInt(portStr);
}
catch (NumberFormatException nfe)
{
System.out.println("Whoops, invalid port number. Will default to 9999");
port = 9999;
}
}
try
{
System.out.println("Client will attempt connecting to server at host="+ host +" port="+ port +".");
Socket skt = new Socket(host,port);
// ok, got a connection. Let's use java.io.* niceties to read and write from the connection.
BufferedReader myInput = new BufferedReader(new InputStreamReader(skt.getInputStream()));
BufferedReader consoleInput = new BufferedReader(new InputStreamReader(System.in));
PrintStream myOutput = new PrintStream(skt.getOutputStream());
boolean done = false;
while (!done)
{
// prompt and read from console
System.out.print("Enter a message, or enter \"done\" to quit: ");
String buf = consoleInput.readLine();
if(buf != null)
{
if(buf.equalsIgnoreCase("done"))
{
done = true;
}
else
{
// write something to the server.
myOutput.println(buf);
// see if the server echoes it back.
buf = myInput.readLine();
if(buf != null)
{
System.out.println("Client received ["+ buf + "] from the server!");
}
}
}
else
{
done = true;
}
}
// we're done, let's get out of dodge!
skt.close();
System.out.println("Client is exiting.");
}
catch (IOException ex)
{
ex.printStackTrace();
System.out.println("Whoops, something bad happened! I'm outta here.");
}
}
}
Program No. # 7
To implement remote method invocation.
//RS.java
import java.rmi.*;
import java.rmi.server.*;
public class RS extends UnicastRemoteObject implements RI
{
public RS() throws RemoteException
{
super(); //to call the super class Constructor form making RMI components distrubutes
}
public int mul(int a,int b) throws RemoteException
{
return(a*b);
}
public int divide(int a,int b) throws RemoteException
{
return(a/b);
}
public static void main(String aa[])
{
try
{
RS o = new RS();
Naming.bind("ashu",o); // arun is the logical name of o Object
}
catch(Exception ex){}
}
}
import java.rmi.server.*;
public class RS extends UnicastRemoteObject implements RI
{
public RS() throws RemoteException
{
super(); //to call the super class Constructor form making RMI components distrubutes
}
public int mul(int a,int b) throws RemoteException
{
return(a*b);
}
public int divide(int a,int b) throws RemoteException
{
return(a/b);
}
public static void main(String aa[])
{
try
{
RS o = new RS();
Naming.bind("ashu",o); // arun is the logical name of o Object
}
catch(Exception ex){}
}
}
//RC.java
import java.rmi.*;
class RC
{
public static void main(String aa[])
{
try
{
RI s = (RI) Naming.lookup("ashu");
int c = s.mul(100,20);
System.out.println("Product is = "+c);
c = s.divide(100,20);
System.out.println("Division is = "+c);
}
catch(Exception ex){ System.out.println("Exception - "+ex);}
}
}
//RI.java
import java.rmi.*;
class RC
{
public static void main(String aa[])
{
try
{
RI s = (RI) Naming.lookup("ashu");
int c = s.mul(100,20);
System.out.println("Product is = "+c);
c = s.divide(100,20);
System.out.println("Division is = "+c);
}
catch(Exception ex){ System.out.println("Exception - "+ex);}
}
}
//RI.java
// to create RemoteInterface with one or more methods extends with Remote
import java.rmi.*;
public interface RI extends Remote
{
int mul(int a,int b) throws RemoteException;
int divide(int a,int b) throws RemoteException;
}
OUTPUT:
No comments:
Post a Comment