예제 #1
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.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;
         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);
     }
 }
예제 #2
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);
            }
        }
예제 #3
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);
     }
 }
예제 #4
0
 /// <summary>
 /// 客户端断开连接
 /// </summary>
 /// <param name="token"> 断开连接的用户对象</param>
 /// <param name="error">断开连接的错误编码</param>
 public void ClientClose(UserToken token,string error) {
     if (token.conn != null) {
         lock (token) { 
          //通知应用层面 客户端断开连接了
             center.ClientClose(token, error);
             token.Close();
             //加回一个信号量,供其它用户使用
             pool.push(token);
             acceptClients.Release();                   
         }
     }
 }
예제 #5
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);
     }
 }
예제 #6
0
 /// <summary>
 /// 客户端断开连接
 /// </summary>
 /// <param name="token">断开连接的用户对象</param>
 /// <param name="error">断开连接的错误编码</param>
 public void ClientClose(UserToken token, string error)
 {
     if (token.conn != null)
     {
         lock (token)
         {
             //通知应用层面,客户端断开连接
             center.ClientClose(token, error);
             token.Close();
             pool.push(token);
             //信号量+1,供其他用户使用
             acceptClients.Release();
         }
     }
 }
예제 #7
0
        private int port;        // 端口
        public ServerStart(int max, int port)
        {
            server         = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            this.macClient = max;
            this.port      = port;

            pool          = new UserTokenPool(max);
            acceptClients = new Semaphore(max, max);
            for (int i = 0; i < max; i++)
            {
                UserToken userToken = new UserToken();

                userToken.receiveSAEA.Completed += new EventHandler <SocketAsyncEventArgs>(IO_Completed);
                userToken.sendSAEA.Completed    += new EventHandler <SocketAsyncEventArgs>(IO_Completed);

                pool.push(userToken);
            }
        }