Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

1.

TCP MULTIPLE CHAT:


Client.java:
import java.io.*;
import java.net.*;
public class Client {
public static void main(String argv[]) throws Exception {
String ip;
String op;
Socket clientSocket=null;
BufferedReader in2 = new BufferedReader(new InputStreamReader(System.in));
clientSocket = new Socket("localhost", 8583);
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
System.out.println("Ready");
op = in2.readLine();
out.println(op);
while(!(out.checkError())){
if((ip = in.readLine())!=null){
System.out.println("Server-->"+ip);
System.out.println("Server-->"+(ip=in.readLine()));
if(ip.equals("Bye")) break;
op = in2.readLine();
out.println(op);
}
if (op.equals("Bye"))
break;
}
System.out.println("Connection Closed");
clientSocket.close();
}
}
Server.java:
import java.io.*;
import java.net.*;
public class Server {
private static int counter = 0;
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
boolean listeningSocket = true;
try {
serverSocket = new ServerSocket(8583);
} catch (IOException e) {
System.err.println("Could not listen on port: 8583");
}
while(listeningSocket){
Socket clientSocket = serverSocket.accept();
TServer mini = new TServer(clientSocket,++counter);
mini.start();
}
serverSocket.close();
}
}
TServer.java:
import java.io.*;
import java.net.*;
public class TServer extends Thread{
private Socket socket = null;
private String id;
public TServer(Socket socket,int c) {
super("TServer");
id="Client "+c;
this.socket = socket;
}
public void run(){
try (
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
) {
String ip,op=null;
boolean i=true;
BufferedReader in2 = new BufferedReader(new InputStreamReader(System.in));
while (!(out.checkError())&&i) {
long StartTime = System.currentTimeMillis() / 1000;
while(i){
if((ip = in.readLine())!=null){
System.out.println(id+"-->"+ip);
out.println("1");
if(ip.equals("Bye")) break;
System.out.println("Reply to "+id+":");
op = in2.readLine();
out.println(op);
}
if (op.equals("Bye"))
break;
if(200<((System.currentTimeMillis() / 1000)-StartTime)){
i=false;
System.out.println("200 seconds up");
}
}
}
System.out.println("Connection Closed with "+id);
socket.close();
}
catch(Exception e)
{
System.err.println(e);
}
}
}

2.FINDING CLIENT IP:
aServer.java:
import java.net.*;
import java.io.*;
import java.util.*;
class aServer
{
public static void main(String args[])
{
ServerSocket ss;
Socket s;
String inet;
DataInputStream dis;
try
{
ss=new ServerSocket(8020);
while(true)
{
s=ss.accept();
dis=new DataInputStream(s.getInputStream());
inet=dis.readLine();
System.out.println("THE CLIENT SYSTEM ADDRESS IS :"+inet);
}
}
catch(IOException e)
{
System.out.println("The exception is :"+e);
}
}
}
aClient.java:
import java.net.*;
import java.io.*;
class aClient
{
public static void main (String args[])
{
Socket soc;
PrintStream ps;
try
{
InetAddress ia=InetAddress.getLocalHost();
soc=new Socket(ia,8020);
ps=new PrintStream(soc.getOutputStream());
ps.println(ia);
}
catch(IOException e)
{
System.out.println("THE EXCEPTION is :"+e);

}
}
}

3.UDP MULTICAST MESSAGE:
mServer.java:
import java.io.*;
import java.net.*;
public class mServer {
public static void main(String[] args) {
DatagramSocket socket = null;
DatagramPacket outPacket = null;
BufferedReader dis;
byte[] outBuf;
final int PORT = 8888;
try {
socket = new DatagramSocket();
dis=new BufferedReader(new InputStreamReader(System.in));
String msg;
while (!((msg =dis.readLine()).equalsIgnoreCase("bye"))) {
outBuf = msg.getBytes();
InetAddress address = InetAddress.getByName("224.2.2.3");
outPacket = new DatagramPacket(outBuf, outBuf.length, address, PORT);
socket.send(outPacket);
System.out.println("Server sends : " + msg);
}
} catch (IOException ioe) {
System.out.println(ioe);
}
}
}
mClient.java:
import java.io.*;
import java.net.*;
public class mClient {
public static void main(String[] args) {
MulticastSocket socket = null;
DatagramPacket inPacket = null;
byte[] inBuf = new byte[256];
try {
socket = new MulticastSocket(8888);
InetAddress address = InetAddress.getByName("224.2.2.3");
socket.joinGroup(address);

while (true) {
inPacket = new DatagramPacket(inBuf, inBuf.length);
socket.receive(inPacket);
String msg = new String(inBuf, 0, inPacket.getLength());
System.out.println("From " + inPacket.getAddress() + " Msg : " + msg);
}
} catch (IOException ioe) {
System.out.println(ioe);
}
}
}

4.DOWNLOAD WEBPAGE:
import java.io.*;
import java.net.*;
public class webPage
{
public static void main(String[] paramArrayOfString)
throws Exception
{
BufferedReader in1 = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the URL: ");
String str1 = in1.readLine();
URL localURL = new URL(str1);
BufferedReader in2 = new BufferedReader(new InputStreamReader(localURL.openStream()));
BufferedWriter out = new BufferedWriter(new FileWriter("data.html"));
String str2;
while ((str2 = in2.readLine()) != null) {
System.out.println(str2);
out.write(str2);
out.newLine();
}
in2.close();
out.close();
}
}

5.PING PGM:
import java.io.*;
import java.net.*;
public class ping{
public static void main(String[] args)
throws Exception {
String s;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the URL: ");
s=in.readLine();
boolean chk = pg(s);
if(chk) System.out.println("The host "+s+" is reachable");
else System.out.println("The host "+s+" is not reachable");
}
public static boolean pg(String host)
{
boolean isReachable = false;
try {
Process proc = new ProcessBuilder("ping", host).start();
int exitValue = proc.waitFor();
System.out.println("Exit Value:" + exitValue);
if(exitValue == 0)
isReachable = true;
} catch (IOException e1) {
System.out.println(e1.getMessage());
e1.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
return isReachable;
}
}
6.UDP FILE TRANSFER:
CLIENT:
import java.net.*;
import java.io.*;
class udpClient
{
public static void main(String args[])throws IOException
{
byte b[]=new byte[1024];
DatagramSocket ds=new DatagramSocket(5000);
DatagramPacket dp=new DatagramPacket(b,1024);
DataInputStream dis=new DataInputStream(System.in);
System.out.println("Enter file name\n");
String str1;
str1=dis.readLine();
b=str1.getBytes();
ds.send(new DatagramPacket(b,b.length,InetAddress.getLocalHost(),5001));
System.out.println("Contents of file:"+str1+"\n");
while(str1!=null)
{
ds.receive(dp);
str1=new String(dp.getData(),0,dp.getLength());
System.out.println(str1);
}
ds.close();
}
}
SERVER.JAVA:
import java.net.*;
import java.io.*;
class udpServer
{
public static void main(String args[])throws IOException
{
byte b[]=new byte[1024];
DatagramSocket ds=new DatagramSocket(5001);
DatagramPacket dp=new DatagramPacket(b,1024);
ds.receive(dp);
String str=new String(dp.getData(),0,dp.getLength());
System.out.println("Received file "+str);
FileReader fr=new FileReader(str);
BufferedReader br1=new BufferedReader(fr);
while((str=br1.readLine())!=null)
{
b=str.getBytes();
ds.send(new DatagramPacket(b,b.length,InetAddress.getLocalHost(),5000));
}
ds.close();
}
}

7.HTTP:
import java.io.*;
import java.net.*;

/**
* This program connects to a Web server and downloads the specified URL
* from it. It uses the HTTP protocol directly.
**/
public class HttpClient {
public static void main(String[] args) {
try {
// Check the arguments
if ((args.length != 1) && (args.length != 2))
throw new IllegalArgumentException("Wrong number of arguments");

// Get an output stream to write the URL contents to
OutputStream to_file;
if (args.length == 2) to_file = new FileOutputStream(args[1]);
else to_file = System.out;

// Now use the URL class to parse the user-specified URL into
// its various parts: protocol, host, port, filename. Check the protocol
URL url = new URL(args[0]);
String protocol = url.getProtocol();
if (!protocol.equals("http"))
throw new IllegalArgumentException("URL must use 'http:' protocol");
String host = url.getHost();
int port = url.getPort();
if (port == -1) port = 80; // if no port, use the default HTTP port
String filename = url.getFile();
// Open a network socket connection to the specified host and port
Socket socket = new Socket(host, port);
// Get input and output streams for the socket
InputStream from_server = socket.getInputStream();
PrintWriter to_server =
new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));

// Send the HTTP GET command to the Web server, specifying the file.
// This uses an old and very simple version of the HTTP protocol
to_server.println("GET " + filename);
to_server.flush(); // Send it right now!

// Now read the server's response, and write it to the file
byte[] buffer = new byte[4096];
int bytes_read;
while((bytes_read = from_server.read(buffer)) != -1)
to_file.write(buffer, 0, bytes_read);

// When the server closes the connection, we close our stuff
socket.close();
to_file.close();
}
catch (Exception e) { // Report any errors that arise
System.err.println(e);
System.err.println("Usage: java HttpClient <URL> [<filename>]");
}
}
}
8.RPC FILE TRANSFER
import java.rmi.Remote; import java.rmi.RemoteException; public
interface FileInterface extends Remote { public byte[]
downloadFile(String fileName) throws RemoteException; }

import java.io.*;

import java.rmi.*;

import java.rmi.server.UnicastRemoteObject;

public class FileImpl extends UnicastRemoteObject implements FileInterface {

private String name;

public FileImpl(String s) throws RemoteException {

super();

name = s;

}

public byte[] downloadFile(String fileName){

try {

File file = new File(fileName);

byte buffer[] = new byte[(int)file.length()];

BufferedInputStream input = new

BufferedInputStream(new FileInputStream(fileName));

input.read(buffer,0,buffer.length);

input.close();

return(buffer);

} catch(Exception e){

System.out.println("FileImpl: "+e.getMessage());

e.printStackTrace();

return(null);

}

} }

import java.io.*;

import java.rmi.*;

public class FileServer {

public static void main(String argv[]) {

try {

FileInterface fi = new FileImpl("FileServer");

Naming.rebind("//127.0.0.1/FileServer", fi);

} catch(Exception e) {

System.out.println("FileServer: "+e.getMessage());

e.printStackTrace();

}

} }

import java.io.*;

import java.rmi.*;

public class FileClient{

public static void main(String argv[]) {

if(argv.length != 2) {

System.out.println("Usage: java FileClient fileName machineName");

System.exit(0);

}try {

String name = "//" + argv[1] + "/FileServer";

FileInterface fi = (FileInterface) Naming.lookup(name);

byte[] filedata = fi.downloadFile(argv[0]);

File file = new File(argv[0]);

BufferedOutputStream output = new BufferedOutputStream(new
FileOutputStream(file.getName()));

output.write(filedata,0,filedata.length);

output.flush();

output.close();

} catch(Exception e) {

System.err.println("FileServer exception: "+ e.getMessage());

e.printStackTrace();

}

} }

9.RPC ARRAY SORT
import java.net.*;
import java.rmi.*;
public class strs{
public static void main(String args[])throws Exception
{
strimp st=new strimp();
Naming.rebind("strs",st);
}
}

import java.rmi.*;
public interface strint extends Remote
{
int[] strev(int[] s)throws Exception;
}

import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
public class strimp extends UnicastRemoteObject implements strint
{
public strimp()throws Exception{}
public int[] strev(int arr[]){
for(int i=0;i<arr.length-1;i++)
{ for(int j=i+1;j<arr.length;j++){
if(arr[i]>arr[j])
{
int a=arr[i];
arr[i]=arr[j];
arr[j]=a;
}
}
}
return arr;
}
}

import java.rmi.*;
import java.io.*;
public class strc{
public static void main(String args[])throws Exception
{
int arr[]=new int[5];
BufferedReader r=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter any five numbers: ");
for(int i=0;i<5;i++)
arr[i]=Integer.parseInt(r.readLine());
String url="rmi://"+args[0]+"/strs";
strint st=(strint)Naming.lookup(url);
arr=st.strev(arr);
for(int i=0;i<5;i++)
System.out.println(arr[i]);
}
}

You might also like