首页 文章

TCP服务器:“通常只允许使用每个套接字地址(协议/网络地址/端口) . ”

提问于
浏览
1

我有一个TCP服务器(实现为Windows服务),用于在车辆跟踪应用程序中监听GPS设备,从其操作后的一段时间后我得到以下错误:“每个套接字地址只有一次使用(协议/网络地址) / port)通常是允许的 . “虽然我确信我在使用它后关闭每个插槽 . 所以有人能告诉我这里有什么问题我在Windows Server 2008注册表中使用(65534)的MaxUserPort值和TCPTimeWaitDelay值(30秒)?

这是代码:1)主线程:

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 );
        // Addedd on [20/03/2012] by Sherif Mosaad for handling socket exceptions ...
        //listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, (int)1);

        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.Shutdown(SocketShutdown.Both);
            listener.Close();
            Thread.Sleep(2000);

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

2)AcceptCallback处理程序:

private void AcceptCallback(IAsyncResult ar) {
        mainDone.Set();

        Socket listener = (Socket)ar.AsyncState;
        Socket handler = null;
        try
        {
            handler = listener.EndAccept(ar);
        }
        catch 
        {
            try
            {
                listener.Shutdown(SocketShutdown.Both);
                listener.Close();
                Thread.Sleep(2000);
            }
            catch { return; }
        }

        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);
    }

3)ReadCallback处理程序:

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

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

        if (bytesRead > 0)
        {
            string data = Encoding.ASCII.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)
            {
                // Connection closed by client
                if (OnDisconnect != null)
                    OnDisconnect(this, state.endPoint);
                return;
            }
        }
        else
        {
            // Connection closed by peer
            if (OnDisconnect != null)
                OnDisconnect(this, state.endPoint);
        }
    }

最后是状态对象:

public class StateObject
    {
        public Socket workSocket = null;
        public const int BufferSize = 1024;
        public byte[] buffer = new byte[BufferSize];
        public StringBuilder sb = new StringBuilder();
        public IPEndPoint endPoint;
    }

有什么帮助吗?

1 回答

  • 0

    有竞争条件 . 您调用 mainDone.Set ,允许另一个线程进入 BeginAccept ,同时当前线程移向 EndAccept . 哪个会先到达那里?如果您在完成上一次接受之前开始接受,我怀疑可能会弹出此错误 .

    固定?调用 EndAccept 后需要设置 mainDone 事件

    更好的是,遵循更简单的模式,没有同步原语 . 我概述了一个here .

相关问题