FTP :No connection could be made because the target machine actively refused it

Discussion in 'ASP .Net' started by gaurav tyagi, Mar 18, 2006.

  1. gaurav tyagi

    gaurav tyagi Guest

    i am trying to downlosd frles from a local ftp server in my network .
    here i am able to connect to server
    but to get file list from server i send "PASV" command it retuns some
    value which is not 227.
    agian i pass "PASV" command then it returns correct value which is 227.
    now i have ip of server and port number so i try to create socket for
    connection but this time it send me a exception "No connection could
    be made because the target machine actively refused it"
    problem 1. "PASV " commmand work in second loop . wrong replay in first
    call
    problem 2. not creating socket.

    all code is in C#

    private Socket createDataSocket()
    {
    this.sendCommand("PASV");

    if ( this.resultCode != 227 ) throw new
    FtpException(this.result.Substring(4));

    int index1 = this.result.IndexOf('(');
    int index2 = this.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 FtpException("Malformed PASV result: " + result);

    if ( ch == ',' || i+1 == len )
    {
    try
    {
    parts[partCount++] = int.Parse(buf);
    buf = "";
    }
    catch (Exception ex)
    {
    throw new 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];

    Socket socket = null;
    IPEndPoint ep = null;

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

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

    return socket;
    }
     
    gaurav tyagi, Mar 18, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roxanne
    Replies:
    0
    Views:
    1,245
    Roxanne
    Jul 4, 2003
  2. Replies:
    9
    Views:
    62,096
    ytterbius
    May 10, 2007
  3. Homer
    Replies:
    2
    Views:
    16,589
    Homer
    Aug 13, 2007
  4. Replies:
    8
    Views:
    239
    Swanagon, John M \(John\)
    Jul 25, 2006
  5. Merhawi Fissehaye
    Replies:
    9
    Views:
    2,607
    Simon Krahnke
    May 3, 2012
Loading...

Share This Page