You are on page 1of 10

FILE TRANSFER PROTOCOL

//FTP Server
import java.io.*;
import java.net.*;
class ftpserver
{
public static void main(String args[])throws Exception
{
String filename;
ServerSocket serverw=new ServerSocket(9981);
System.out.println("\n ftp server running");
while(true)
{
Socket serverc=serverw.accept();
BufferedReader informclient=new BufferedReader(new
InputStreamReader(serverc.getInputStream()));
filename=informclient.readLine();
BufferedReader br=new BufferedReader(new FileReader(filename));
System.out.println("\n file received");
System.out.println("\n filename"+filename);
System.out.println("\n the content of the file are");
String out=br.readLine();
while(!out.equals("ends"));
{
System.out.println("\n \t"+out);
out=br.readLine();
if(out==null)
break;
}
}
}
}
//FTP client:
import java.io.*;
import java.net.*;
public class ftpclient
{
public static void main(String args[])throws IOException
{
Socket clients=new Socket(InetAddress.getLocalHost(),9981);
String sent1;
BufferedReader informuser= new BufferedReader(new InputStreamReader(System.in));
DataOutputStream outtoserver=new DataOutputStream(clients.getOutputStream());
System.out.println("\n enter the file name");
sent1=informuser.readLine();
outtoserver.writeBytes(sent1+"\n");
clients.close();
}
}

OUTPUT:
Server:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\MCA-HOD>d:
D:\>set path=C:\Program Files\Java\jdk1.5.0_06\bin;
D:\>set classpath=.;
D:\>javac ftpserver.java
D:\>java ftpserver
ftp server running
file received
filenameswing.java
the content of the file are
Client:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\MCA-HOD>d:
D:\>set path=C:\Program Files\Java\jdk1.5.0_06\bin;
D:\>set classpath=.;
D:\>javac ftpclient.java
D:\>java ftpclient
enter the file name
swing.java

PRIME NUMBER USING RMI


//Cliprime.java:
import java.rmi.*;
import java.net.*;
import java.io.*;
public class Cliprime
{
public static void main (String args []) throws Exception
{
Ifprime ifp;
String answer="";
InputStreamReader ins=new InputStreamReader (System.in);
BufferedReader br=new BufferedReader (ins);
InetAddress ia=InetAddress.getLocalHost ();
String ip=ia.toString ().substring (ia.toString ().indexOf ('/') +1);
String url="rmi://"+ip+"/Srvprime";
System.out.println ("Check whether the number is prime or not");
System.out.println ("Type an int to test for prime :");
ifp= (Ifprime) Naming.lookup (url);
String sn=br.readLine ();
int n =Integer.parseInt (sn);
System.out.println (ifp.Checkprime (n));
}
}
//Ifprime.java:
import java.rmi.*;
public interface Ifprime extends Remote
{
String Checkprime (int n) throws RemoteException;
}
Srvprime.java
import java.rmi.*;
import java.rmi.server.*;
public class Srvprime extends UnicastRemoteObject implements Ifprime
{
int i;
boolean test=true;
public Srvprime () throws RemoteException {;}
public String Checkprime (int gn)
{
for (i=2;i<gn;i++)
if (gn%i==0)
return gn+ "is not a prime number";
return gn + "is a prime number";
}
public static void main (String args [])

{
try {
Srvprime svp=new Srvprime ();
Naming.bind ("Srvprime", svp);
System.out.println ("Server is bound");
} catch (Exception e)
{
System.out.println (Error in connection");
}
System.out.println (Server End");
}
}
OUTPUT:

CHAT ROOM APPLICATION


//Client:
import java.io.*;
import java.net.*;
public class MultiThreadChatClient implements Runnable{
static Socket clientSocket = null;
static PrintStream os = null;
static DataInputStream is = null;
static BufferedReader inputLine = null;
static boolean closed = false;
public static void main(String[] args)
{
int port_number=2222;
String host="localhost";
if (args.length < 2)
{
System.out.println("Usage: java MultiThreadChatClient \n"+
"Now using host="+host+", port_number="+port_number);
} else {
host=args[0];
port_number=Integer.valueOf(args[1]).intValue();
}
try {
clientSocket = new Socket(host, port_number);
inputLine = new BufferedReader(new InputStreamReader(System.in));
os = new PrintStream(clientSocket.getOutputStream());
is = new DataInputStream(clientSocket.getInputStream());
} catch (UnknownHostException e) {
System.err.println("Don't know about host "+host);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to the host "+host);
}
if (clientSocket != null && os != null && is != null) {
try {
// Create a thread to read from the server
new Thread(new MultiThreadChatClient()).start();
while (!closed) {
os.println(inputLine.readLine());
}
os.close();
is.close();
clientSocket.close();
} catch (IOException e) {

System.err.println("IOException: " + e);


}
}
}
public void run() {
String responseLine;
try{
while ((responseLine = is.readLine()) != null) {
System.out.println(responseLine);
if (responseLine.indexOf("*** Bye") != -1) break;
}
closed=true;
} catch (IOException e) {
System.err.println("IOException: " + e);
}
}
}
//Server:
import java.io.*;
import java.net.*;
public class MultiThreadChatServer{
static Socket clientSocket = null;
static ServerSocket serverSocket = null;
// This chat server can accept up to 10 clients' connections
static clientThread t[] = new clientThread[10];
public static void main(String args[]) {
// The default port
int port_number=2222;
if (args.length < 1)
{
System.out.println("Usage: java MultiThreadChatServer \n"+
"Now using port number="+port_number);
} else {
port_number=Integer.valueOf(args[0]).intValue();
}
try {
serverSocket = new ServerSocket(port_number);
}
catch (IOException e)
{System.out.println(e);}

while(true){
try {
clientSocket = serverSocket.accept();
for(int i=0; i<=9; i++){
if(t[i]==null)
{
(t[i] = new clientThread(clientSocket,t)).start();
break;
}
}
}
catch (IOException e) {
System.out.println(e);}
}
}
}
class clientThread extends Thread{
DataInputStream is = null;
PrintStream os = null;
Socket clientSocket = null;
clientThread t[];
public clientThread(Socket clientSocket, clientThread[] t){
this.clientSocket=clientSocket;
this.t=t;
}
public void run()
{
String line;
String name;
try{
is = new DataInputStream(clientSocket.getInputStream());
os = new PrintStream(clientSocket.getOutputStream());
os.println("Enter your name.");
name = is.readLine();
os.println("Hello "+name+" to our chat room.\nTo leave enter /quit in a new line");
for(int i=0; i<=9; i++)
if (t[i]!=null && t[i]!=this)
t[i].os.println("*** A new user "+name+" entered the chat room !!! ***" );
while (true) {
line = is.readLine();
if(line.startsWith("/quit")) break;
for(int i=0; i<=9; i++)
if (t[i]!=null) t[i].os.println("<"+name+"> "+line);
}
for(int i=0; i<=9; i++)
if (t[i]!=null && t[i]!=this)

t[i].os.println("*** The user "+name+" is leaving the chat room !!! ***" );
os.println("*** Bye "+name+" ***");
// Clean up:
// Set to null the current thread variable such that other client could
// be accepted by the server
for(int i=0; i<=9; i++)
if (t[i]==this) t[i]=null;
is.close();
os.close();
clientSocket.close();
}
catch(IOException e){};
}
}
OUTPUT:
Client1:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\MCA-HOD>d:
D:\>set path=C:\Program Files\Java\jdk1.5.0_06\bin;
D:\>set classpath=.;
D:\>javac MultiThreadChatClient.java
Note: MultiThreadChatClient.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
D:\>java MultiThreadChatClient
Usage: java MultiThreadChatClient
Now using host=localhost, port_number=2222
Enter your name.
anitha
Hello anitha to our chat room.
To leave enter /quit in a new line
<Kumar>
hi
< Kumar > hi
*** A new user Arun entered the chat room !!! ***
<hello> hello
Client:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\MCA-HOD>d:

D:\>set path=C:\Program Files\Java\jdk1.5.0_06\bin;


D:\>javac MultiThreadChatClient1.java
Note: MultiThreadChatClient1.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
D:\>java MultiThreadChatClient1
Usage: java MultiThreadChatClient1
Now using host=localhost, port_number=2222
Enter your name.
*** A new user Kumar entered the chat room !!! ***
< Kumar >
< Kumar > hi
Arun
Hello Arun to our chat room.
To leave enter /quit in a new line
hello
< Arun> hello
Server:
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.
C:\Documents and Settings\MCA-HOD>d:
D:\>set path=C:\Program Files\Java\jdk1.5.0_06\bin;
D:\>set classpath=.
D:\>javac MultiThreadChatServer.java
Note: MultiThreadChatServer.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
D:\>java MultiThreadChatServer
Usage: java MultiThreadChatServer
Now using port number=2222

You might also like