You are on page 1of 4

Echo:

//server:
import java.io.*;
import java.lang.*;
import java.net.*;
class server
{
public static void main (String args[]) throws Exception
{
Socket s;
ServerSocket ss;
DataOutputStream dos;
DataInputStream dis;
String str;
try
{
BufferedReader br= new BufferedReader (new InputStreamReader(System.in)) ;
ss=new ServerSocket(1000);
System.out.println("Waiting for client...");
s=ss.accept();
System.out.println("Client request accepted");
dos= new DataOutputStream(s.getOutputStream());
dis= new DataInputStream(s.getInputStream());
int i=0;
while(true)
{
System.out.println("Type message to transmit....'q'to Quit");
str=br.readLine();
if(str.equals("q") || str.equals("Q"))
break;
dos.writeUTF(str);
dos.flush();
System.out.println("Message Sent");
Thread.sleep(1000);
i=dis.read();
System.out.println("Message "+i+" delivered");
}//while
s.close();
ss.close();
dos.close();
dis.close();
}//try
catch (Exception e)
{
System.out.println(e.getMessage());
}}}

Client:
import java.io.*;
import java.lang.*;
import java.net.*;
class client
{
public static void main(String args[]) throws Exception
{
Socket s;
DataOutputStream dos;
DataInputStream dis;
String str;
int ack;
try
{
s=new Socket("",1000);
System.out.println("connection established...");
dis= new DataInputStream(s.getInputStream());
dos= new DataOutputStream(s.getOutputStream());
ack=0;
while(true)
{
str=dis.readUTF();
if(str.equals("q") || str.equals("Q"))
break;
System.out.println("Message received "+str);
dos.write(ack++);
dos.flush();
}//while
s.close();
dos.close();
dis.close();
}//try
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}

File Transfer :
//Server:
import java.net.*;
import java.io.*;
public class ftpserver {

public static void main(String[] args) throws IOException {


ServerSocket servsock = new ServerSocket(13267);
while(true)
{
System.out.println("waiting...");
Socket sock = servsock.accept();
System.out.println("Accept connection:"+sock);
File myfile = new File("input.txt");
byte[] mybytearray = new byte[(int) myfile.length()];
FileInputStream fis = new FileInputStream(myfile);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(mybytearray,0,mybytearray.length);
OutputStream os = sock.getOutputStream();
System.out.println("Sending");
os.write(mybytearray,0,mybytearray.length);
os.flush();
sock.close();
}}}

//Client:

import java.net.*;
import java.io.*;
public class ftpclient{
public static void main(String[] args) throws IOException{
int filesize=60022386;
//long start=System.currentTimeMillis();
int bytesread;
int current=0;
Socket sock=new Socket("127.0.0.1",13267);
System.out.println("connecting");
byte[] mybytearray=new byte[filesize];
InputStream is=sock.getInputStream();
FileOutputStream fos=new FileOutputStream("output.txt");
BufferedOutputStream bos=new BufferedOutputStream(fos);
bytesread = is.read(mybytearray,0,mybytearray.length);
current=bytesread;
do{
bytesread=is.read(mybytearray,current,mybytearray.length-current);
if(bytesread>=0)
current+=bytesread;
}while((bytesread)>-1);
bos.write(mybytearray,0,current);
bos.flush();
//long tm=System.currentTimeMillis();
bos.close();
sock.close();
}
}

RMI:
//Server:

import java.util.*;
import java.io.*;
import java.rmi.*;
import javax.rmi.*;
import java.rmi.server.*;
public class serverIMPL extends UnicastRemoteObject implements intf
{
public serverIMPL() throws RemoteException
{super();
}
public int add(int a,int b) throws RemoteException
{
return(a+b);
}
public int sub(int a1,int b1) throws RemoteException
{
return(a1-b1);
}

public static void main (String s[]) throws Exception


{
serverIMPL impl=new serverIMPL();

Naming.rebind("Sweety",impl);
//System.out.println(“registered………”);
}
}
//Client:

import java.util.*;
import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
public class clientrmi
{
public static void main (String s[]) throws Exception
{
try
{
String url="rmi://127.0.0.1/Sweety";
intf intf1=(intf)Naming.lookup(url);
int a =intf1.add(10,20);
int b=intf1.sub(30,10);
System.out.println(a);
System.out.println(b);
}
catch(Exception e){}
}
}

// Interface

import java.util.*;
import java.io.*;
import java.rmi.*;
import java.rmi.server.*;
public interface intf extends Remote
{
public int add(int a,int b)throws RemoteException;
public int sub(int a1,int b1) throws RemoteException;
}

You might also like