Total members 10259 | Gratitudes |It is currently Mon May 21, 2012 3:59 pm Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Topic
 Topic subject: FTP Server and FTP Client
PostPosted: Thu Oct 16, 2008 7:33 pm 
Offline
Mastermind
User avatar

Joined: Tue Mar 27, 2007 10:55 pm
Posts: 2277
Location: Earth
Has thanked: 39 time
Have thanks: 61 time

* Project Name:   FTP Server and FTP Client
* Programmer:   Abhishek K. Dhote
* Type:   Network
* Technology:  Java
* IDE:   Any
* Description:   This is the implementation of the File Transfer Protocol in Java.The protocol is used for transfer of the file from remote system.

To execute the program go through following steps:-

1.install FtpServer.java on one machine.
2.install FtpClient.java another machine.
3.execute FtpServer.java.
4.execute FtpClient.java.
5.use GUI objects provided on the FtpClient.java to upload or download the particular file.


Code:
//FtpServer.java
import java.io.*;
import java.net.*;

public class FtpServer
{
     public static void main(String [] args)
     {
          int i=1;

System.out.println("******************************************************
**************************");
          System.out.println("******************************   FTP
SERVER
***********************************");

System.out.println("******************************************************
**************************");
          System.out.println("Server Started...");
          System.out.println("Waiting for connections...");
          System.out.println("
");
          try
          {

               ServerSocket s = new ServerSocket(100);
               for(;;)
               {
                    Socket incoming = s.accept();
                    System.out.println("New Client Connected with id "
+ i
+" from "+incoming.getInetAddress().getHostName()+"..." );
                    Thread t = new ThreadedServer(incoming,i);
                    i++;
                    t.start();
               }
          }
          catch(Exception e)
          {
               System.out.println("Error: " + e);
          }
     }
}

class ThreadedServer extends Thread
{
     int n;
     String c,fn,fc;
     String filenm;
     Socket incoming;
     int counter;
     String dirn="c:/FTP SERVER DIRECTORY";
     public ThreadedServer(Socket i,int c)
     {
          incoming=i;
          counter=c;
     }

     public void run()
     {
          try
          {

               BufferedReader in =new BufferedReader(new
InputStreamReader(incoming.getInputStream()));
               PrintWriter out = new
PrintWriter(incoming.getOutputStream(), true);
               OutputStream output=incoming.getOutputStream();
               fn=in.readLine();
               c=fn.substring(0,1);

               if(c.equals("#"))
               {
               n=fn.lastIndexOf("#");
               filenm=fn.substring(1,n);
               FileInputStream fis=null;
               boolean filexists=true;
               System.out.println("Request to download file "+filenm+"
recieved from "+incoming.getInetAddress().getHostName()+"...");
               try
                 {
                  fis=new FileInputStream(filenm);
                 }
               catch(FileNotFoundException exc)
                 {
                  filexists=false;
                  System.out.println("FileNotFoundException:
"+exc.getMessage());
                 }
                if(filexists)
                {
                 sendBytes(fis, output) ;
       fis.close();
                }
               }
              else
                 {
                  try
                  {
                  boolean done=true;
                  System.out.println("Request to upload file " +fn+"
recieved from "+incoming.getInetAddress().getHostName()+"...");

                  File dir=new File(dirn);
                  if(!dir.exists())
                  {
                   dir.mkdir();
                  }
                  else
                  {}
                    File f=new File(dir,fn);
                    FileOutputStream fos=new FileOutputStream(f);
                    DataOutputStream dops=new DataOutputStream(fos);

                   while(done)
                   {
                    fc=in.readLine();
                    if(fc==null)
                    {
                     done=false;
                    }
                    else
                    {
                     dops.writeChars(fc);
                    // System.out.println(fc);

                    }
                 }
                 fos.close();
                 }
                 catch(Exception ecc)
                 {
                  System.out.println(ecc.getMessage());
                 }
                }
               incoming.close();
          }
          catch(Exception e)
          {
               System.out.println("Error: " + e);
          }
     }
     private static void sendBytes(FileInputStream f,OutputStream op)
throws Exception
     {
      byte[] buffer=new byte[1024];
      int bytes=0;

      while((bytes=f.read(buffer))!=-1)
      {
       op.write(buffer,0,bytes);
      }
     }
}

Code:
//FtpClient.java

import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.*;
class FtpClient extends JFrame implements ActionListener
{
String fn,filenm;
String fc;
String dirn="c:/FTP CLIENT DIRECTORY";
JPanel pnl;
JLabel lbltle,lblud;
Font fnt;
JTextField txtfn;
JButton btnu,btnd;
Socket s;
InputStreamReader in;
OutputStream out;
BufferedReader br;
PrintWriter pw;
public FtpClient()
{
  super("FTP CLIENT");

  pnl=new JPanel(null);

  fnt=new Font("Times New Roman",Font.BOLD,25);

  lbltle=new JLabel("FTP CLIENT");
  lbltle.setFont(fnt);
  lbltle.setBounds(225,35,200,30);
  pnl.add(lbltle);

  lblud=new JLabel("ENTER  FILE-NAME :");
  lblud.setBounds(100,100,150,35);
  pnl.add(lblud);

  txtfn=new JTextField();
  txtfn.setBounds(300,100,200,25);
  pnl.add(txtfn);

  btnu=new JButton("UPLOAD");
  btnu.setBounds(150,200,120,35);
  pnl.add(btnu);


  btnd=new JButton("DOWNLOAD");
  btnd.setBounds(320,200,120,35);

  pnl.add(btnd);

  btnu.addActionListener(this);
  btnd.addActionListener(this);
  getContentPane().add(pnl);

  try
  {
  s=new Socket("localhost",100);
  br=new BufferedReader(new InputStreamReader(s.getInputStream()));
  pw=new PrintWriter(s.getOutputStream(),true);
  out=s.getOutputStream();
  }
  catch(Exception e)
  {
   System.out.println("ExCEPTION :"+e.getMessage());
  }
}
public void actionPerformed(ActionEvent e)
{
  if(e.getSource()==btnu)
  {
   try
   {
   filenm=txtfn.getText();
   pw.println(filenm);
   FileInputStream  fis=new FileInputStream(filenm);
   byte[] buffer=new byte[1024];
   int bytes=0;

   while((bytes=fis.read(buffer))!=-1)
   {
    out.write(buffer,0,bytes);
   }
   fis.close();
  }
  catch(Exception exx)
  {
   System.out.println(exx.getMessage());
  }
  }

  if(e.getSource()==btnd)
  {
   try
   {
   File dir=new File(dirn);
   if(!dir.exists())
   {
    dir.mkdir();
   }
   else{}
   boolean done=true;
   filenm=txtfn.getText();
   fn=new String("#"+filenm+"#");
   //System.out.println(filenm);
   pw.println(fn);
   File f=new File(dir,filenm);
   FileOutputStream fos=new FileOutputStream(f);
   DataOutputStream dops=new DataOutputStream(fos);
   while(done)
   {
     fc=br.readLine();
     if(fc==null)
     {
     done=false;
     }
    else
        {
          dops.writeChars(fc);
       //  System.out.println(fc);

        }
    }
   fos.close();
  }

  catch(Exception exx)
  {}

  }
}
public static void main(String args[])
{
  FtpClient ftpc=new FtpClient();
  ftpc.setSize(600,300);
  ftpc.show();
}
}

author email : abhishekdhote@rediffmail.com

_________________
Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  


 Similar topics
 how to connect client on one machine with server
 client server client
 Ajax Source code to Suggest application with JSP Server side
 RMI EXAMPLE FOR CLIENT SERVER ARCHITECTURE CODE ?????
 client server problem
 HELP CONNECT SQL SERVER 2005 WITH PHP 5.2.6
 Accessing files in remote server (unix/windows) using JSP
 Client-Server Forum discussion
 Client-side Caching using AJAX & Javascript
 clients cannot connect to server on another machine

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Topic | Next Topic 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team