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 NetRuning()
        {
            while (keepalive)
            {
                // 处理网络连接
                if (myClientSoc.connected)
                {
                    receiveStr = myClientSoc.RecieveStr();
                    if (receiveStr != null && receiveStr[0] != null)
                    {
                        //pthread = new Thread(new ThreadStart(processStr));
                        //pthread.Start();
                        if (processStr())
                        {
                            break;
                        }
                    }
                }
            }

            keepalive = false;
            myClientSoc.CloseConn();
            //receiveThread.Abort();
        }