tags:

views:

64

answers:

1

hi,

When I am trying to do multiple upload i am getting the above error In FTP i have two classes one is for FTPdatatransfer and other is FTPcommand And in my main mage i am doing something like this

FTPCommand ftpcommand = new FTPCommand(ServerAddress, UserName, Password);
ftpcommand.Login();
ftpcommand.sendCommand("CWD ", "/home/praveena/RMS");  
ftpcommand.sendCommand("PASV");              

ftpcommand.sendCommand("STOR ","FTP.pdf");                 
ftpcommand.sendCommand("STOR ","linux.pdf");
ftpcommand.sendCommand("QUIT");

One upload is working fine while other gives above error

FTPCommand.cs

public void sendCommand(String command, params string[] strfilename)
    {


        string ServerAddress = "172.24.18.240";
        string UserName = "praveena";
        string Password = "praveena";


        if (command == "STOR ") //Uploading to Server
        {

            FTPDataTransfer ftpdatatransfer = new FTPDataTransfer(ServerAddress, UserName, Password, 10000, iport);               
            foreach (string dir in strfilename)
            {
                command = "STOR " + strfilename[0];
            }

            Send(command);                
            ftpdatatransfer.Upload(strfilename[0]);                
            this.readResponse();

        }
        else if (command == "MKD ")
        {
            command = "MKD " + strfilename[0];
            Send(command);
            this.readResponse();
        }

        else if (command == "CWD ")
        {
            command = "CWD " + strfilename[0];
            Send(command);
            this.readResponse();
        }

        else if (command == "NLST ") //Listing Files from Server.
        {
            FTPDataTransfer ftpdatatransfer = new FTPDataTransfer(ServerAddress, UserName, Password, 10000, iport);
            foreach (string dir in strfilename)
            {
                command = "NLST " + "*";
            }
            Send(command);
            ftpdatatransfer.GetFilelist();
            this.readResponse();

        }
        else
        {

            Send(command);
            this.readResponse();
            if (command == "PASV")
            {
                iport = GetPort();
            }


        }


    }



    public void Send(string command)
    {
        Byte[] cmdBytes = Encoding.ASCII.GetBytes((command + "\r\n").ToCharArray());
        clientSocket.Send(cmdBytes, cmdBytes.Length, 0);
    }


    public int GetPort()
    {
        int index1 = result.IndexOf('(');
        int index2 = result.IndexOf(')');

        string ipData = this.result.Substring(index1 + 1, index2 - index1 - 1);

        int[] parts = new int[6];

        int len = ipData.Length;
        int partCount = 0;
        string buf = "";

        for (int i = 0; i < len && partCount <= 6; i++)
        {
            char ch = char.Parse(ipData.Substring(i, 1));

            if (char.IsDigit(ch))
                buf += ch;

            else if (ch != ',')
                throw new WinFTPClient.FTPCommand.FtpException("Malformed PASV result: " + result);

            if (ch == ',' || i + 1 == len)
            {
                try
                {
                    parts[partCount++] = int.Parse(buf);
                    buf = "";
                }
                catch (Exception ex)
                {
                    throw new WinFTPClient.FTPCommand.FtpException("Malformed PASV result (not supported?): " + this.result, ex);
                }
            }
        }


        string ipAddress = parts[0] + "." + parts[1] + "." + parts[2] + "." + parts[3];

        int port = (parts[4] << 8) + parts[5];          

        return port;
    }

FTPdatatransfer.cs

public void Upload(string strfilename)
    {


        Thread thread = new Thread(new ThreadStart(() => UploadFile(strfilename)));
        thread.Start();




    }

    public void UploadFile(string strfilename)
    {


        Socket cSocket = createDataSocket();

         //open stream to read file

        FileStream input = new FileStream(strfilename, FileMode.Open);
        while ((bytes = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            cSocket.Send(buffer, bytes, 0);
        }

        input.Close();
        if (cSocket.Connected)
        {
            cSocket.Close();
        }

       // this.readResponse();

        MessageBox.Show("File Uploaded successfully");

    }

 public Socket createDataSocket()
    {


        IPAddress ipAddress = IPAddress.Parse("172.24.18.240");  
        Socket socket = null;
        IPEndPoint ep = null;


        try
        {
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            ep = new IPEndPoint(Dns.GetHostEntry(ipAddress).AddressList[0], port);
            socket.Connect(ep);
        }
        catch (Exception ex)
        {
            // doubtfull....
            if (socket != null && socket.Connected) socket.Close();

            throw new WinFTPClient.FTPCommand.FtpException("Can't connect to remote server", ex);
        }

        return socket;
    }
A: 

Most likely the FTP server is limiting the number of simultaneous uploads. There isn't a way to force this from the client side.

Stephen Cleary
No what i found is after uploading one file connection closes i guess thats why it is not able to upload second file.
Swapnil Gupta
Ah. Yes, some servers use an "abortive close" because it allows them to reclaim resources more quickly.
Stephen Cleary
@stephen : i didn't get you ?
Swapnil Gupta
@Swapnil When exactly does the connection close? And we are talking about the *server* connection (21), right?
bzlm
It closes after first upload that is obvious.when it tries to upload second file it says "could not connect to remote server " because upload method closes the connection after first upload so is there a way where i can get range of ports for data transfer so that i can assign the requests on various ports.
Swapnil Gupta
Please update the question with the correct error message.
Stephen Cleary
Error : An existing connection was forcibly closed by the remote host
Swapnil Gupta