Ejemplo n.º 1
0
        public void _Client(int port, String ip_add)
        {
            new_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            new Thread(delegate()
            {
                try
                {
                    new_socket.Connect(new IPEndPoint(IPAddress.Parse(ip_add), port));
                    while (flag)
                    {
                        try
                        {
                            byte[] buffer = new byte[255];
                            int rec       = new_socket.Receive(buffer, 0, buffer.Length, 0);

                            Array.Resize(ref buffer, rec);
                            Invoke((MethodInvoker) delegate()
                            {
                                String btn = Encoding.Default.GetString(buffer);
                                if (btn == "new")
                                {
                                    new_Game msgBox = new new_Game();
                                    msgBox.ShowDialog();
                                    if (msgBox.clicked == "OK")
                                    {
                                        clearBtn();
                                        sentForNew(msgBox.clicked);
                                    }
                                    else
                                    {
                                        sentForNew(msgBox.clicked);
                                    }
                                    //clearBtn();
                                }
                                else if (btn == "A1" || btn == "A2" || btn == "A3" || btn == "B1" || btn == "B2" || btn == "B3" || btn == "C1" || btn == "C2" || btn == "C3")
                                {
                                    receiveData(btn);
                                    checkForWinner();
                                    playbtn_panel.Enabled = true;
                                }
                                else if (btn == "OK")
                                {
                                    clearBtn();
                                }
                                else if (btn == "Cancel")
                                {
                                    MessageBox.Show("Other player rejected...", "Reject", MessageBoxButtons.OK);
                                }
                                else
                                {
                                    textView.Visible = true;
                                    listBox2.Items.Add("Fri :" + btn);
                                    timer2.Start();
                                }
                            });
                        }

                        catch (Exception ex)
                        {
                            flag = false;
                            MessageBox.Show("Exit", "Close");
                        }
                    }
                }
                catch (Exception ex)
                {
                    flag = false;
                    MessageBox.Show("Server Time out...\n" + ex.Message);
                }
            }).Start();
        }
Ejemplo n.º 2
0
        public void _Servrer(int port)
        {
            try
            {
                new_server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, port);
                new_server.Bind(endPoint);
                new_server.Listen(100);
                clearBtn();
                new Thread(delegate()
                {
                    if (server_check)
                    {
                        new_client = new_server.Accept();

                        if (check)
                        {
                            check = false;
                        }
                        {
                            Invoke((MethodInvoker) delegate()
                            {
                                panel5.Visible = true;
                                panel6.Visible = false;
                            });
                        }
                        while (flag)
                        {
                            try
                            {
                                byte[] buffer = new byte[1024];
                                int received  = new_client.Receive(buffer, 0, buffer.Length, 0);
                                Array.Resize(ref buffer, received);

                                Invoke((MethodInvoker) delegate()
                                {
                                    String btnId = Encoding.ASCII.GetString(buffer);
                                    if (btnId == "new")
                                    {
                                        new_Game msgBox = new new_Game();
                                        msgBox.ShowDialog();
                                        if (msgBox.clicked == "OK")
                                        {
                                            clearBtn();
                                            sentForNew(msgBox.clicked);
                                        }
                                        else
                                        {
                                            sentForNew(msgBox.clicked);
                                        }
                                    }
                                    else if (btnId == "A1" || btnId == "A2" || btnId == "A3" || btnId == "B1" || btnId == "B2" || btnId == "B3" || btnId == "C1" || btnId == "C2" || btnId == "C3")
                                    {
                                        receiveData(btnId);
                                        checkForWinner();
                                        playbtn_panel.Enabled = true;
                                    }
                                    else if (btnId == "OK")
                                    {
                                        clearBtn();
                                    }
                                    else if (btnId == "Cancel")
                                    {
                                        MessageBox.Show("Other player rejected...", "Reject", MessageBoxButtons.OK);
                                    }
                                    else
                                    {
                                        textView.Visible = true;
                                        listBox2.Items.Add("Fri :" + btnId);
                                        timer2.Start();
                                    }
                                });
                            }
                            catch (Exception ex)
                            {
                                server_check = false;
                                flag         = false;
                                MessageBox.Show("Client is Closed.....");
                            }
                        }
                    }
                }).Start();
            }catch (Exception ex)
            {
                MessageBox.Show("Client is closed...");
            }
        }