C# SOCKET编程 求教
自己尝试进行FTP的SOCKET编程,遇到些问题,特此求教各位高手赐教!谢谢!
1)我用同步,异步混编模式(即命令的发送与响应消息接收用同步模式,SEND,RECEIVE,数据接收用异步beginreceive,endreceive),发起连接FTP时创建一个线程A(避免用户界面无响应),并处理登陆,列表显示文件目录,这个过程OK,没问题。当按下窗体按钮切换路径时,常常会出现接收不到响应消息,但是连接国外FTP确又能收到,何解?难道是本机延迟太小,来不及响应?
private void sendCommand(String command) //发送命令
{
Byte[] cmdBytes = System.Text.Encoding.Default.GetBytes((command + "\r\n ").ToCharArray());
clientSocket.Send(cmdBytes, cmdBytes.Length, 0);
this.readResponse();
}
private void readResponse() //接收响应消息
{
this.rmsg = " ";
response = this.readLine();
}
private string readLine()
{
while (true)
{
this.bytes = clientSocket.Receive(this.buffer, this.buffer.Length, 0);
this.rmsg += System.Text.Encoding.Default.GetString(this.buffer, 0, this.bytes);
if (this.bytes < this.buffer.Length)
{
break;
}
}
}
2)在用ListView控件进行文件列表显示时,刚登陆时可以正常显示,但是切换改变路径后,异步接收的回调函数却说socket已被disposed,难道函数只能调用一次?
Receive(cSocket); //异步接收返回的文件列表
allDone.WaitOne();
IAsyncResult result = ListViewFtpServ.BeginInvoke(new System.EventHandler(ShowList), rmsg);