using System;
using System.Net;
using System.IO;
using System.Text;
using System.Net.Sockets;
namespace WindowsApplication1.Bll
{
///
/// FTPClient 的摘要说明。
///
public class FTPClient
{
#region 构造函数
///
/// 缺省构造函数
///
public FTPClient()
{
strRemoteHost = "";
strRemotePath = "";
strRemoteUser = "";
strRemotePass = "";
strRemotePort = 21;
bConnected = false;
} ///
/// 构造函数
///
///
///
///
///
///
public FTPClient(string remoteHost, string remotePath, string remoteUser, string remotePass, int remotePort)
{
strRemoteHost = remoteHost;
strRemotePath = remotePath;
strRemoteUser = remoteUser;
strRemotePass = remotePass;
strRemotePort = remotePort;
Connect();
}
#endregion
#region 登陆
///
/// FTP服务器IP地址
///
private string strRemoteHost;
public string RemoteHost
{
get
{
return strRemoteHost;
}
set
{
strRemoteHost = value;
}
}
///
/// FTP服务器端口
///
private int strRemotePort;
public int RemotePort
{
get
{
return strRemotePort;
}
set
{
strRemotePort = value;
}
}
///
/// 当前服务器目录
///
private string strRemotePath;
public string RemotePath
{
get
{
return strRemotePath;
}
set
{
strRemotePath = value;
}
}
///
/// 登录用户账号
///
private string strRemoteUser;
public string RemoteUser
{
set
{
strRemoteUser = value;
}
}
///
/// 用户登录密码
///
private string strRemotePass;
public string RemotePass
{
set
{
strRemotePass = value;
}
} ///
/// 是否登录
///
private Boolean bConnected;
public bool Connected
{
get
{
return bConnected;
}
}
#endregion
#region 链接
///
/// 建立连接
///
public void Connect()
{
socketControl = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(IPAddress.Parse(RemoteHost), strRemotePort);
// 链接
try
{
socketControl.Connect(ep);
}
catch (Exception)
{
throw new IOException("Couldn't connect to remote server");
} // 获取应答码
ReadReply();
if (iReplyCode != 220)
{
DisConnect();
throw new IOException(strReply.Substring(4));
} // 登陆
SendCommand("USER " + strRemoteUser);
if (!(iReplyCode == 331 || iReplyCode == 230))
{
CloseSocketConnect();//关闭连接
throw new IOException(strReply.Substri