无法登录FTP,System.Net.Sockets.SocketException: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败

kate12345 2010-09-14 05:59:19
namespace EDI
{
public class FTPFactory
{

// Methods
public FTPFactory()
{
this.m_verboseDebugging = false;
this.m_server = "localhost";
this.m_remotePath = ".";
this.m_username = "anonymous";
this.m_password = "anonymous@anonymous.net";
this.m_message = null;
this.m_result = null;
this.m_port = 0x15;
this.m_bytes = 0;
this.m_resultCode = 0;
this.m_loggedin = false;
this.m_binMode = false;
this.m_buffer = new byte[BUFFER_SIZE];
this.m_clientSocket = null;
this.m_transferSocket = null;
this.m_timeoutSeconds = 10;
}

public FTPFactory(string server, string username, string password)
{
this.m_verboseDebugging = false;
this.m_server = "localhost";
this.m_remotePath = ".";
this.m_username = "anonymous";
this.m_password = "anonymous@anonymous.net";
this.m_message = null;
this.m_result = null;
this.m_port = 0x15;
this.m_bytes = 0;
this.m_resultCode = 0;
this.m_loggedin = false;
this.m_binMode = false;
this.m_buffer = new byte[BUFFER_SIZE];
this.m_clientSocket = null;
this.m_transferSocket = null;
this.m_timeoutSeconds = 10;
this.m_server = server;
this.m_username = username;
this.m_password = password;
}

public FTPFactory(string server, string username, string password, int timeoutSeconds, int port)
{
this.m_verboseDebugging = false;
this.m_server = "localhost";
this.m_remotePath = ".";
this.m_username = "anonymous";
this.m_password = "anonymous@anonymous.net";
this.m_message = null;
this.m_result = null;
this.m_port = 0x15;
this.m_bytes = 0;
this.m_resultCode = 0;
this.m_loggedin = false;
this.m_binMode = false;
this.m_buffer = new byte[BUFFER_SIZE];
this.m_clientSocket = null;
this.m_transferSocket = null;
this.m_timeoutSeconds = 10;
this.m_server = server;
this.m_username = username;
this.m_password = password;
this.m_timeoutSeconds = timeoutSeconds;
this.m_port = port;
}

public void Login()
{
if (this.m_loggedin)
{
this.Close();
}
Debug.WriteLine("Opening connection to " + this.m_server, "FtpClient");
IPAddress address = null;
IPEndPoint remoteEP = null;
try
{
this.m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
address = Dns.Resolve(this.m_server).AddressList[0];
remoteEP = new IPEndPoint(address, this.m_port);
this.m_clientSocket.Connect(remoteEP);
}
catch (Exception exception)
{
if ((this.m_clientSocket != null) && this.m_clientSocket.Connected)
{
this.m_clientSocket.Close();
}
throw new FtpException("Couldn't connect to remote server", exception);
}
try
{
this.readResponse();
}
catch (Exception ex)
{
throw ex;
}
if (this.m_resultCode != 220)
{
this.Close();
throw new FtpException(this.m_result.Substring(4));
}
this.sendCommand("USER " + this.m_username);
if ((this.m_resultCode != 0x14b) && (this.m_resultCode != 230))
{
this.cleanup();
throw new FtpException(this.m_result.Substring(4));
}
if (this.m_resultCode != 230)
{
this.sendCommand("PASS " + this.m_password);
if ((this.m_resultCode != 230) && (this.m_resultCode != 0xca))
{
this.cleanup();
throw new FtpException(this.m_result.Substring(4));
}
}
this.m_loggedin = true;
Debug.WriteLine("Connected to " + this.m_server, "FtpClient");
this.ChangeDir(this.m_remotePath);
}

private string readLine()
{
while (true)
{
try
{
this.m_bytes = m_clientSocket.Receive(this.m_buffer, this.m_buffer.Length, 0);
}
catch(Exception ex)
{
throw ex;
}
this.m_message += ASCII.GetString(this.m_buffer, 0, this.m_bytes);

if (this.m_bytes < this.m_buffer.Length)
{
break;
}
}

string[] msg = this.m_message.Split('\n');

if (this.m_message.Length > 2)
this.m_message = msg[msg.Length - 2];

else
this.m_message = msg[0];


if (this.m_message.Length > 4 && !this.m_message.Substring(3, 1).Equals(" ")) return this.readLine();

if (this.m_verboseDebugging)
{
for (int i = 0; i < msg.Length - 1; i++)
{
Debug.Write(msg[i], "FtpClient");
}
}

return m_message;
}

private void readResponse()
{
this.m_message = "";
this.m_result = this.readLine();
if (this.m_result.Length > 3)
{
this.m_resultCode = int.Parse(this.m_result.Substring(0, 3));
}
else
{
this.m_result = null;
}
}


private void sendCommand(string command)
{
if (this.m_verboseDebugging)
{
Debug.WriteLine(command, "FtpClient");
}
byte[] bytes = Encoding.ASCII.GetBytes((command + "\r\n").ToCharArray());
this.m_clientSocket.Send(bytes, bytes.Length, SocketFlags.None);
this.readResponse();
}




public void Upload(string fileName)
{
this.Upload(fileName, false);
}

public void Upload(string fileName, bool resume)
{
if (!this.m_loggedin)
{
this.Login();
}
Socket socket = null;
long offset = 0L;
if (resume)
{
try
{
this.BinaryMode = true;
offset = this.GetFileSize(Path.GetFileName(fileName));
}
catch (Exception)
{
offset = 0L;
}
}
FileStream stream = new FileStream(fileName, FileMode.Open);
if (resume && (stream.Length < offset))
{
Debug.WriteLine("Overwriting " + fileName, "FtpClient");
offset = 0L;
}
else if (resume && (stream.Length == offset))
{
stream.Close();
Debug.WriteLine("Skipping completed " + fileName + " - turn resume off to not detect.", "FtpClient");
return;
}
socket = this.createDataSocket();
if (offset > 0L)
{
this.sendCommand("REST " + offset);
if (this.m_resultCode != 350)
{
Debug.WriteLine("Resuming not supported", "FtpClient");
offset = 0L;
}
}
this.sendCommand("STOR " + Path.GetFileName(fileName));
if ((this.m_resultCode != 0x7d) && (this.m_resultCode != 150))
{
throw new FtpException(this.m_result.Substring(4));
}
if (offset != 0L)
{
Debug.WriteLine("Resuming at offset " + offset, "FtpClient");
stream.Seek(offset, SeekOrigin.Begin);
}
Debug.WriteLine("Uploading file " + fileName + " to " + this.m_remotePath, "FtpClient");
while ((this.m_bytes = stream.Read(this.m_buffer, 0, this.m_buffer.Length)) > 0)
{
socket.Send(this.m_buffer, this.m_bytes, SocketFlags.None);
}
stream.Close();
if (socket.Connected)
{
socket.Close();
}
this.readResponse();
if ((this.m_resultCode != 0xe2) && (this.m_resultCode != 250))
{
throw new FtpException(this.m_result.Substring(4));
}
}


}

用以上的类文件登录FTP,无法登录,捕捉到系统提示如下。另外还有一个奇怪的现象,我单步执行可以正常登录FTP,但正常运行无法登录。跪求大侠帮忙解决!



220-Microsoft FTP Service
220 Welecome to Toshiba Digital Media Co.,LTD FTP Server
421 Timeout (120 seconds): closing control connection.
421 Terminating connection.

System.Net.Sockets.SocketException: 由于连接方在一段时间后没有正确答复或连接的主
机没有反应,连接尝试失败。
...全文
1482 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
derrial 2010-09-15
  • 打赏
  • 举报
回复
楼主办法很多哦,支持一下
kate12345 2010-09-15
  • 打赏
  • 举报
回复
用下面的方法,问题已经解决了。

try
{
//變數設定
System.Net.FtpWebRequest theFtpWebRequest;
FtpWebResponse ftpRes;
theFtpWebRequest = (FtpWebRequest)WebRequest.Create(strDestinationFile); //目的檔 及 檔名
theFtpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
theFtpWebRequest.Credentials = new NetworkCredential(strUserId, strPassWord);
theFtpWebRequest.KeepAlive = false;
theFtpWebRequest.UseBinary = true;

StreamReader sourceStream = new StreamReader(strSourceFile);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
sourceStream.Close();
theFtpWebRequest.ContentLength = fileContents.Length;

Stream requestStream = theFtpWebRequest.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();

ftpRes = (FtpWebResponse)theFtpWebRequest.GetResponse();

Console.WriteLine("Upload File Complete, status {0}", ftpRes.StatusDescription);

ftpRes.Close();


ftpRes = null;
theFtpWebRequest = null;

return true;

}
catch (Exception ex)
{
return false;
}

4,356

社区成员

发帖
与我相关
我的任务
社区描述
通信技术相关讨论
社区管理员
  • 网络通信
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧