Implement FTP request in Java using socket | How to perform FTP Request

To implement the FTP Request using socket in java CS1404 INTERNET PROGRAMMING LABORATORY .FTP –File transfer Protocol. Ftp is normally is used to sharing or transfer the data from the one computer to the computer. In this sharing we can perform the authentication. This authentication it is not must if wee need. And in this coding it has three module first module Fileserver.java program then second module is Fileclient.java program and the finally module is Fileservert.java program

Source code java programming File Transfer Protocol
ServerFile.java
import java.net.*;
import java.io.*;
public class ServerFile
{
ServerSocket serverSocket;
Socket socket;
int port;
ServerFile()
{
this(9999);
}
ServerFile(int port)
{
this.port = port;
}
void waitForRequests() throws IOException
{
serverSocket = new ServerSocket(port);
while (true)
{
System.out.println("Server Is WAITING...");
socket = serverSocket.accept();
System.out.println("Request Received From " +
socket.getInetAddress()+"@"+socket.getPort());
new ServantFile(socket).start();
System.out.println("Service Started Thread ");
}
}
public static void main(String[] args)
{
try
{
new ServerFile().waitForRequests();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
source code java programming ClientFile.java
import java.io.*;
import java.net.*;
public class ClientFile
{
String serverAddress;
String fileName;
int port;
Socket socket;
ClientFile()
{
this("localhost", 9999, "Model.txt");
}
ClientFile(String serverAddress, int port, String
fileName)
{
this.serverAddress = serverAddress;
this.port = port;
this.fileName = fileName;
}
void sendRequestForFile() throws UnknownHostException,
IOException
{
socket = new Socket(serverAddress, port);
System.out.println("Connecting to Server...");
PrintWriter writer = new PrintWriter(new
OutputStreamWriter(socket.getOutputStream()));
writer.println(fileName);
writer.flush();
System.out.println("Request has been Sent... ");
getResponseFromServer();
socket.close();
}
void getResponseFromServer() throws IOException
{
BufferedReader reader = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
String response = reader.readLine();
if(response.trim().toLowerCase().equals("filenotfound"))
{
System.out.println(response);
return; }
else
{
BufferedWriter fileWriter = new
BufferedWriter(new
FileWriter("FileRecd.txt"));
do
{
fileWriter.write(response);
fileWriter.flush();
}while((response=reader.readLine())!=null
}while((response=reader.readLine())!=null);
fileWriter.close();
}
}
public static void main(String[] args)
{
try
{
new ClientFile().sendRequestForFile();
}
catch (UnknownHostException er)
{
er.printStackTrace();
}
catch (IOException er)
{
er.printStackTrace();
}
}
}
source code java programming FileServent.java
import java.net.*;
import java.io.*;
public class ServantFile extends Thread
{
Socket socket;
String fileName;
BufferedReader in;
PrintWriter out;
ServantFile(Socket socket) throws IOException
{
this.socket = socket;
in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new
OutputStreamWriter(socket.getOutputStream()));
}
public void run()
{
try
{
fileName = in.readLine();
File file = new File(fileName);
if (file.exists())
{
BufferedReader fileReader = new
BufferedReader(new FileReader(fileName));
String content = null;
while ((content = fileReader.readLine())
!=
null)
{
out.println(content);
out.flush();
}
System.out.println("File has been Sent...");
}
else
{
System.out.println("Requested File was Not
Found...");
out.println("File Not Found");
out.flush();
}
socket.close();
System.out.println("Connection Closed!");
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
}
}
Output: FTP REQUEST
C:\IPLAB>javac ServerFile.java
C:\IPLAB>javac ClientFile.java
C:\IPLAB>javac FileServent.java
C:\IPLAB>copy con Model.txt
Welcome to FTP
C:\IPLAB>java ServerFile
Server Is WAITING...
C:\IPLAB>java ClientFile
Connecting to Server...
Request has been Sent...
C:\IPLAB>java ServerFile
Server Is WAITING...
Request Received From /127.0.0.1@2160
Service Started Thread
Server Is WAITING...
File has been Sent...
Connection Closed!
C:\IPLAB>type FileRecd.txt
Welcome to FTP


Related post