コード例 #1
0
        public void requestFriends()
        {
            dataReceived = false;
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect(new IPEndPoint(ipAdress, port));
            // use the ipaddress as in the server program

            receiveState = new ServerState();
            receiveState.Client = socket;

            //send request message
            //SendMessage(1, username, password);
        }
コード例 #2
0
        public void ReceiveMessageThread()
        {
            while (runServer)
            {
                try
                {
                    ServerState state2 = new ServerState();

                    state2.Client = socket;
                    socket.BeginReceive(state2.Buffer, 0, state2.Buffer.Length, SocketFlags.None, new AsyncCallback(ServerReadCallback), state2);
                    Thread.Sleep(50);
                }
                catch (SocketException socketEx)
                {
            #if DEBUG
                    GuiController.getInstance().ShowMessageBox(socketEx.ToString());
            #endif
                }
            }
        }
コード例 #3
0
        public void SendLogin(string username, string password)
        {
            try
            {
                bool timeOut = false;
                dataReceived = false;
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                GuiController.getInstance().SetDebugText("Connecting...");
                socket.Connect(new IPEndPoint(ipAdress, port));
                // use the ipaddress as in the server program

                receiveState = new ServerState();
                receiveState.Client = socket;

                GuiController.getInstance().SetDebugText("Connected... Checking login credentials");

                SendMessage(1, username, password);

                conTimeOut.Start();
                while (!dataReceived)
                {
                    if (timerElapsed)
                    {
                        timeOut = true;
                        break;
                    }
                    Thread.Sleep(50);
                }

                if (!timeOut)
                {

                    Console.Write(data[4]);

                    //Byte values
                    //***********************************
                    //255 =   "Login Succes"
                    //0   =   "Account does not exist"
                    //1   =   "Account blocked"
                    //2   =   "Account already logged in"
                    switch (data[4])
                    {
                        case 255:
                            if (data.Length >= 6)
                            {
                                string accountInfo = "";

                                for (int i = 5; i < data.Length; i++)
                                    accountInfo += (Convert.ToChar(data[i]));

                                DomainController.getInstance().ReceiveAccountInfo(accountInfo, true);

                                DomainController.getInstance().loggedIn = true;

                                if (DomainController.getInstance().myAccount.guildId != -1)
                                {
                                    DomainController.getInstance().AddLobbyMessageToQueue(32, DomainController.getInstance().myAccount.guildId.ToString());
                                }

                                //Update GUI
                                GuiController.getInstance().SetDebugText("Succesfully connected!");
                                GuiController.getInstance().UserLoggedIn();

                                NetworkController.getInstance().StartConnections();

                                //get Social lists
                                DomainController.getInstance().AddLobbyMessageToQueue(2); //friends
                                DomainController.getInstance().AddLobbyMessageToQueue(10); //pending friends
                                DomainController.getInstance().AddLobbyMessageToQueue(20);//get channels
                                DomainController.getInstance().AddLobbyMessageToQueue(50);//get users in home
                            }
                            break;
                        case 0:
                            GuiController.getInstance().SetDebugText("No account found, check your username and password.");
                            break;
                        case 1:
                            GuiController.getInstance().SetDebugText("Can't connect, your account is blocked.");
                            break;
                        case 2:
                            GuiController.getInstance().SetDebugText("Account already logged in!");
                            break;
                        default:
                            GuiController.getInstance().SetDebugText("Received unknown byte value from server: " + data[0]);
                            break;
                    }
                }
                else
                {
                    GuiController.getInstance().SetDebugText("Timed out when trying to connect to server.");
                }

                socket.Close();
                Thread.Sleep(1000);

            }
            catch (Exception e)
            {
                GuiController.getInstance().SetDebugText("Couldn't connect to Loginserver..." + e.ToString());
            }
        }