Java Code For FTP Using TCP
Java Code For FTP Using TCP
//Server
import java.io.*;
import java.net.*;
import java.util.Arrays;
class FileSend
{
public static void main(String args[])
{
//Keep trying until Client connects.
try
{
ServerSocket serversocket=new ServerSocket(50000);
//Created ServerSocket on port 50000. Acknowledge client.
System.out.println("Running...");
//Accept incoming Client request.
Socket socket=serversocket.accept();
System.out.println("Client connected.");
//Read and Write on Socket.
PrintWriter pw=new PrintWriter(socket.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//Send File List
String dirname="/media/VA/ACN/TextFiles";
File f1=new File(dirname);
File fl[]=f1.listFiles();
//Sort Alphabetically
Arrays.sort(fl);
//Counter for required files
int c=0;
for(int i=0;i<fl.length;i++)
{
if(fl[i].canRead() && (fl[i].toString()).endsWith(".txt"))
c++;
}
pw.println(" "+c+" .txt files found, listed A-Z.");
for(int i=0;i<fl.length;i++)
if((fl[i].toString()).endsWith(".txt"))
pw.println(" "+fl[i].getName()+" "+fl[i].length()+" Bytes");
//Output String Stream delimiter
pw.println("~");
pw.flush();
//Convert ASCII to Decimal value.
String tem=br.readLine();
int temp=Integer.parseInt(tem);
temp-=49;
System.out.println("Index: "+temp);
//Check if file exists
boolean flis=false;
int index=0;
if(temp>=0 && temp<=fl.length)
{
flis=true;
index=temp;
}
else
flis=false;
if(flis)
{
try
{
//File Send Process, Independent
File ff=new File(fl[index].getAbsolutePath());
FileReader fr=new FileReader(ff);
BufferedReader brf=new BufferedReader(fr);
String s;
while((s=brf.readLine())!=null)
pw.println(s);
//Force write buffer to Client
pw.flush();
if(brf.readLine()==null)
System.out.println("File Read Successful. Closing Socket.");
}
catch(IOException ioe)
{
System.out.println("\nError in FTP. Please try again.");
}
}
//Close streams and socket.
br.close();
socket.close();
}
catch(Exception E)
{
System.out.println("\nConnection error, please try again.");
}
}
}
//client
import java.io.*;
import java.net.*;
class FileReceive
{
public static void main(String args[])
{
try
{
Socket socket=new Socket("127.0.0.1", 50000);
//Read and Write on Socket.
PrintWriter out=new PrintWriter(socket.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//Read from Console
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
String s;
while((br.read())!='~')
System.out.println(br.readLine());
System.out.println("Enter file index no: ");
out.println(bu.read());
//Force write buffer
out.flush();
//File Receive Process, Independent
try
{
BufferedWriter pw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("Received.txt")));
while((s=br.readLine())!=null)
pw.write(s);
//Force write buffer to Server
pw.close();
if(br.readLine()==null)
System.out.println("File Write Successful. Closing Socket.");
}
catch(IOException ioe)
{
}
}
catch(Exception E)
{
System.out.println("Server is down, please try again later.");
}
}
}
//Server
import java.io.*;
import java.net.*;
import java.util.Arrays;
class FileSend
{
public static void main(String args[])
{
//Keep trying until Client connects.
try
{
ServerSocket serversocket=new ServerSocket(50000);
//Created ServerSocket on port 50000. Acknowledge client.
System.out.println("Running...");
//Accept incoming Client request.
Socket socket=serversocket.accept();
System.out.println("Client connected.");
//Read and Write on Socket.
PrintWriter pw=new PrintWriter(socket.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//Send File List
String dirname="/media/VA/ACN/TextFiles";
File f1=new File(dirname);
File fl[]=f1.listFiles();
//Sort Alphabetically
Arrays.sort(fl);
//Counter for required files
int c=0;
for(int i=0;i<fl.length;i++)
{
if(fl[i].canRead() && (fl[i].toString()).endsWith(".txt"))
c++;
}
pw.println(" "+c+" .txt files found, listed A-Z.");
for(int i=0;i<fl.length;i++)
if((fl[i].toString()).endsWith(".txt"))
pw.println(" "+fl[i].getName()+" "+fl[i].length()+" Bytes");
//Output String Stream delimiter
pw.println("~");
pw.flush();
//Convert ASCII to Decimal value.
String tem=br.readLine();
int temp=Integer.parseInt(tem);
temp-=49;
System.out.println("Index: "+temp);
//Check if file exists
boolean flis=false;
int index=0;
if(temp>=0 && temp<=fl.length)
{
flis=true;
index=temp;
}
else
flis=false;
if(flis)
{
try
{
//File Send Process, Independent
File ff=new File(fl[index].getAbsolutePath());
FileReader fr=new FileReader(ff);
BufferedReader brf=new BufferedReader(fr);
String s;
while((s=brf.readLine())!=null)
pw.println(s);
//Force write buffer to Client
pw.flush();
if(brf.readLine()==null)
System.out.println("File Read Successful. Closing Socket.");
}
catch(IOException ioe)
{
System.out.println("\nError in FTP. Please try again.");
}
}
//Close streams and socket.
br.close();
socket.close();
}
catch(Exception E)
{
System.out.println("\nConnection error, please try again.");
}
}
}
//client
import java.io.*;
import java.net.*;
class FileReceive
{
public static void main(String args[])
{
try
{
Socket socket=new Socket("127.0.0.1", 50000);
//Read and Write on Socket.
PrintWriter out=new PrintWriter(socket.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
//Read from Console
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
String s;
while((br.read())!='~')
System.out.println(br.readLine());
System.out.println("Enter file index no: ");
out.println(bu.read());
//Force write buffer
out.flush();
//File Receive Process, Independent
try
{
BufferedWriter pw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("Received.txt")));
while((s=br.readLine())!=null)
pw.write(s);
//Force write buffer to Server
pw.close();
if(br.readLine()==null)
System.out.println("File Write Successful. Closing Socket.");
}
catch(IOException ioe)
{
}
}
catch(Exception E)
{
System.out.println("Server is down, please try again later.");
}
}
}
0 comments :