Professional Documents
Culture Documents
//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
{
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:
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: