Monday, May 17, 2010

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

8 comments:

Anonymous said...

You ought to take part in a contest for one of the best sites online.
I most certainly will recommend this blog!
hardwood floors installation

Also visit my weblog - engineered hardwood flooring

Anonymous said...

Hello to all, it's genuinely a fastidious for me to visit this web site, it contains helpful Information.

my page: hardwood floors

Anonymous said...

Hey There. I discovered your weblog the usage of msn.
That is a very well written article. I'll be sure to bookmark it and come back to learn extra of your helpful info. Thank you for the post. I will definitely comeback.
engineered hardwood floors

My web-site ... hardwood flooring

Anonymous said...

Right now it seems like BlogEngine is the best blogging platform out there right
now. (from what I've read) Is that what you are using on your blog?
hardwood floor refinishing

My blog post ... hardwood floors installation
Also see my web site - installing hardwood floors

Anonymous said...

I'm really loving the theme/design of your site. Do you ever run into any internet browser compatibility problems? A number of my blog audience have complained about my site not working correctly in Explorer but looks great in Chrome. Do you have any solutions to help fix this problem?
hardwood floor refinishing

My website - installing hardwood floors

Anonymous said...

Hello! This is my first comment here so I just wanted to
give a quick shout out and say I truly enjoy reading
your posts. Can you recommend any other blogs/websites/forums
that deal with the same subjects? Thanks a ton!
cleaning hardwood floors

Anonymous said...

I just could not go away your site before suggesting that I extremely enjoyed the
standard info a person provide in your visitors? Is going to be again steadily in order to check out new posts

Have a look at my website: provillus shampoo
Also see my page > http://nopolitic.com/index.php?do=/blog/26808/the-top-three-hair-loss-products/

Anonymous said...

A fascinating discussion is worth comment. I do believe that you should write more about this
subject matter, it might not be a taboo matter but usually people don't speak about these issues. To the next! Cheers!!

Feel free to visit my web-site - zetaclear

Flag counter

free counters