TCP套接字没有响应

时间:2012-02-21 19:42:57

标签: c# sockets tcpclient

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

首先,

private void MainThread(){             byte [] bytes = new Byte [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 AcceptCallback(IAsyncResult ar) {
        Socket handler = null;
        try
        {
            mainDone.Set();

            Socket listener = (Socket)ar.AsyncState;
            handler = listener.EndAccept(ar);

            if (OnConnect != null)
                OnConnect(this, handler);

            StateObject state = new StateObject();

            state.workSocket = handler;
            state.endPoint = (IPEndPoint)handler.RemoteEndPoint;
            stateObjectDictionary.Add(state, state.workSocket);
            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                new AsyncCallback(ReadCallback), state);
        }
        catch (ObjectDisposedException)
        {
            // Connection closed by client
            if (OnDisconnect != null)
                OnDisconnect(this, (IPEndPoint)handler.RemoteEndPoint);
            return;
        }
        catch (Exception ex)
        {
            LogManager.LogError(ex, "TCPSimpleServer AcceptCallback");
            return;
        }

最后,

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()方法中出现问题,socket(处理程序)永远不会释放端口。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

可能是,客户端可以阻止服务器:

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