自己尝试进行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);  //在ListView控件里显示文件列表
            ListViewFtpServ.EndInvoke(result);        private void ReceiveCallback(IAsyncResult ar) //异步接收的回调
        {
            try
            {
                StateObject state = (StateObject)ar.AsyncState;
                Socket client = state.workSocket;                int bytesRead = client.EndReceive(ar); //第二次使用异步接收时说client被disposed                if (bytesRead > 0)
                {
                    state.sb.Append(Encoding.Default.GetString(state.buffer, 0, bytesRead));                    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                        new AsyncCallback(ReceiveCallback), state);
                }
                else
                {
                    if (state.sb.Length > 1)
                    {
                        this.rmsg = state.sb.ToString();
                    }
                    allDone.Set();
                }
            }
            catch (Exception e)
            {
                ShowMsg(e.ToString(), false);
            }
        }3)能不能对同一个线程(thread)指定不同的方法,比如一开始按下连接按钮,执行
thread = new Thread(new ThreadStart(Login));
thread.Start();
连接上看到文件目录列表后,选中后按下切换目录按钮后,执行
thread = new Thread(new ThreadStart(ChangeDirectory));
thread.Start();还望路过的达人不吝赐教,谢谢!