Esempio n. 1
0
        private void cmdStart_Click(object sender, EventArgs e)
        {
            if (textBox_playerNo.Text != "")
            {
                GlobalData.PLAYER_NUM = Convert.ToInt32(textBox_playerNo.Text);
            }

            if (textBox_round_trial.Text != "")
            {
                GlobalData.ROUND_NUM_trial = Convert.ToInt32(textBox_round_trial.Text);
            }
            if (textBox_round_1.Text != "")
            {
                GlobalData.ROUND_NUM_1 = Convert.ToInt32(textBox_round_1.Text);
            }
            if (textBox_round.Text != "")
            {
                GlobalData.ROUND_NUM = Convert.ToInt32(textBox_round.Text);
            }

            if (comboBox_IP.Text != "")
            {
                GlobalData.IP = comboBox_IP.Text;
            }

            if (comboBox_DB.Text != "")
            {
                GlobalData.strDatabaseIP = comboBox_DB.Text;
            }


            m_bListening = true;

            string      str         = System.Environment.CurrentDirectory;
            string      strHostName = Dns.GetHostName();
            IPHostEntry ipEntry     = Dns.GetHostEntry(strHostName);

            IPEndPoint iep = new IPEndPoint(ipEntry.AddressList[0], 5567);//ServerIp

            MyIP = System.Net.IPAddress.Parse(ipEntry.AddressList[0].ToString());

            picStatus.Load(str + "/Image/green.bmp");
            label4.Text      = "Server Listening";
            comboBox_IP.Text = ipEntry.AddressList[0].ToString();
            cmdStart.Text    = "Stop";

            try
            {
                IPAddress ipAd = IPAddress.Parse(GlobalData.IP);
                // use local m/c IP address, and
                // use the same in the client

                /* Initializes the Listener */
                TcpListener MySocketListener = new TcpListener(ipAd, 5567);

                /* Start Listeneting at the specified port */
                MySocketListener.Start();


                //MySocketListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //MySocketListener.Bind(iep);

                Console.WriteLine("The server is running at port 5567...");
                Console.WriteLine("The local End point is  :" +
                                  MySocketListener.LocalEndpoint);


                while (true)
                {
                    int flag = 0;
                    while (true)
                    {
                        Console.WriteLine("Waiting for a connection.....");
                        Socket newSocket = MySocketListener.AcceptSocket();

                        int iNum3 = 0;
                        for (int i = 0; i < SClient.Length; i++)
                        {
                            if (SClient[i] == null)
                            {
                                iNum3 = i;
                                break;
                            }
                        }
                        SClient[iNum3] = new SocketClient(newSocket);
                        Thread.Sleep(100);
                        txtInfor.Text = DateTime.Now.ToString() + ":  " + newSocket.RemoteEndPoint.ToString() + ": Connected\r\n" + txtInfor.Text;
                        Thread clientservice = new Thread(new ThreadStart(SClient[iNum3].ReceiveData));
                        clientservice.Start();

                        txtInfor.Refresh();


                        while (SClient[iNum3].strA == null)
                        {
                            Thread.Sleep(100);
                        }
                        string[] strA = SClient[iNum3].strA;


                        if (SClient[iNum3].strA.Length > 1)
                        {
                            string strID, strName;
                            int    iGame, iScenario, iRound, iRoute, iscore;
                            strID = strA[1];


                            //strName = strA[2];
                            iRound    = Convert.ToInt16(strA[2]);
                            iRoute    = Convert.ToInt16(strA[3]);
                            iGame     = Convert.ToInt16(strA[4]);
                            iscore    = Convert.ToInt16(strA[5]);
                            iScenario = Convert.ToInt16(strA[6]);

                            GlobalData.Current_Game     = iGame;
                            GlobalData.Current_Round    = iRound;
                            GlobalData.Current_Scenario = iScenario;


                            //if (strA[0] == "Player")
                            //{

                            //    store2PlayerDB(strA);

                            //    //ListViewItem Item = new ListViewItem();
                            //    //Item.Text = (listView1.Items.Count).ToString();
                            //    //Item.SubItems.Add(strID);
                            //    //listView1.Items.Add(Item);
                            //}
                            if (strA[0] == "Game")
                            {
                                store2GameDB(strA);
                                flag = flag + 1;
                            }
                            else //trA[0]=="Score"
                            {
                                //storeData2ScoreDB(strA);
                            }
                        }
                        else
                        {
                            QueryGameDB();
                        }


                        if (flag == GlobalData.PLAYER_NUM)
                        {
                            ASCIIEncoding asen = new ASCIIEncoding();

                            for (int m = 0; m < SClient.Length - 1; m++)
                            {
                                if (SClient[m] != null)
                                {
                                    SClient[m].Sock.Send(asen.GetBytes(GlobalData.strSend));//"The string was recieved by the server from port 5567."));
                                    Console.WriteLine("\nSent Acknowledgement");
                                    SClient[m] = null;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            break;
                        }

                        /* clean up */
                        //newSocket.Close();
                    }
                    //ASCIIEncoding asen = new ASCIIEncoding();
                    //Socket s = MySocketListener.AcceptSocket();
                    //s.Send(asen.GetBytes(GlobalData.strSend));//"The string was recieved by the server from port 5567."));
                    //Console.WriteLine("\nSent Acknowledgement");
                }
            }
            catch (Exception err)
            {
                Console.WriteLine("Error..... " + err.Message);//.StackTrace);
            }

            //myList.Stop();
        }
Esempio n. 2
0
        private void cmdStart_Click(object sender, EventArgs e)
        {
            if (textBox_playerNo.Text != "")
            {
                GlobalData.PLAYER_NUM = Convert.ToInt32(textBox_playerNo.Text);
            }

            if (textBox_round_trial.Text != "")
            {
                GlobalData.ROUND_NUM_trial = Convert.ToInt32(textBox_round_trial.Text);
            }
            if (textBox_round_1.Text != "")
            {
                GlobalData.ROUND_NUM_1 = Convert.ToInt32(textBox_round_1.Text);
            }
            if (textBox_round.Text != "")
            {
                GlobalData.ROUND_NUM = Convert.ToInt32(textBox_round.Text);
            }

            if (comboBox_IP.Text != "")
            {
                GlobalData.IP = comboBox_IP.Text;
            }

            if (comboBox_DB.Text != "")
            {
                GlobalData.strDatabaseIP = comboBox_DB.Text;
            }

            m_bListening = true;

            string str = System.Environment.CurrentDirectory;
            string strHostName = Dns.GetHostName();
            IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);

            IPEndPoint iep = new IPEndPoint(ipEntry.AddressList[0], 5567);//ServerIp
            MyIP = System.Net.IPAddress.Parse(ipEntry.AddressList[0].ToString());

            picStatus.Load(str + "/Image/green.bmp");
            label4.Text = "Server Listening";
            comboBox_IP.Text = ipEntry.AddressList[0].ToString();
            cmdStart.Text = "Stop";

            try
            {
                IPAddress ipAd = IPAddress.Parse(GlobalData.IP);
                // use local m/c IP address, and
                // use the same in the client

                /* Initializes the Listener */
                TcpListener MySocketListener = new TcpListener(ipAd, 5567);

                /* Start Listeneting at the specified port */
                MySocketListener.Start();

                //MySocketListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                //MySocketListener.Bind(iep);

                Console.WriteLine("The server is running at port 5567...");
                Console.WriteLine("The local End point is  :" +
                                  MySocketListener.LocalEndpoint);

                while(true)
                {

                    int flag = 0;
                    while (true)
                    {
                        Console.WriteLine("Waiting for a connection.....");
                        Socket newSocket = MySocketListener.AcceptSocket();

                        int iNum3 = 0;
                        for (int i = 0; i < SClient.Length; i++)
                        {
                            if (SClient[i] == null)
                            {
                                iNum3 = i;
                                break;
                            }
                        }
                        SClient[iNum3] = new SocketClient(newSocket);
                        Thread.Sleep(100);
                        txtInfor.Text = DateTime.Now.ToString() + ":  " + newSocket.RemoteEndPoint.ToString() + ": Connected\r\n" + txtInfor.Text;
                        Thread clientservice = new Thread(new ThreadStart(SClient[iNum3].ReceiveData));
                        clientservice.Start();

                        txtInfor.Refresh();

                        while (SClient[iNum3].strA == null)
                        {
                            Thread.Sleep(100);
                        }
                        string[] strA = SClient[iNum3].strA;

                        if (SClient[iNum3].strA.Length > 1)
                        {
                            string strID, strName;
                            int iGame, iScenario, iRound, iRoute,iscore;
                            strID = strA[1];

                                //strName = strA[2];
                                iRound = Convert.ToInt16(strA[2]);
                                iRoute = Convert.ToInt16(strA[3]);
                                iGame = Convert.ToInt16(strA[4]);
                                iscore = Convert.ToInt16(strA[5]);
                                iScenario = Convert.ToInt16(strA[6]);

                                GlobalData.Current_Game = iGame;
                                GlobalData.Current_Round = iRound;
                                GlobalData.Current_Scenario = iScenario;

                            //if (strA[0] == "Player")
                            //{

                            //    store2PlayerDB(strA);

                            //    //ListViewItem Item = new ListViewItem();
                            //    //Item.Text = (listView1.Items.Count).ToString();
                            //    //Item.SubItems.Add(strID);
                            //    //listView1.Items.Add(Item);
                            //}
                            if (strA[0] == "Game")
                            {
                                store2GameDB(strA);
                                flag = flag + 1;
                            }
                            else //trA[0]=="Score"
                            {
                                //storeData2ScoreDB(strA);
                            }
                        }
                        else
                        {
                            QueryGameDB();
                        }

                        if (flag == GlobalData.PLAYER_NUM)
                        {
                            ASCIIEncoding asen = new ASCIIEncoding();

                            for (int m = 0; m < SClient.Length - 1; m++)
                            {
                                if (SClient[m] != null)
                                {
                                    SClient[m].Sock.Send(asen.GetBytes(GlobalData.strSend));//"The string was recieved by the server from port 5567."));
                                    Console.WriteLine("\nSent Acknowledgement");
                                    SClient[m] = null;
                                }
                                else
                                {
                                    break;
                                }
                            }
                            break;
                        }

                        /* clean up */
                        //newSocket.Close();
                    }
                    //ASCIIEncoding asen = new ASCIIEncoding();
                    //Socket s = MySocketListener.AcceptSocket();
                    //s.Send(asen.GetBytes(GlobalData.strSend));//"The string was recieved by the server from port 5567."));
                    //Console.WriteLine("\nSent Acknowledgement");
                }
            }
            catch (Exception err)
            {
                Console.WriteLine("Error..... " + err.Message);//.StackTrace);
            }

                //myList.Stop();
        }