Results 1 to 2 of 2
  1. #1
    toxic is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default FTP client server

    ok guys straight to the problem. This is based on file transfer protocol.
    Both client and servers are getting executed without any errors..the only problem is when i enter the filename and click on Upload or Download..nothing happens i.e. no file transfer takes place.

    This is the screenshot when i click on download.


    and ere is the server code..

    Java Code:
    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("<BR>");
              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:<BR>"+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);
          }
         }
    }
    and ere is the client code..

    Java Code:
    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();
     }
    }
    Any help would be gladly appreciated.. :-)

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. server/client
    By deepkar in forum Networking
    Replies: 1
    Last Post: 02-16-2010, 05:31 AM
  2. Replies: 3
    Last Post: 07-29-2009, 04:52 AM
  3. Client server
    By yash in forum Networking
    Replies: 9
    Last Post: 01-04-2009, 04:33 PM
  4. Replies: 1
    Last Post: 10-05-2008, 03:12 PM
  5. Identify Client in Socket Client Server Application
    By masadjie in forum Networking
    Replies: 1
    Last Post: 12-20-2007, 09:18 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •