Beispiel #1
0
        private void Communication()
        {
            // send to server
            try
            {
                TcpClient tcpclnt = new TcpClient();
                Console.WriteLine("Connecting.....");

                tcpclnt.Connect(GlobalData.IP, 5567);
                // use the ipaddress as in the server program

                Console.WriteLine("Connected");
                Console.Write("Enter the string to be transmitted : ");

                String str = "Game" + "$" + GlobalData.UserID + "$" + Convert.ToString(GlobalData.numRound_FiveLink) + "$" + Convert.ToString(GlobalData.numRoute_FiveLink) + "$" + GlobalData.NumGame.ToString() + "$" + GlobalData.Score_total.ToString() + "$" + GlobalData.NumScenario.ToString();
                Stream stm = tcpclnt.GetStream();

                ASCIIEncoding asen = new ASCIIEncoding();
                byte[]        ba   = asen.GetBytes(str);
                Console.WriteLine("Transmitting.....");

                stm.Write(ba, 0, ba.Length);

                //receive data from server once submit
                byte[] dat       = new byte[100];
                int    datLength = stm.Read(dat, 0, 100);
                string strDat    = Encoding.ASCII.GetString(dat, 0, datLength);
                //label_submit.Visible = true;
                //label_submit.Text = strDat;

                DialogResult dialog = MessageBox.Show(strDat, "Important Message");//, MessageBoxIcon.Asterisk);
                button_Submit.Enabled = false;

                //receive data from server
                byte[] data = new byte[100];
                int    receivedDataLength = stm.Read(data, 0, 100);
                GlobalData.strData = Encoding.ASCII.GetString(data, 0, receivedDataLength);

                for (int i = 0; i < receivedDataLength; i++)
                {
                    Console.Write(Convert.ToChar(data[i]));
                }

                //tcpclnt.Close();
            }

            catch (Exception err)
            {
                Console.WriteLine("Error..... " + err.StackTrace);
            }


            try
            {
                GlobalData.msg_received = GlobalData.strData.Split('$');

                GlobalData.TT[0] = GlobalData.msg_received[4];
                GlobalData.TT[1] = GlobalData.msg_received[5];
                GlobalData.TT[2] = GlobalData.msg_received[6];

                GlobalData.Path_shortest = GlobalData.msg_received[7];
                GlobalData.IsEquilibrium = Convert.ToBoolean(GlobalData.msg_received[8]);


                //if (GlobalData.NumGame == 2 || GlobalData.NumGame == 4)//(GlobalData.NumGame == 4 && GlobalData.numRoute_FiveLink != 3))
                //{
                GlobalData.Score_total -= Convert.ToInt32(GlobalData.TT[GlobalData.numRoute_FiveLink - 1]);
                label5_score_total.Text = GlobalData.Score_total.ToString();
                //}
                //else if (GlobalData.NumGame == 3 && GlobalData.numRoute_FiveLink == 3)
                //{
                //    GlobalData.Score_total = GlobalData.Score_total-Convert.ToInt32(GlobalData.TT[GlobalData.numRoute_FiveLink - 1])-GlobalData.FEE;
                //    label5_score_total.Text = GlobalData.Score_total.ToString();
                //}

                //if (GlobleData.numRound == GlobleData.TOTAL_ROUND)
                //{
                //    SendTotalScore();
                //}
            }
            catch
            {
            }


            ResultsForm_FiveLink result = new ResultsForm_FiveLink();

            result.Visible = true;
            //this.Visible = false;
            this.Close();
        }
        private void Communication()
        {
            // send to server
            try
            {
                TcpClient tcpclnt = new TcpClient();
                Console.WriteLine("Connecting.....");

                tcpclnt.Connect(GlobalData.IP, 5567);
                // use the ipaddress as in the server program

                Console.WriteLine("Connected");
                Console.Write("Enter the string to be transmitted : ");

                String str = "Game" + "$" + GlobalData.UserID + "$" + Convert.ToString(GlobalData.numRound_FiveLink) + "$" + Convert.ToString(GlobalData.numRoute_FiveLink) + "$" + GlobalData.NumGame.ToString() + "$" + GlobalData.Score_total.ToString() + "$" + GlobalData.NumScenario.ToString();
                Stream stm = tcpclnt.GetStream();

                ASCIIEncoding asen = new ASCIIEncoding();
                byte[] ba = asen.GetBytes(str);
                Console.WriteLine("Transmitting.....");

                stm.Write(ba, 0, ba.Length);

                //receive data from server once submit
                byte[] dat = new byte[100];
                int datLength = stm.Read(dat, 0, 100);
                string strDat = Encoding.ASCII.GetString(dat, 0, datLength);
                //label_submit.Visible = true;
                //label_submit.Text = strDat;

                DialogResult dialog = MessageBox.Show(strDat, "Important Message");//, MessageBoxIcon.Asterisk);
                button_Submit.Enabled = false;

                //receive data from server
                byte[] data = new byte[100];
                int receivedDataLength = stm.Read(data, 0, 100);
                GlobalData.strData = Encoding.ASCII.GetString(data, 0, receivedDataLength);

                for (int i = 0; i < receivedDataLength; i++)
                    Console.Write(Convert.ToChar(data[i]));

                //tcpclnt.Close();
            }

            catch (Exception err)
            {
                Console.WriteLine("Error..... " + err.StackTrace);
            }

            try
            {
                GlobalData.msg_received = GlobalData.strData.Split('$');

                GlobalData.TT[0] = GlobalData.msg_received[4];
                GlobalData.TT[1] = GlobalData.msg_received[5];
                GlobalData.TT[2] = GlobalData.msg_received[6];

                    GlobalData.Path_shortest = GlobalData.msg_received[7];
                    GlobalData.IsEquilibrium = Convert.ToBoolean(GlobalData.msg_received[8]);

                    //if (GlobalData.NumGame == 2 || GlobalData.NumGame == 4)//(GlobalData.NumGame == 4 && GlobalData.numRoute_FiveLink != 3))
                    //{
                        GlobalData.Score_total -= Convert.ToInt32(GlobalData.TT[GlobalData.numRoute_FiveLink - 1]);
                        label5_score_total.Text = GlobalData.Score_total.ToString();
                    //}
                    //else if (GlobalData.NumGame == 3 && GlobalData.numRoute_FiveLink == 3)
                    //{
                    //    GlobalData.Score_total = GlobalData.Score_total-Convert.ToInt32(GlobalData.TT[GlobalData.numRoute_FiveLink - 1])-GlobalData.FEE;
                    //    label5_score_total.Text = GlobalData.Score_total.ToString();
                    //}

                //if (GlobleData.numRound == GlobleData.TOTAL_ROUND)
                //{
                //    SendTotalScore();
                //}

            }
            catch
            {
            }

            ResultsForm_FiveLink result = new ResultsForm_FiveLink();
            result.Visible = true;
            //this.Visible = false;
            this.Close();
        }