G
gaurav tyagi
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;
}
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;
}