FtpException when getting files - c#

I'm successfully establishing an ftp connection and want to get files from FTP server.
This is my code:
ftp.GetFile(file.Name, path + file.Name, false);
where ftp is FtpConnection.
And for all the other files it went well. Now, for the first time I got it and the exception msg isn't clear to me at all. Here it is:
200 Type set to I
227 Entering Passive Mode (93,186,176,75,78,64).
213 21846
What does this mean?

Since FTP is a bidirectional protocol and you need 2 connections (server <-> client)
In active mode the client connect to the server and the server connects back to the client. This is usually a problem with firewalls. In passive mode the client creates both connections, one on the ftp port, the second one on another port.
This can be for high server load or bad firewall configuration.

Related

Fluent FTP Won't connect to FTP Server [duplicate]

This question already has an answer here:
"The handshake failed due to an unexpected packet format" FluentFTP error when connecting to SFTP server
(1 answer)
Closed 1 year ago.
I'm trying to connect to an FTP Server using Fluent FTP.
I have no issue using Filezilla when I use Port 22 (SFTP).
However I can't connect to it using Fluent FTP.
var client = new FtpClient(Constants.FtpHost, Constants.FtpUsername, Constants.FtpPassword)
{
// DataConnectionType = FtpDataConnectionType.PASV,
// EncryptionMode = FtpEncryptionMode.Implicit,
// SslProtocols = SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Tls13,
Port = 22
};
Console.WriteLine("Connecting...");
client.Connect();
Console.WriteLine("Connected");
I Get the following error: Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
When I Uncomment DataConnectionType = FtpDataConnectionType.PASV Nothing changes.
When I Uncomment EncryptionMode = FtpEncryptionMode.Implicit I Get the following Error:
System.IO.IOException: Cannot determine the frame size or a corrupted frame was received.
Once again I have 0 issues with FileZilla or WinSCP.
Any help would be greatly appreciated.
From the official GitHub page:
SFTP is not supported as it is FTP over SSH, a completely different protocol (use SSH.NET for that)
You're trying to use an FTP library to connect to an SFTP server. They're two completely different protocols that have nothing in common, despite fulfulling the same goal. Use a proper SFTP library for that server.
Both FileZilla and WinSCP support both FTP and SFTP, that's why you have no problems with them.

WinSCP .NET library FTP transfer fails with "IP addresses of control and data connection do not match"

I have implemented FTP code with use of WinSCP .NET assembly and hosted it on an Azure AppService.
It works locally and on Azure.
But in very few random times, when hosted on Azure, it throws the following error:
Error transferring file 'D:\local\Temp\test_settings.txt'. Server sent passive reply with unroutable address 10.YYY.YYY.YYY, using host address instead. Copying files to remote side failed. Rejected data connection for transfer of "/test_settings.txt", IP addresses of control and data connection do not match
Since the IP starts with 10. does that mean that it's local in the FTP server's network?
Can I do something to improve the implementation?
Do you think that the solution will have a problem when used concurrently by multiple requests?
My code is a copy of the Simple C# example with the following settings:
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
UserName = username,
Password = password,
GiveUpSecurityAndAcceptAnyTlsHostCertificate = true,
FtpSecure = FtpSecure.Explicit,
HostName = 'ftp.domain.com'
};
IP addresses of control and data connection do not match
That's a message from FileZilla FTP server. It's a security measure. It might indicate that external IP address of your app service instance changed mid transfer. Or of course, it might indicate that you connection was hijacked (that's what the server tries to detect).
It has nothing to do with WinSCP.
I do not know if the IP address of the Azure app service can be fixed somehow. If not, all you can do is to reconnect and retry the transfer. I believe you would have the same problem with any FTP client. Maybe with IPv6 connection, the problem would not happen. But I'm not sure, it's just a wild guess. Though you cannot force IPv6 with WinSCP (only by disabling IPv4 altogether, but I do not know if that's even possible with the app service).

This is usually a temporary error during hostname resolution and mean that the local server didn't receive a response from an authoritative server

Could you please help me in this ?
Using webserver :
I have a Dns server with local database, when any client machine sends DNS name query to DNS Server, it forward that request to authoritative server and after that the client gets the response.
For this process I have used the same code so that my machine can directly communicate with DNS server. The process is working fine for most of the times but from last 2/3 days when my client m/c trying to connect to DNS server I got this exception
::"exception : this is usually a temporary error during hostname resolution and mean that the local server didn't receive a response from an authoritative server""
I got this exception 10/12 times a day
Details: there are total 30/40 times I try to connect to DNS server
__- 10/12 times I get this exception
__- for remaining it works.

.NET C# - Cannot access a SQL Server database behind remote machine

I have the credentials to access a server in a local network - a network that is behind a proxy. And that server is running a SQL Server 7.0 database. The database is configured to use Windows authentication for log in.
I use the Remote Desktop Connection to access the server, put in the credentials, and when inside I open the Query Analyser, select log in with Windows authentication and then I query the DB.
But, now I need a .NET C# program to access the database from my machine, remotely. I have checked for a TCP/IP connection on port 1433 and it's open. However, with the following code, I cannot open a connection to the remote database.
SqlConnectionStringBuilder connStringBuilder = new SqlConnectionStringBuilder();
connStringBuilder["Trusted_Connection"] = false;
connStringBuilder["user id"] = "<domain>\\<user>";
connStringBuilder["password"] = "<pass>";
connStringBuilder["Database"] = "<db>";
connStringBuilder["Server"] = "\\\\<servername>\\MSSQLServer,1433";
connStringBuilder["Connection Timeout"] = "30";
SqlConnection myConn = new SqlConnection(connStringBuilder.ConnectionString);
try
{
myConn.Open();
// success
Console.WriteLine("Connection Opened");
}
catch (Exception e)
{
// failed
Console.WriteLine("Connection Failed");
Console.WriteLine(e.ToString());
}
The servername field is the same text I put in the Remote Desktop Connection tool, as it is with the user id and password fields after a connection is established (normal log on with Windows Server 2000).
Also, with the SSMS (SQL Server Management Studio) I cannot connect to the instance, however with Windows Explorer I can browse the server's drives with \\servername\e$ (for example).
Thank you in advance!
EDIT 1
I believe the problem is go through the remote machine log in, but I have no idea how to do that. I tested on a local db on my machine and the program works like a charm.
The error message from the exception e is the following, weather with true or false on the Trusted Connection:
System.Data.SqlClient.SqlException: A network-related or instance-specific error
occurred while establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that SQL Server is
configured to allow remote connections. (provider: SQL Network Interface, error: 25 -
Connection string is not valid)
at .... etc
EDIT 2
The steps I take to log on to the machine using some credentials are the following:
I first put in the server name as in "MACHINE33" (without quotes)
Remote Desktop Connection
And then I put the credentials \ as in "me\johndoe"
Credentials to access the remote machine
In the end the database is accessed using the windows authentication used to access the machine.
If you are using integrated authentication then the value for trusted connection should be either 'true' or 'sspi', not false. If 'true', any user id and password you specify on the connection string will be ignored so might as well not set them if you are intending to use the credentials for the currently logged in user (or the AppPool user if a web app, or the service user if a windows service etc.).
Are you using a named SQL Server instance? If so, a dynamic port is used, not 1433. You would need to either configure SQL Server to use a specific port for the named instance, or not specify a port.
"Login failed for user" when specifying default port
I have asked the systems administrator, this morning, and he told me that the database cannot be accessed from the outside, even though there is a TCP/IP configuration on port 1433 (checked under Server Network Utility for SQL Server 7.0). The reason is that the whole network is behind a proxy that checks all incoming requests and prevents the ones not coming from the tools specified (i.e. Remote Desktop Connection).
Since what I want is to migrate some fields from the structure of the old database to a new one, used by a new system, the solution is:
Access the remote machine, create a backup of the database, access the files through mapping the network drive, copy the backup files to your machine, create a local database and restore the backup to your new local database.
It's not the most elegant way, but it's the only way. And as an advice, first ask the systems administrator if you're behind a network full of proxies because it can save you a lot of headaches.
Thank you and sorry for the whole trouble.

Apache Thrift remote service timeout

I set up Thrift to build two entities and a corresponding service. I genereated the code for C#, created a small console application for the server and another one for a test client. I ran both on my local machine and it worked like a charm. But when deploying the server on my Azure VM running Windows Server 2012, with an endpoint created on port 9090 for TCP, my client can't connect. There's a timeout.
Similar questions on SO: This one is about a php library specific problem and this one is about two linux machines. The latter one is close to my question, but I can imagine my problem is Windows or Azure VM specific.
I'm using Thrift 0.9.0.0 from NuGet. I tried SimpleServer and ThreadPoolServer. This is what I'm doing on the server:
Thrift.Server.TServer server = new Thrift.Server.TThreadPoolServer(new TodoThriftService.Processor(new TodoThriftServiceHandler()), new TServerSocket(9090));
This is what I'm doing on the client:
TodoThriftService.Client client = new TodoThriftService.Client(new TBinaryProtocol(new TSocket("my.host.com", 9090)));
I also tried the server IP address instead of its host name, but it didn't work either.
netstat -an output is:
TCP 0.0.0.0:9090 0.0.0.0:0 LISTENING
Output from telnet from client (telnet my.host.com 9090):
Verbindungsaufbau zu my.host.com...Es konnte keine Verbindung mit dem Host hergestellt werden, auf Port 9090: Verbindungsfehler
Which is German for "connection error".
When I visit http://canyouseeme.org/ on the server and check port 9090 the output is:
Error: I could not see your service on 123.34.567.890 on port (9090). Reason: Connection timed out
When I sniff packets with Wireshark on the server the output is:
1 0.000000000 12.3.456.78 12.34.567.89 TCP 66 53566 > websm [SYN] Seq=0 Win=8192 Len=0 MSS=1452 WS=256 SACK_PERM=1
And two retransmissions.
Note: The destination IP shown in Wireshark differs from the IP that canyouseeme.org determined.
Also note: Both connection attempts from my test client as well as from canyouseeme.org are shown in Wireshark.
Is my server console application doing anything wrong? It works fine on my local machine.
Because netstat reports that your server is listening on 9090, and because tcpdump reports that SYN packets are arriving to 9090, something else must be blocking the client.
My suspicion in this case would be some sort of local firewall (because the packets did arrive, but the OS should have answered with a SYN packet and you didn't see that). Check Windows Firewall to make sure port 9090 is open.

Categories