高分求助:我写了一段FTP上传的程序,但每当传文件的时候就会出现异常.请大家帮忙看看.
public class FTP
{
private Socket cs;
private string remoteHost,remoteUser,remotePwd,remoteFilePath;
private int remotePort,bytes;
private bool logined;
private string replyCode,replyMess,fileName;
private byte[] buffer=new byte[1024];
public FTP()
{
}
/// <summary>
/// 属性:远程主机名称
/// </summary>
public string RemoteHost
{
get
{
return remoteHost;
}
set
{
remoteHost=value;
}
}
/// <summary>
/// 属性:用户名
/// </summary>
public string RemoteUser
{
get
{
return remoteUser;
}
set
{
remoteUser=value;
}
}
/// <summary>
/// 属性:登陆口令
/// </summary>
public string RemotePassword
{
get
{
return remotePwd;
}
set
{
remotePwd=value;
}
}
/// <summary>
/// 属性:Ftp端口号
/// </summary>
public int RemotePort
{
get
{
return remotePort;
}
set
{
remotePort=value;
}
}
/// <summary>
/// 属性:FTP文件目录
/// </summary>
public string RemoteFilePath
{
get
{
return remoteFilePath;
}
set
{
remoteFilePath=value;
}
}
/// <summary>
/// 属性:设置本地文件名与路径
/// </summary>
public string LocalFile
{
get
{
return fileName;
}
set
{
fileName=value;
}
}
/// <summary>
/// 登陆函数
/// </summary>
private void Login()
{
IPEndPoint ep=new IPEndPoint(Dns.Resolve(remoteHost).AddressList[0],remotePort);
cs=new Socket(ep.AddressFamily,SocketType.Stream,ProtocolType.Tcp);
try
{
cs.Connect(ep);
UserCheck();
PassCheck();
logined=true;
}
catch
{
logined=false;
CleanUp();
throw new IOException("Couldn't connect to the remote server.");
}
}
/// <summary>
/// Tcp发送命令
/// </summary>
/// <param name="strCommand"></param>
private void SendFtpCommand(string strCommand)
{
byte[] sendByte=System.Text.Encoding.ASCII.GetBytes((strCommand+"\r\n").ToCharArray());
cs.Send(sendByte,sendByte.Length,0);
}
/// <summary>
/// 读取响应结果
/// </summary>
private void ReadReply()
{
replyMess="";
replyCode=ReadReplyLine();
}
/// <summary>
/// 获取响应
/// </summary>
/// <returns></returns>
private string ReadReplyLine()
{
while(true)
{
int byteCount=cs.Receive(buffer,buffer.Length,0);
replyMess+=Encoding.ASCII.GetString(buffer,0,byteCount);
if(byteCount<buffer.Length)
break;
}
string[] mess=replyMess.Split('\n');
if(mess.Length>2)
replyMess=mess[mess.Length-2];
else
replyMess=mess[0];
return replyMess.Substring(0,3);
}
/// <summary>
/// 检查用户名
/// </summary>
private void UserCheck()
{
SendFtpCommand("USER "+remoteUser);
ReadReply();
if(replyCode!="331"&&replyCode!="230")
{
CleanUp();
throw new Exception("Error user!");
}
}
/// <summary>
/// 口令检查
/// </summary>
private void PassCheck()
{
SendFtpCommand("PASS "+remotePwd);
ReadReply();
if(replyCode!="202"&&replyCode!="230")
{
CleanUp();
throw new Exception("Error password!");
}
}
/// <summary>
/// 保存文件
/// </summary>
private void StoreFile()
{
SendFtpCommand("STOR "+remoteFilePath);
ReadReply();
if(replyCode!="125"&&replyCode!="150")
{
CleanUp();
throw new Exception("Prepare failure!");
}
FileStream stream=new FileStream(fileName,FileMode.Open,FileAccess.Read);
while((bytes=stream.Read(buffer,0,buffer.Length))!=0)
{
cs.Send(buffer,bytes,0);
}
string fileEnd="<EOF>";
byte[] byt=new byte[1024];
byt=System.Text.Encoding.ASCII.GetBytes(fileEnd.ToCharArray());
cs.Send(byt,byt.Length,0);
stream.Close();
ReadReply();
if(replyCode!="226"&&replyCode!="250")
{
CleanUp();
throw new Exception("Transfer error!");
}
QuitFtp();
cs.Close();
CleanUp();
}
private void QuitFtp()
{
SendFtpCommand("QUIT");
}
private void CleanUp()
{
if(cs.Connected)
cs.Close();
cs=null;
logined=false;
}
/// <summary>
/// 上传文件
/// </summary>
public void FtpFile()
{
Login();
if(logined)
{
StoreFile();
}
}
}
以上是我写的一个简单的FTP上传程序,目的是要传到远程的FTP主机上,但调试时发现
每次开始传文件时,就是在发送STOR命令之后的这一段程序
FileStream stream=new FileStream(fileName,FileMode.Open,FileAccess.Read);
while((bytes=stream.Read(buffer,0,buffer.Length))!=0)
{
cs.Send(buffer,bytes,0);
}
string fileEnd="<EOF>";
byte[] byt=new byte[1024];
byt=System.Text.Encoding.ASCII.GetBytes(fileEnd.ToCharArray());
cs.Send(byt,byt.Length,0);
stream.Close();
通过ReadReply()函数,得到返回的响应码为425,说Can't open data connection.
但STOR命令得到的响应码为150,说明数据连接已经打开.
请CSDN上的朋友们帮我看看.谢谢