示例#1
0
        // 初始化通信监听
        public ServerStart(int max, int port)
        {
            // 实例化监听对象
            server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            // 设定服务器最大连接人数
            maxClient = max;
            // 创建连接池
            pool = new UserTokenPool(max);

            // 连接信号量
            acceptClients = new Semaphore(max, max);
            for (int i = 0; i < max; ++i)
            {
                UserToken token = new UserToken();
                // 初始化token信息

                token.receiveSAEA.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Comleted);

                token.sendSAEA.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Comleted);

                token.LD     = LD;
                token.LE     = LE;
                token.encode = encode;
                token.decode = decode;

                token.sendProcess = ProcessSend;    // 将具体方法初始化给UserToken

                pool.push(token);
            }
        }
示例#2
0
        private Socket _server; //服务器Socket监听对象

        #endregion Fields

        #region Constructors

        /// <summary>
        /// 初始化通信监听
        /// </summary>
        /// <param name="max">最大连接数</param>
        /// <param name="port">监听的端口</param>
        public ServerStart(int max,int port)
        {
            _server = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            _maxClient = max;                               //实例化socket监听对象

            _pool=new UserTokenPool(max);                   //实例化连接池对象

            //实例化一个最大允许max个线程允许的信号量
            //并将它的计数器的初始值设为max
            //这就是说除了调用该信号量的线程都将被阻塞
            _acceptClients = new Semaphore(max,max);

            //初始化创建max个数的链接对象并存入连接池
            for (var i = 0; i < max; i++)
            {
                //初始化token信息
                var token=new UserToken();

                //绑定接收事件
                token.ReceiveSAEA.Completed+=
                    new EventHandler<SocketAsyncEventArgs>(IO_Comleted);

                //绑定发送事件
                token.SendSAEA.Completed+=
                    new EventHandler<SocketAsyncEventArgs>(IO_Comleted);

                token.LD = LD;
                token.LE = LE;
                token.encode = encode;
                token.decode = decode;
                token.sendProcess = ProccessSend;

                _pool.Push(token);
            }
        }
示例#3
0
 /// <summary>开启服务端 </summary>
 public void Start(int port)
 {
     //创建连接池
     pool = new UserTokenPool(maxClient);
     //连接信号量
     acceptClients = new Semaphore(maxClient, maxClient);
     for (int i = 0; i < maxClient; i++)
     {
         UserToken token = new UserToken();
         //初始化token信息
         token.ReceiveSAEA.Completed += new EventHandler <SocketAsyncEventArgs>(IOCompleted);
         token.SendSAEA.Completed    += new EventHandler <SocketAsyncEventArgs>(IOCompleted);
         token.LD           = LD;
         token.LE           = LE;
         token.Encode       = Encode;
         token.Decode       = Decode;
         token.SendProcess  = ProcessSend;
         token.CloseProcess = ClientClose;
         token.Center       = Center;
         pool.Push(token);
     }
     //监听当前服务器网卡所有可用IP地址的port端口
     //外网IP 内网IP192.168.x.x 本机IP一个127.0.0.1
     try {
         server.Bind(new IPEndPoint(IPAddress.Any, port));
         //置于监听状态,并没有真正的有监听事件
         server.Listen(10);
         StartAccept(null);
     } catch (Exception e) {
         Console.WriteLine(e.Message);
     }
 }
示例#4
0
 public void Start(int port)
 {
     pool          = new UserTokenPool(maxClient);
     acceptClients = new Semaphore(maxClient, maxClient);
     for (int i = 0; i < maxClient; i++)
     {
         UserToken token = new UserToken();
         //TODO初始化连接信息
         token.receiveSAEA.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
         token.sendSAEA.Completed    += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
         token.lengthDecode           = lengthDecode;
         token.lengthEncode           = lengthEncode;
         token.msgDecode              = msgDecode;
         token.msgEncode              = msgEncode;
         token.sendProssess           = ProcessSend;
         token.closeProcess           = ClientClose;
         token.center = center;
         pool.push(token);
     }
     try
     {
         Server.Bind(new IPEndPoint(IPAddress.Any, port)); //监听当前服务器网卡所有可用IP地址和端口
         Server.Listen(10);                                //置于监听
         StartAccept(null);
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }
示例#5
0
        public void Start(int port)
        {
            pool          = new UserTokenPool(maxClient);
            acceptClients = new Semaphore(maxClient, maxClient);
            for (int i = 0; i < maxClient; i++)
            {
                UserToken userToken = new UserToken();
                //初始化token信息
                userToken.receiveSAEA.Completed += IO_Comleted;
                userToken.sendSAEA.Completed    += IO_Comleted;
                userToken.LD           = LD;
                userToken.LE           = LE;
                userToken.encode       = encode;
                userToken.decode       = decode;
                userToken.sendProcess  = ProcessSend;
                userToken.closeProcess = ClientClose;
                userToken.center       = center;
                pool.Push(userToken);
            }

            //监听当前服务器网卡所有可用IP地址的port端口
            // 外网IP  内网IP192.168.x.x 本机IP一个127.0.0.1
            try
            {
                server.Bind(new IPEndPoint(IPAddress.Any, port));
                //置于监听状态
                server.Listen(10);
                StartAccept(null);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
示例#6
0
 public ServerStart(int max)
 {
     server        = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     maxClient     = max;
     pool          = new UserTokenPool(max);
     acceptClients = new Semaphore(max, max);
     for (int i = 0; i < max; i++)
     {
         UserToken token = new UserToken();
         token.receiveSAEA.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
         token.sendSAEA.Completed    += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
         pool.Push(token);
     }
 }
示例#7
0
 // 初始化通信监听
 public ServerStart(int max)
 {
     // 实例化监听对象
     server    = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     maxClient = max;
     // 创建连接池
     pool          = new UserTokenPool(max);
     acceptClients = new Semaphore(max, max);
     for (int i = 0; i < max; i++)
     {
         UserToken token = new UserToken();
         // 初始化token信息
         pool.push(token);
     }
 }
示例#8
0
        public void Start(int port)
        {
            //创建连接池
            pool = new UserTokenPool(maxClient);
            //连接信号量
            acceptClients = new Semaphore(maxClient, maxClient);

            for (int i = 0; i < maxClient; i++)
            {
                UserToken token = new UserToken();
                //初始化token信息
                token.sendSAEA.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Comleted);

                token.receiveSAEA.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Comleted);

                token.LD           = LD;
                token.LE           = LE;
                token.encode       = encode;
                token.decode       = decode;
                token.sendProcess  = ProcessSend;
                token.closeProcess = ClientClose;
                token.center       = center;

                pool.push(token);
            }

            //通过port端口监听当前所有ip
            try
            {
                server.Bind(new IPEndPoint(IPAddress.Any, port));
                //置于监听状态
                server.Listen(10);
                StartAccept(null);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
示例#9
0
        /// <summary>
        /// 初始化对象池
        /// </summary>
        /// <param name="max">连接数</param>
        private void InitTokenPool(int max)
        {
            pool         = new UserTokenPool(max);  //创建连接池
            acceptClient = new Semaphore(max, max); //连接信号量,初始化数量和最大连接数量

            //在初始化的时候就将连接对象依次创建,好处:省去了连接时候创建,断开时销毁的过程,坏处:服务器连接空余较多
            for (int i = 0; i < max; i++)
            {
                UserToken token = new UserToken();
                //初始化token信息
                token.receiveSAEA.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
                token.sendSAEA.Completed    += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
                token.lengthEncode           = lengthEncode;
                token.lengthDecode           = lengthDecode;
                token.messageEncode          = messageEncode;
                token.messageDecode          = messageDecode;
                token.sendProcess            = ProcessSend;
                token.closeProcess           = ClientClose;
                token.center = center;

                pool.Push(token);
            }
        }
示例#10
0
 /// <summary>开启服务端 </summary>
 public void Start(int port)
 {
     //创建连接池
     pool = new UserTokenPool(maxClient);
     //连接信号量
     acceptClients = new Semaphore(maxClient, maxClient);
     for (int i = 0; i < maxClient; i++)
     {
         UserToken token = new UserToken();
         //初始化token信息
         token.ReceiveSAEA         = null;
         token.SendSAEA.Completed += new EventHandler <SocketAsyncEventArgs>(IOCompleted);
         token.LD           = LD;
         token.LE           = LE;
         token.Encode       = Encode;
         token.Decode       = Decode;
         token.SendProcess  = ProcessSend;
         token.CloseProcess = ClientClose;
         token.Center       = Center;
         pool.Push(token);
     }
     receiveSocketArgs                = new SocketAsyncEventArgs();
     receiveSocketArgs.Completed     += new EventHandler <SocketAsyncEventArgs>(IOCompleted);
     receiveSocketArgs.RemoteEndPoint = new IPEndPoint(IPAddress.Any, port);
     receiveSocketArgs.SetBuffer(new byte[1024], 0, 1024);
     //监听当前服务器网卡所有可用IP地址的port端口
     //外网IP 内网IP192.168.x.x 本机IP一个127.0.0.1
     try {
         server.Bind(new IPEndPoint(IPAddress.Any, port));
         sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
         //置于监听状态,并没有真正的有监听事件
         //server.Listen(10);
         StartReceive();
     } catch (Exception e) {
         Console.WriteLine(e.Message);
     }
 }
示例#11
0
        /// <summary>
        /// 初始化对象池
        /// </summary>
        /// <param name="max">连接数</param>
        private void InitTokenPool(int max)
        {
            pool = new UserTokenPool(max); //创建连接池
            acceptClient = new Semaphore(max, max); //连接信号量,初始化数量和最大连接数量

            //在初始化的时候就将连接对象依次创建,好处:省去了连接时候创建,断开时销毁的过程,坏处:服务器连接空余较多
            for (int i = 0; i < max; i++)
            {
                UserToken token = new UserToken();
                //初始化token信息
                token.receiveSAEA.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed);
                token.sendSAEA.Completed += new EventHandler<SocketAsyncEventArgs>(IO_Completed);
                token.lengthEncode = lengthEncode;
                token.lengthDecode = lengthDecode;
                token.messageEncode = messageEncode;
                token.messageDecode = messageDecode;
                token.sendProcess = ProcessSend;
                token.closeProcess = ClientClose;
                token.center = center;

                pool.Push(token);
            }
        }