Example #1
0
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="config">传输配置</param>
        /// <param name="rev">接收数据回调</param>
        public void Init(NetConfig config, Action <ReceiveDatagramInfo> rev)
        {
            this._status = true;
            this._config = config;
            var listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                listenSocket.Bind(config.ListenEP);
                this._listenSocket      = listenSocket;
                this._listenThread      = new Thread(this.AcceptClient);
                this._listenThread.Name = "Tcp监听线程";
                //this._listenThread.IsBackground = true;
                this._rev = rev;
            }
            catch (Exception)
            {
                this._listenSocket = null;
                this.DisposeSocket(listenSocket);
                throw;
            }
        }
Example #2
0
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="config">传输配置</param>
        /// <param name="rev">接收数据回调</param>
        public void Init(NetConfig config, Action <ReceiveDatagramInfo> rev)
        {
            if (rev == null)
            {
                throw new ArgumentNullException(nameof(rev));
            }

            this._listenEP = config.ListenEP;
            this._rev      = rev;
            this._status   = true;

            this._socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            this._socket.ReceiveBufferSize = config.ReceiveBufferSize;
            this._socket.SendBufferSize    = config.SendBufferSize;
            this._socket.Bind(this._listenEP);

            //如果没有这两行代码,则windows底层BUG会在接收抛出异常,并将socket重置,然后再也收不到数据
            const int SIO_UDP_CONNRESET = -1744830452;

            this._socket.IOControl(SIO_UDP_CONNRESET, new byte[] { 0, 0, 0, 0 }, null);

            //初始化接收线程
            this._receiveDataThread = new ThreadEx(this.ReceiveDtaThreadMethod, "udp接收数据线程", true);
        }