Error : An existing connection was forcibly closed by the remote host in FTP In C#?
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 = "";
开发者_JAVA百科 }
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;
}
Most likely the FTP server is limiting the number of simultaneous uploads. There isn't a way to force this from the client side.
精彩评论