Пример #1
0
        private void createRoomB_Click(object sender, EventArgs e)
        {
            string createRoomMsg = "213" + roomName.Text.Length.ToString().PadLeft(2) + roomName.Text +
                                   numOfPB.Text.ToString().PadLeft(1) + numOfQB.Text.ToString().PadLeft(2) + timeForQB.Text.ToString().PadLeft(2);
            NetworkStream serverStream = clientSocket.GetStream();

            byte[] outStream = System.Text.Encoding.ASCII.GetBytes(createRoomMsg);
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            byte[] inStream = new byte[4];
            serverStream.Read(inStream, 0, 4);
            string returndata = System.Text.Encoding.ASCII.GetString(inStream);

            if (returndata == "1140")
            {
                Form waitForGameForm = new WaitForGame(clientSocket, parent, roomName.Text, true, int.Parse(numOfQB.Text), int.Parse(timeForQB.Text), int.Parse(numOfPB.Text));
                waitForGameForm.Show();
                roomCreated = true;
                this.Close();
            }
            else
            {
                msgToUser.Visible = true;
            }
        }
Пример #2
0
        private void joinB_Click(object sender, EventArgs e)
        {
            if (roomList.SelectedItem == null)
            {
                return;
            }
            string        selectedRoomId = roomList.SelectedItem.ToString().Substring(9, 4);
            string        joinMsg        = "209" + selectedRoomId;
            NetworkStream serverStream   = clientSocket.GetStream();

            byte[] outStream = System.Text.Encoding.ASCII.GetBytes(joinMsg);
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            byte[] inStream = new byte[4];
            serverStream.Read(inStream, 0, 4);
            string returndata = System.Text.Encoding.ASCII.GetString(inStream);

            switch (returndata)
            {
            case "1100":
                //success
                joinedToRoom = true;
                this.Close();
                inStream = new byte[4];     //2 byte of questionsNumber and 2 byte of questionTimeInSec
                serverStream.Read(inStream, 0, 4);
                returndata = System.Text.Encoding.ASCII.GetString(inStream);
                Form waitForGameForm = new WaitForGame(clientSocket, parent,
                                                       roomList.SelectedItem.ToString().Substring(27), false,
                                                       int.Parse(returndata.Substring(0, 2)), int.Parse(returndata.Substring(2, 2)));
                waitForGameForm.Show();
                break;

            case "1101":
                // failed - room is full
                msgToUser.Text = "failed - room is full";
                break;

            case "1102":
                //failed - room not exist or other reason
                msgToUser.Text = "failed - room not exist or other reason";
                break;

            default:
                //unknown
                msgToUser.Text = "Unknown code";
                break;
            }
        }