private void ReadCallBack(IAsyncResult ar)  异步接受回调方法
        {
            StateObject state = (StateObject)ar.AsyncState;
            Socket TT = state.worksocket;            int bytesRead = handler.EndReceive(ar);
            state.sb.Append(System.Text.Encoding.ASCII.GetString(state.buffer,0,bytesRead));
            string content = state.sb.ToString();
            state.sb.Remove(0,content.Length);
            TextBox1.Text += content + "\r\n";
            TT.BeginReceive(state.buffer,0,StateObject.buffersize,0,new AsyncCallback(ReadCallBack),state);        }
      int bytesRead = handler.EndReceive(ar);这句总报错,“由于线程退出或应用程序请求,以放弃I/O操作。”

解决方案 »

  1.   

    在BeginReceive之后你做了什么操作啊,是不是将当前的线程之类的进行了关闭啊
      

  2.   

    我贴源码你给我看哈
    我弄了三天都没有弄出来
      private IPAddress myIp;
            private IPEndPoint myserver;
            private Socket sock;
            private Socket handler;
            private static ManualResetEvent Done = new ManualResetEvent(false);
            protected void Page_Load(object sender, EventArgs e)
            {        }
            //监听
            #region
            /// <summary>
            /// 监听服务器端口
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            #endregion
            protected void Button1_Click(object sender, EventArgs e)
            {
                try
                {
                    myIp =IPAddress.Parse(TextBox3.Text);
                }
                catch(Exception E)
                {
                    show(E.Message);
                }
                try
                {
                    //服务器的IP和port
                    myserver = new IPEndPoint(myIp,int.Parse(TextBox4.Text));
                    //实例化一个SOCKET
                    sock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
                    //关联服务器和socket
                    sock.Bind(myserver);
                    //使socket处于侦听状态
                    sock.Listen(50);
                    status.Text = "主机" + TextBox3.Text + "端口" + TextBox4.Text + "正在监听";                //开启线程
                    Thread thread = new Thread(new ThreadStart(listenfunction));
                    thread.Start();
                }
                catch (Exception ee)
                {
                    status.Text = ee.Message;
                }
            }        #region
            /// <summary>
            /// 监听的工作线程的方法
            /// </summary>
            private void listenfunction()
            {
                while(true)
                {
                    //ManualResetEvent复位
                    Done.Reset();                //开始接受请求连接
                    sock.BeginAccept(new AsyncCallback(AcceptCallBack),sock);                //等待信号
                    Done.WaitOne();
                }
            }
            #endregion        #region
            private void AcceptCallBack(IAsyncResult ar)
            {
                //允许继续等待!
                Done.Set();
                Socket listener = (Socket)ar.AsyncState;
                handler = listener.EndAccept(ar);            StateObject state = new StateObject();
                state.worksocket = handler;
                status.Text = "与客户建立连接";
                try
                {
                    byte[] bytedata = System.Text.Encoding.ASCII.GetBytes("准备完毕,可以通话!"+"\n\r");
                   // handler.BeginSend(bytedata,0,bytedata.Length,0,new AsyncCallback(SendCallBack),handler);            }
                catch(Exception e)
                {
                    show(e.Message);
                }
                Thread threads = new Thread(new ThreadStart(rec));
                threads.Start();
            }
            #endregion        #region
            /// <summary>
            /// 异步接收
            /// </summary>
            private void rec()
            {
                StateObject state = new StateObject();
                state.worksocket = handler;            //开始异步接收            handler.BeginReceive(state.buffer,0,StateObject.buffersize,0,new AsyncCallback(ReadCallBack),state);
            }
            #endregion
            #region
            /// <summary>
            /// 异步接受回调方法
            /// </summary>
            /// <param name="ar"></param>
            private void ReadCallBack(IAsyncResult ar)
            {
                StateObject state = (StateObject)ar.AsyncState;
                Socket TT = state.worksocket;            int bytesRead = handler.EndReceive(ar);
                state.sb.Append(System.Text.Encoding.ASCII.GetString(state.buffer,0,bytesRead));
                string content = state.sb.ToString();
                state.sb.Remove(0,content.Length);
                TextBox1.Text += content + "\r\n";
                TT.BeginReceive(state.buffer,0,StateObject.buffersize,0,new AsyncCallback(ReadCallBack),state);        }
            #endregion
            #region
            /// <summary>
            /// 异步发送的回调方法
            /// </summary>
            /// <param name="ar"></param>
            private void SendCallBack(IAsyncResult ar)
            {
                try
                {
                    handler = (Socket)ar.AsyncState;
                    int bytesSent = handler.EndSend(ar);
                }
                catch
                {
                    show(" !");
                }        }
            #endregion        #region
            /// <summary>
            /// 弹出脚本框
            /// </summary>
            /// <param name="str"></param>
            private void show(string str)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(),"","<script>alert('"+str+"')</script>");
            }
            #endregion        #region
            /// <summary>
            /// 停止监听
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void Button3_Click(object sender, EventArgs e)
            {
                try
                {
                    sock.Close();
                    status.Text = "主机" + TextBox3.Text + "端口" + TextBox4.Text + "停止监听";
                }
                catch
                {
                    show("监听尚未开始,关闭无效!");
                }
            }
            #endregion
            #region
            /// <summary>
            /// 发送数据
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void Button2_Click(object sender, EventArgs e)
            {
                try
                {
                    byte[] bytedata = System.Text.Encoding.ASCII.GetBytes(TextBox2.Text+"\r\n");
                    handler.BeginSend(bytedata,0,bytedata.Length,0,new AsyncCallback(SendCallBack),handler);
                }
                catch(Exception E)
                {
                    show(E.Message);
                }
            }
            
            #endregion
        }