Ejemplo n.º 1
0
        //
        // 网络通信的功能
        //

        public bool ConnectNet(Message msg)
        {
            if (myClientSoc.connected == false)
            {
                myClientSoc.InitialSoc();
                // 如果网络连接失败
                if (myClientSoc.connected == false)
                {
                    return(false);
                }
            }

            userSend(msg);
            Message serverMsg = myClientSoc.RecieveMsg();

            if (serverMsg.msgType == MsgType.Sys)
            {
                MsgSys loginMsg = (MsgSys)serverMsg.msgContent;
                if (loginMsg.sysType == MsgSysType.Judge)
                {
                    bool ret = (bool)loginMsg.sysContent;
                    // 登录失败 直接返回
                    if (ret != true)
                    {
                        MessageBox.Show("登录失败!");
                        return(false);
                    }

                    // 登录成功
                    keepalive = true;
                    myStatus  = PlayerStatus.OnLine;
                }
                else
                {
                    // 如果登录不成功,主动结束socket
                    // 下次会重新连接。
                    myClientSoc.CloseConn();
                    return(false);
                }
            }

            this.myStatus = PlayerStatus.OnLine;
            return(true);
        }
Ejemplo n.º 2
0
        //
        // 网络通信的功能
        //
        public void ConnectNet(string msg)
        {
            if (keepalive == false)
            {
                //... someting to do
                // 初始化网络
                keepalive = true;

                myClientSoc.InitialSoc();


                // 启动单独的线程用于接收服务器端发送来的消息
                //receiveThread = new Thread(new ThreadStart(NetRuning));
                if (receiveThread == null)
                {
                    receiveThread = new Thread(new ThreadStart(NetRuning));
                }
                receiveThread.Start();

                myClientSoc.SendStr("login", msg);
            }
        }