示例#1
0
        //开始监听和设置
        private void StartListening()
        {
            string strIP   = txtIP.Text.Trim();
            string strport = txtPort.Text.Trim();

            if (string.IsNullOrWhiteSpace(strIP))
            {
                MessageBox.Show("服务器地址不能为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(strport))
            {
                MessageBox.Show("端口号不能为空");
                return;
            }
            int port = 0;

            if (int.TryParse(strport, out port))
            {
                if (port < 2000)
                {
                    MessageBox.Show("端口号取值范围为2000到65535");
                    return;
                }
                if (port > 65535)
                {
                    MessageBox.Show("端口号取值范围为2000到65535");
                    return;
                }
                if (IsUse(port))
                {
                    MessageBox.Show("当前端口号已被占用");
                    return;
                }
            }
            else
            {
                MessageBox.Show("端口号必须是整数");
                return;
            }
            //配置日志记录器
            //ILogger logger = new LiteLogger(LiteLogger.LogMode.ConsoleAndLogFile, "ServerLogFile_" + NetworkComms.NetworkIdentifier + ".txt");
            //NetworkComms.EnableLogging(logger);
            //不启用日志记录
            NetworkComms.DisableLogging();
            //如果某客户端离线,触发此方法
            NetworkComms.AppendGlobalConnectionCloseHandler(HandleConnectionClosed);


            #region 用户登录和聊天相关

            //客户端发来的聊天信息  转发之
            NetworkComms.AppendGlobalIncomingPacketHandler <MsgEntity>("ChatMessage", IncomingChatMessage);

            //客户端获取好友列表
            NetworkComms.AppendGlobalIncomingPacketHandler <string>("GetFriends", IncomingGetFriends);

            //获取所有在线用户列表

            NetworkComms.AppendGlobalIncomingPacketHandler <string>("GetOnlineUser", IncomingGetOnlineUser);

            //用户登录
            NetworkComms.AppendGlobalIncomingPacketHandler <IMUserInfo>("UserLogin", IncomingLoginHandler);
            //更改密码
            NetworkComms.AppendGlobalIncomingPacketHandler <UserPswEntity>("ChangePsw", IncomingChangePsw);

            //注册新用户
            //NetworkComms.AppendGlobalIncomingPacketHandler<RcUsers>("RegUser", IncomingRegisterUser);

            #endregion

            #region 用户和组相关
            //====================================================================================================
            ////获取所有用户组
            //NetworkComms.AppendGlobalIncomingPacketHandler<string>("GetGroup", IncomingGetGrooup);



            // 获取离线消息
            NetworkComms.AppendGlobalIncomingPacketHandler <UserIDContract>("GetMyOffLineMsg", IncomingMyOffLineMsg);

            //获取所有在线用户的P2P信息
            NetworkComms.AppendGlobalIncomingPacketHandler <string>("GetP2PInfo", IncomingP2PInfo);

            #endregion
            IPEndPoint thePoint = new IPEndPoint(IPAddress.Parse(strIP), port);
            TCPConnection.StartListening(thePoint, false);
            AddLineToLog("初始化完成,正在监听:");
            AddLineToLog("IP地址" + strIP + "--" + "端口" + port.ToString());
        }