C# TCP套接字没有响应

C# TCP套接字没有响应,c#,sockets,tcpclient,C#,Sockets,Tcpclient,我正在使用C#socket编程开发TCP客户机/服务器应用程序。有时,我会遇到一个非常奇怪的问题,因为服务器(windows服务)在端口(8089)上运行,但它没有侦听任何客户端请求,当我使用端口扫描程序测试端口时,它告诉我端口没有响应!这是我的服务器代码: 首先, 私有void主线程(){ 字节[]字节=新字节[1024] IPEndPoint localEndPoint = new IPEndPoint(0, this.port); Socket liste

我正在使用C#socket编程开发TCP客户机/服务器应用程序。有时,我会遇到一个非常奇怪的问题,因为服务器(windows服务)在端口(8089)上运行,但它没有侦听任何客户端请求,当我使用端口扫描程序测试端口时,它告诉我端口没有响应!这是我的服务器代码:

首先,

私有void主线程(){ 字节[]字节=新字节[1024]

        IPEndPoint localEndPoint = new IPEndPoint(0, this.port);

        Socket listener = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp );

        try {
            listener.Bind(localEndPoint);
            listener.Listen(100);

            while (active) {
                mainDone.Reset();

                listener.BeginAccept(new AsyncCallback(AcceptCallback),listener);

                while (active)
                    if (mainDone.WaitOne(100, true))
                        break;
            }
            listener.Close();

        } catch (Exception e) {
            if (OnError != null)
                OnError(this, e.ToString());
            LogManager.LogError(e, "TCPSimpleServer MainThread"); 
        }
那么

最后

private void ReadCallback(IAsyncResult ar) {
        try
        {
            String content = String.Empty;
            StateObject state = (StateObject)ar.AsyncState;
            Socket handler = state.workSocket;

            int bytesRead = 0;
            try
            {
                bytesRead = handler.EndReceive(ar);
            }
            catch (Exception ex)
            {
                // Connection closed by client
                if (OnDisconnect != null)
                    OnDisconnect(this, state.endPoint);
                handler.Close();
                return;
            }

            if (bytesRead > 0)
            {
                string data = Encoding.Default.GetString(state.buffer, 0, bytesRead);

                if (OnDataAvailable != null)
                    OnDataAvailable(this, handler, data);
                try
                {
                    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                        new AsyncCallback(ReadCallback), state);
                }
                catch (Exception e)
                {
                    if (OnError != null)
                        OnError(this, e.Message);
                    LogManager.LogError(e, "TCPSimpleServer ReadCallback");
                    handler.Close();
                }
            }
            else
            {
                // Connection closed by peer
                if (OnDisconnect != null)
                    OnDisconnect(this, state.endPoint);
            }
        }
        catch (Exception ex)
        {
            LogManager.LogError(ex, "TCPSimpleServer ReadCallback");
        }
    }

我认为问题出在最后一个方法ReadCallback()中,如果EndReceive()方法出现问题,套接字(处理程序)将永远不会释放端口。请提供任何帮助?

是否客户端可以在以下位置阻止服务器:

while (active) 
    if (mainDone.WaitOne(100, true)) 
        break; 
while (active) 
    if (mainDone.WaitOne(100, true)) 
        break;