Can anybody help me in a ftp program?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fjlpf

    Can anybody help me in a ftp program?


    Hi

    I am a beginner of programming. These days, I try to
    develop a ftp program. But I find it is harder than I
    have thought.
    I want to post my code, then concentrate on resolving
    the multithread problem and the communication problem
    between the client and the server.

    Can anybody help me or just join the discussion?
  • Peter Koen

    #2
    Re: Can anybody help me in a ftp program?

    "fjlpf" <anonymous@disc ussions.microso ft.com> wrote in news:054a01c3ac 35
    $0425dbe0$a1012 80a@phx.gbl:
    [color=blue]
    >
    > Hi
    >
    > I am a beginner of programming. These days, I try to
    > develop a ftp program. But I find it is harder than I
    > have thought.
    > I want to post my code, then concentrate on resolving
    > the multithread problem and the communication problem
    > between the client and the server.
    >
    > Can anybody help me or just join the discussion?
    >[/color]

    Well, start with some palpable question and we will be able to help you...

    cheers,
    Peter

    --
    ------ooo---OOO---ooo------

    Peter Koen - www.kema.at
    MCAD CAI/RS CASE/RS IAT

    ------ooo---OOO---ooo------

    Comment

    • Konrad Neitzel

      #3
      Re: Can anybody help me in a ftp program?

      Hi "fjlpf",

      (Your parents really gave you that name? :) )

      "fjlpf" <anonymous@disc ussions.microso ft.com> schrieb im Newsbeitrag
      news:054a01c3ac 35$0425dbe0$a10 1280a@phx.gbl.. .
      [color=blue]
      > I am a beginner of programming. These days, I try to
      > develop a ftp program. But I find it is harder than I
      > have thought.
      > I want to post my code, then concentrate on resolving
      > the multithread problem and the communication problem
      > between the client and the server.[/color]

      As Peter told you already: Ask a question and maybe we could answer it.

      And: Please go to google and search a little. Or simply go to


      There you find a lot of stuff about ftp and c#.

      Some small examples are:
      FTP Component in C# for .NET:

      FTP client library for C#:

      Application for uploading modified Files to a FTP Server


      With kind regards,

      Konrad


      Comment

      • Guest's Avatar

        #4
        Can anybody help me in a ftp program?


        Here is my code of ftp server. And I just extract the
        implement of the function of transfering files.
        I know it is very awful, so I am here to asked for your
        help.



        namespace FtpSrvr
        {
        public class Form1 : System.Windows. Forms.Form
        {
        private bool srv = true;
        private IPAddress ipAddr;
        private IPEndPoint ipEP;
        public Socket sock_accpt;//port 20
        public Socket sock_daccpt;//port 21
        private Thread thrd_accpt;//a thread to accept connection
        private Thread thrd_Daccpt;

        public int total =0;

        ..........

        [STAThread]
        static void Main()
        {Application.Ru n(new Form1()); }
        ............... .

        private void btStartSrvr_Cli ck(object sender,
        System.EventArg s e)
        {
        this.btStartSrv r.Enabled = false;
        ipAddr = IPAddress.Parse ("127.0.0.1" );
        IPEndPoint ipEP1 = new IPEndPoint(ipAd dr,20);
        sock_accpt = new Socket
        (AddressFamily. InterNetwork,So cketType.Stream ,ProtocolType.
        Tcp);
        sock_accpt.Bind ((EndPoint)ipEP 1);
        sock_accpt.List en(5);
        //
        IPEndPoint ipEP2 = new IPEndPoint(ipAd dr,21);
        sock_daccpt = new Socket
        (AddressFamily. InterNetwork,So cketType.Stream ,ProtocolType.
        Tcp);
        sock_daccpt.Bin d((EndPoint)ipE P2);
        sock_daccpt.Lis ten(5);

        //create new thread to accept connection
        thrd_Accpt = new Thread(new ThreadStart(thi s.Thrd_Accpt));
        thrd_Accpt.Star t();

        }


        private void Thrd_Accpt()
        {
        while(srv)//
        { Socket newClient = sock_accpt.Acce pt();

        ThreadStartComm and c = new ThreadStartComm and
        (newClient,this .sock_daccpt);

        Thread thrd = new Thread(new ThreadStart(c.T hrd_Command));
        thrd.Start();
        }
        }

        internal class ThreadStartComm and
        {
        private Socket _sock;//store the socket created by accept()
        private Form1 _form;

        public ThreadStartComm and(Socket sock, Form1 form
        { _sock = sock;
        _form = form;
        }

        //this function aims to retrevie command
        public void Thrd_Command()
        {
        _sock.SetSocket Option
        (SocketOptionLe vel.Socket,Sock etOptionName.Se ndTimeout,5000
        );

        byte[] b= System.Text.Enc oding.ASCII.Get Bytes("welcome to
        my ftp server");

        try
        {
        _sock.Send(b,0, b.Length,Socket Flags.None);
        }
        catch(SocketExc eption ex)
        {
        System.Diagnost ics.Debug.Write ("error to send welcome
        message" );
        return;
        }


        _sock.SetSocket Option
        (SocketOptionLe vel.Socket,Sock etOptionName.Re ceiveTimeout,2
        0000);

        byte[] buffer = new byte[1024];
        int rcv ;
        while(true)
        {
        try
        {
        rcv = _sock.Receive(b uffer,0,1024,So cketFlags.None) ;
        }
        catch(SocketExc eption ex)
        {
        /*if the client doesn't send command within 20 seconds,the
        socket will close.*/

        _sock.Close();
        return
        }
        }

        string s = System.Text.Enc oding.ASCII.Get String
        (buffer,0,rcv);
        s = s.ToLower();
        int i = s.IndexOf(" ");
        string comCode = s.Substring(0,i );
        string para = s.Substring(i+1 ,rcv-1-i);
        AnalyseCommand( comCode,para);
        byte[] buffer = ASCII.GetBytes( "120 new port allocated");
        _sock.Send(buff er,0,buffer.Len gth,SocketFlags .None);
        }
        }

        public void AnalyseCommand( string comCode, string para)
        {
        switch(comCode)
        {
        case "retr":
        {
        lock(this._form .sock_daccpt)//
        {if(this._form. total<4)
        this._form.tota l++;//restrict total connection of Port 21
        else
        {byte[] buf = System.Text.Enc oding.ASCII.Get Bytes("sorry,
        overload");
        this._sock.Send (buf,0,buf.Leng th,SocketFlags. None);
        return;
        }
        }
        Thread thrd = new Thread(new ThreadStart
        (this.ThrdSndFi le));

        thrd.Start();// thread to transfer file to client
        }
        break;
        }
        case "":...

        case "":...
        }


        }



        public void ThrdSndFile(Soc ket accpt, string para)
        {
        Socket dataclient = this._form.sock _daccpt.Accept( );
        FileStream fs;
        try
        {
        fs = new FileStream
        (@para,FileMode .Open,FileAcces s.Read,FileShar e.Read);
        }
        catch(IOExcepti on ex)
        {dataclient.Clo se();
        return;
        }

        dataclient.SetS ocketOption
        (SocketOptionLe vel.Socket,Sock etOptionName.Se ndTimeout,5000
        0);
        int rd = 1;

        while(rd!=0)
        {
        //Question:if the client socket collapse, what happen to
        //socket of server, does it still push data to TCP's buffer


        byte[] buffer = new byte[1024];
        rd = fs.Read(buffer, 0,1024);

        try
        {_sock.Send(buf fer,0,rd,Socket Flags.None);
        }
        catch(SocketExc eption ex)
        {
        lock(this._form ._sock_daccpt)
        { this._form.tota l--;
        }
        _sock.Close();
        return;
        }
        }
        }

        _sock.Close();
        }
        }


        }


        Comment

        • fjlpf

          #5
          Re: Can anybody help me in a ftp program?


          :)
          konrad,
          my parents' taste isn't awful like me.

          with kind regards

          Comment

          • Konrad Neitzel

            #6
            Re: Can anybody help me in a ftp program?

            Hi!

            "fjlpf" <anonymous@disc ussions.microso ft.com> schrieb im Newsbeitrag
            news:057201c3ac 4a$a3cc2bd0$a30 1280a@phx.gbl.. .
            [color=blue]
            > :)
            > konrad,
            > my parents' taste isn't awful like me.[/color]

            Then everything is ok for me. I hope, my links helped a little.

            With kind regards,

            Konrad


            Comment

            Working...