直接使用异步发送BeginSend就好

解决方案 »

  1.   

    我是来顶贴的。。异步就是有beginXXX和endXXX的方法的
      

  2.   


     private void BeginListen()
            {
                IPAddress ip = GetServerIP();
                IPEndPoint ipe = new IPEndPoint(ip, 8000);
                s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                byte[] recBytes = new byte[1024];           
                s.Bind(ipe);//连接IP
                s.Listen(100);//监听            while (true)
                {
                    try
                    {
                        Socket temp = s.Accept();//新建SOCKET
                        int bytes;
                        bytes = temp.Receive(recBytes, recBytes.Length, 0);//接受信息
                        string resultcode;
                        string str;
                        str = Encoding.ASCII.GetString(recBytes, 0, bytes);
                        Data1 a = new Data1();
                        resultcode = a.Data2(str);
                        string sendstr = resultcode;
                        byte[] bs = Encoding.ASCII.GetBytes(sendstr);
                        temp.Send(bs, bs.Length, 0);//发送信息
                        Dispose();
                    }
                    catch (SocketException ex)
                    {
                        recvBox.Text += ex.ToString();
                    }            }        }
    试试这样