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

本文关键字:一次 许使用 网络地址 服务器 套接字 TCP 协议 地址 端口 | 更新日期: 2023-09-27 18:28:39

我有一个TCP服务器(作为windows服务实现),用于监听车辆跟踪应用程序中的GPS设备,在其运行后的一段随机时间后,我得到了以下错误:"通常只允许使用每个套接字地址(协议/网络地址/端口)一次。"虽然我确信我在使用后会关闭每个套接字。所以有人能告诉我这里有什么问题吗?我在windows server 2008注册表中的MaxUserPort值为(65534),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;
    }

请帮忙吗?

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

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

修复?您需要在调用EndAccept 之后设置mainDone事件

更好的是,遵循一个更简单的模式,不使用同步原语。我在这里概述一个。