17,740
社区成员
发帖
与我相关
我的任务
分享
//连接FTP
public int ConnectFTP(string Host, string User, string Pass)
{
try
{
//初始化Socket
ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ep = new IPEndPoint(Dns.GetHostAddresses(Host)[0], 21); //默认端口号是21
//根据IP和端口号连接FTP
ClientSocket.Connect(ep);
ReadMessage();
if (RetMsgId != 220)
{
ClientSocket = null;
return 0;
}
//登录FTP
SendCommand("USER " + User);
if (!(RetMsgId == 331 || RetMsgId == 230))
{
//FTPの接続を閉じる
//DisconnectFTP();
return 0;
}
//パスワードを送信する
SendCommand("PASS " + Pass);
if (!(RetMsgId == 230 || RetMsgId == 202))
{
//FTPの接続を閉じる
//DisconnectFTP();
return 0;
}
return 1;
}
catch (Exception ex)
{
throw ex;
}
}
static void Main(string[] args)
{
ProcessStartInfo info = new ProcessStartInfo("ftp");
info.UseShellExecute = false;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
Process p = Process.Start(info);
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);//2.0新增加的事件
p.BeginOutputReadLine();
p.StandardInput.WriteLine("?");
p.StandardInput.WriteLine("quit");
p.WaitForExit();
Console.WriteLine("Process exited.");
Console.ReadLine();
}
static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
Console.WriteLine(e.Data);
}