当前位置:编程学习 > C#/ASP.NET >>

socket.Receive 堵塞问题


    public Form1()
        {
            InitializeComponent();
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            int port = 6678;
            listener = new TcpListener(port);
            listener.Start();
            Thread thread = new Thread(new ThreadStart(reControlCode));
            thread.Start();
        }
    private void reControlCode()
        {
            try
            {
                //设置接收套接字,接收listener.AcceptSocket是返回已经接收的客户的请求
                socket = listener.AcceptSocket();
                while (socket.Connected)
                {
                      string str = "";
                    string ss = "";
                    byte[] recvBytes = new byte[1024];
                    int bytes = 0;
                    while (true)
                    {
                        bytes = socket.Receive(recvBytes, recvBytes.Length, 0);
                        if (bytes <= 0)
                            break;
                        ss += Encoding.ASCII.GetString(recvBytes, 0, bytes);
                    } 
                    switch (ss)
                    {
                        case "openstar":
                            str = "true";
                            SendMessage(str);
                            break;
                        default:
                            break;
                    }
                }
            }
            catch
            {
            }
        }

这个数据读完之后运行到 bytes = socket.Receive(recvBytes, recvBytes.Length, 0);就跳出去了,大家帮我解决一下,谢谢啊

补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,