Esempio n. 1
0
        //Broadcast the message typed by the user to everyone
        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                //Fill the info for the message to be send
                Data msgToSend = new Data();

                msgToSend.strName = strName;
                msgToSend.strMessage = txtMessage.Text;
                msgToSend.cmdCommand = Command.Message;
                if (lstChatters.SelectedItem != null)
                    msgToSend.clientName = lstChatters.SelectedItem.ToString();
                else
                    msgToSend.clientName = null;

                byte [] byteData = msgToSend.ToByte();

                //Send it to the server
                clientSocket.BeginSend (byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null);

                msgToSend.strMessage = null;
            }
            catch (Exception)
            {
                MessageBox.Show("Unable to send message to the server.", "SGSclientTCP: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;

            this.Text = strName;
            lstChatters.Items.Add("Online users: \n\n");
            btnDone.Hide();

            //The user has logged into the system so we now request the server to send
            //the names of all users who are in the chat room
            Data msgToSend = new Data();

            msgToSend.cmdCommand = Command.List;
            msgToSend.strName    = strName;
            msgToSend.strMessage = null;

            byteData = msgToSend.ToByte();

            clientSocket.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer,
                                     new AsyncCallback(OnSend), null);

            byteData = new byte[1024];
            //Start listening to the data asynchronously
            clientSocket.BeginReceiveFrom(byteData,
                                          0, byteData.Length,
                                          SocketFlags.None,
                                          ref epServer,
                                          new AsyncCallback(OnReceive),
                                          null);
        }
Esempio n. 3
0
        private void btnOK_Click(object sender, EventArgs e)
        {
            strName = txtName.Text;
            try
            {
                //Using UDP sockets
                clientSocket = new Socket(AddressFamily.InterNetwork,
                    SocketType.Dgram, ProtocolType.Udp);

                //IP address of the server machine
                IPAddress ipAddress = IPAddress.Parse(txtServerIP.Text);
                //Server is listening on port 1000
                IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 1000);

                epServer = (EndPoint)ipEndPoint;

                Data msgToSend = new Data ();
                msgToSend.cmdCommand = Command.Login;
                msgToSend.strMessage = null;
                msgToSend.strName = strName;

                byte[] byteData = msgToSend.ToByte();

                //Login to the server
                clientSocket.BeginSendTo(byteData, 0, byteData.Length,
                    SocketFlags.None, epServer, new AsyncCallback(OnSend), null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SGSclient",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void SGSClient_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to leave the chat room?", "SGSclient: " + strName,
                                MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.No)
            {
                e.Cancel = true;
                return;
            }

            try
            {
                //Send a message to logout of the server
                Data msgToSend = new Data();
                msgToSend.cmdCommand = Command.Logout;
                msgToSend.strName    = strName;
                msgToSend.strMessage = null;

                byte[] b = msgToSend.ToByte();
                clientSocket.SendTo(b, 0, b.Length, SocketFlags.None, epServer);
                clientSocket.Close();
            }
            catch (ObjectDisposedException)
            { }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SGSclient: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        //Broadcast the message typed by the user to everyone
        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                //Fill the info for the message to be send
                Data msgToSend = new Data();

                msgToSend.strName    = strName;
                msgToSend.strMessage = txtMessage.Text;
                msgToSend.cmdCommand = Command.Message;

                byte[] byteData = msgToSend.ToByte();

                //Send it to the server
                clientSocket.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer,
                                         new AsyncCallback(OnSend), null);

                txtMessage.Text = null;
            }
            catch (Exception)
            {
                MessageBox.Show("Unable to send message to the server.", "UDPClient: " + strName,
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 6
0
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "ConcordChat: " + strName;

            //The user has logged into the system so we now request the server to send
            //the names of all users who are in the chat room
            Data msgToSend = new Data();

            msgToSend.cmdCommand = Command.List;
            msgToSend.strName    = strName;
            msgToSend.strMessage = null;

            byteData = msgToSend.ToByte();

            clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null);

            byteData = new byte[1024];
            //Start listening to the data asynchronously
            clientSocket.BeginReceive(byteData,
                                      0,
                                      byteData.Length,
                                      SocketFlags.None,
                                      new AsyncCallback(OnReceive),
                                      null);
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            strName = txtName.Text;
            try
            {
                //Using UDP sockets
                clientSocket = new Socket(AddressFamily.InterNetwork,
                                          SocketType.Dgram, ProtocolType.Udp);

                //IP address of the server machine
                IPAddress ipAddress = IPAddress.Parse(txtServerIP.Text);
                //Server is listening on port 1000
                IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 1000);

                epServer = (EndPoint)ipEndPoint;

                Data msgToSend = new Data();
                msgToSend.cmdCommand = Command.Login;
                msgToSend.strMessage = null;
                msgToSend.strName    = strName;

                byte[] byteData = msgToSend.ToByte();

                //Login to the server
                clientSocket.BeginSendTo(byteData, 0, byteData.Length,
                                         SocketFlags.None, epServer, new AsyncCallback(OnSend), null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SGSclient",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 8
0
        private void btnNewGame_Click(object sender, EventArgs e)
        {
            Data msgToSend = new Data();

            msgToSend.strName    = strName;
            msgToSend.strMessage = null;
            msgToSend.cmdCommand = Command.NewGame;
            byte[] byteData = msgToSend.ToByte();
            //Send it to the server
            clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
            txtMessage.Text = null;
        }
Esempio n. 9
0
 private void OnConnect(IAsyncResult ar)
 {
     try
     {
         clientSocket.EndConnect(ar);
         //We are connected so we login into the server
         Data msgToSend = new Data();
         msgToSend.cmdCommand = Command.Login;
         msgToSend.strName    = txtName.Text;
         msgToSend.strMessage = null;
         byte[] b = msgToSend.ToByte();
         //Send the message to the server
         clientSocket.BeginSend(b, 0, b.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "SGSclient", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Esempio n. 10
0
 private void OnConnect(IAsyncResult ar)
 {
     try
     {
         clientSocket.EndConnect(ar);
         //We are connected so we login into the server
         Data msgToSend = new Data ();
         msgToSend.cmdCommand = Command.Login;
         msgToSend.strName = txtName.Text;
         msgToSend.strMessage = null;
         byte[] b = msgToSend.ToByte ();
         //Send the message to the server
         clientSocket.BeginSend(b, 0, b.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "SGSclient", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Esempio n. 11
0
        private void Form1_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;

            this.Text = "SGSclient: " + strName;

            //client đăng nhập => yêu cầu server lấy tên lên chatroom
            Data msgToSend = new Data();

            msgToSend.cmdCommand = Command.List;
            msgToSend.strName    = strName;
            msgToSend.strMessage = null;

            byteData = msgToSend.ToByte();

            clientSocket.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer,
                                     new AsyncCallback(OnSend), null);

            byteData = new byte[1024];
            clientSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epServer, new AsyncCallback(OnReceive), null);
        }
Esempio n. 12
0
        //Broadcast the message typed by the user to everyone
        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                //Fill the info for the message to be send
                Data msgToSend = new Data();

                msgToSend.textsend = txtMessage.Text;

                byte [] byteData = msgToSend.ToByte();

                //Send it to the server
                clientSocket.BeginSend (byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
                txtMessage.Clear();
                //msgToSend.textsend = null;
            }
            catch (Exception)
            {
                MessageBox.Show("Unable to send message to the server.", "SGSclientTCP: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 13
0
        public void Send(string msg)
        {
            try
            {
                //Fill the info for the message to be send
                Data msgToSend = new Data();

                msgToSend.strName    = strName;
                msgToSend.strMessage = msg;
                msgToSend.cmdCommand = Command.Message;

                byte[] byteData = msgToSend.ToByte();

                //Send it to the server
                clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
            }
            catch (Exception)
            {
                MessageBox.Show("Unable to send message to the server.", "SGSclientTCP: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 14
0
        public void Close()
        {
            try
            {
                //Send a message to logout of the server
                Data msgToSend = new Data();
                msgToSend.cmdCommand = Command.Logout;
                msgToSend.strName    = strName;
                msgToSend.strMessage = null;

                byte[] b = msgToSend.ToByte();
                clientSocket.Send(b, 0, b.Length, SocketFlags.None);
                clientSocket.Close();
            }
            catch (ObjectDisposedException)
            { }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SGSclientTCP: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 15
0
        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "SGSclientTCP: " + strName;

            //The user has logged into the system so we now request the server to send
            //the names of all users who are in the chat room
            Data msgToSend = new Data ();
            msgToSend.textsend = strName;

            byteData = msgToSend.ToByte();

            clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null);

            byteData = new byte[1024];
            //Start listening to the data asynchronously
            clientSocket.BeginReceive(byteData,
                                       0,
                                       byteData.Length,
                                       SocketFlags.None,
                                       new AsyncCallback(OnReceive),
                                       null);
        }
Esempio n. 16
0
        private void butSendXY_Click(object sender, EventArgs e)
        {
            try
            {
                //Fill the info for the message to be send
                Data msgToSend = new Data();
                msgToSend.strName   = players[0].name;
                msgToSend.posX      = Convert.ToByte(players[0].position.X);
                msgToSend.posY      = Convert.ToByte(players[0].position.Y);
                msgToSend.byteColor = Convert.ToByte(comboBox1.SelectedIndex);
                //msgToSend.strMessage = players[0].position.X.ToString() + " " + players[0].position.Y.ToString() + " " + players[0].color.ToString();
                msgToSend.cmdCommand = Command.Draw;

                byte[] byteData = msgToSend.ToByte();

                //Send it to the server
                clientSocket.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer, new AsyncCallback(OnSend), null);
            }
            catch (Exception)
            {
                MessageBox.Show("Unable to send message to the server.", "SGSclientUDP: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 17
0
        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                Data msgToSend = new Data();

                msgToSend.strName = strEmri;
                Console.WriteLine(txtMessage.Text);
                msgToSend.strMessage = myRSA.Encrypt(txtMessage.Text);
                msgToSend.cmdCommand = Command.Message;


                byte[] byteData = msgToSend.ToByte();

                clientSocketa.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer, new AsyncCallback(OnSend), null);

                txtMessage.Text = null;
            }
            catch (Exception)
            {
                MessageBox.Show("Mesazhi nuk u dergua");
            }
        }
Esempio n. 18
0
        public void List()
        {
            //The user has logged into the system so we now request the server to send
            //the names of all users who are in the chat room
            Data msgToSend = new Data();

            msgToSend.cmdCommand = Command.List;
            msgToSend.strName    = strName;
            msgToSend.strMessage = null;

            byteData = msgToSend.ToByte();

            clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null);

            byteData = new byte[1024];
            //Start listening to the data asynchronously
            clientSocket.BeginReceive(byteData,
                                      0,
                                      byteData.Length,
                                      SocketFlags.None,
                                      new AsyncCallback(OnReceive),
                                      null);
        }
Esempio n. 19
0
        private void connectAudio()
        {
            audioSocket.Connect(IPAddress.Parse("127.0.0.1"), 1000);
            Data conData = new Data();

            conData.cmdCommand = Command.AudioConnect;
            conData.strMessage = null;
            conData.strName    = strName;

            byte[] byteData = conData.ToByte();
            while (true)
            {
                audioSocket.Send(byteData, byteData.Length);
                IPEndPoint ep          = null;
                byte[]     receiveData = audioSocket.Receive(ref ep);
                Data       data        = new Data(receiveData);
                if (data.cmdCommand == Command.AudioConnectSucceed)
                {
                    break;
                }
            }
            byte[] recData = new byte[1024];
            audioSocket.BeginReceive(new AsyncCallback(OnAudioReceive), null);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Random r   = new Random();
            int    num = r.Next(1000, 60000);

            timer1.Interval = num;
            textBox1.Invoke((MethodInvoker) delegate()
            {
                textBox1.AppendText("Interval = " + num.ToString() + Environment.NewLine);
            });

            timer1.Enabled = true;

            this.Text = "SGSclientTCP: " + strName;

            //The user has logged into the system so we now request the server to send
            //the names of all users who are in the chat room
            Data msgToSend = new Data();

            msgToSend.cmdCommand = Command.List;
            msgToSend.strName    = strName;
            msgToSend.strMessage = null;

            byteData = msgToSend.ToByte();

            clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null);

            byteData = new byte[1024];
            //Start listening to the data asynchronously
            clientSocket.BeginReceive(byteData,
                                      0,
                                      byteData.Length,
                                      SocketFlags.None,
                                      new AsyncCallback(OnReceive),
                                      null);
        }
Esempio n. 21
0
 private void btnNewGame_Click(object sender, EventArgs e)
 {
     
     Data msgToSend = new Data();
     msgToSend.strName = strName;
     msgToSend.strMessage = null;
     msgToSend.cmdCommand = Command.NewGame;
     byte[] byteData = msgToSend.ToByte();
     //Send it to the server
     clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
     txtMessage.Text = null;
     
 }
Esempio n. 22
0
        private void SGSClient_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (boot == false) 
            if (MessageBox.Show("Are you sure you want to leave the chat room?", "SGSclient: " + strName,
                MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.No)
            {
                e.Cancel = true;
                return;
            }

            try
            {
                //Send a message to logout of the server
                Data msgToSend = new Data ();
                msgToSend.cmdCommand = Command.Logout;
                msgToSend.strName = strName;
                msgToSend.strMessage = null;

                byte[] b = msgToSend.ToByte ();
                clientSocket.Send(b, 0, b.Length, SocketFlags.None);
                clientSocket.Close();
            }
            catch (ObjectDisposedException)
            { }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SGSclientTCP: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 23
0
        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                clientSocket.EndReceive(ar);
                Data msgReceived = new Data(byteData);
                //Accordingly process the message received
                switch (msgReceived.cmdCommand)
                {
                    #region Commands Login, Logout, Message
                    case Command.Login:
                        lstChatters.Items.Add(msgReceived.strName);
                        break;
                    case Command.Logout:
                        lstChatters.Items.Remove(msgReceived.strName);
                        break;
                    case Command.Message:
                        break;
                    #endregion
                    #region Command.BeginGame
                    case Command.BeginGame:
                        if (msgReceived.strMessage == "first")
                        {
                            this.txtChatBox.Text += "Server: You get to move first (Black). \r\n";
                            
                            myColor = "Black";
                        }
                        else
                        {
                            ChangeTurns();
                            this.txtChatBox.Text += "Server: You will move second (Red). \r\n";
                            myColor = "Red";
                        }
                        Data msgToSend = new Data();               
                msgToSend.strName = strName;
                msgToSend.strMessage = txtMessage.Text;
                msgToSend.cmdCommand = Command.List;
                byte [] stuff = msgToSend.ToByte();
                //Send it to the server
                clientSocket.BeginSend(stuff, 0, stuff.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
                        break;
                    #endregion
                    #region Command.NewGame
                    case Command.NewGame:
                        if (myColor == "Black")
                        {
                            this.txtChatBox.Text += "You will move first for the new game. \r\n";
                            if (button2.Enabled == false)
                                DisableMoveButtons();
                            currentTurn = true;
                        }
                        else
                        {
                            this.txtChatBox.Text += "You willl move second for the new game. \r\n";
                            if (button2.Enabled == true)
                                DisableMoveButtons();
                            currentTurn = false;
                        }
                        requestNewGame();
                        break;

                    #endregion
                    #region Command.SendMove
                    case Command.SendMove:
                        MoveToMake = msgReceived.strMessage.Split(',');
                        colorDrop = MoveToMake[0];
                        _timer = new System.Timers.Timer();
                        _timer.Interval = 100;
                        _timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
                        _timer.Enabled = true;
                        _timer.Start();
                        txtChatBox.Text += MoveToMake[0] + " Made a move at [" + MoveToMake[1] + "," + MoveToMake[2] + "]." + "\r\n";
                        ChangeTurns();
                        break;
                    #endregion
                    #region Command.Win
                    case Command.Win:
                        MoveToMake = msgReceived.strMessage.Split(',');
                        if (MoveToMake[0] == "Black")
                        {
                            zonelist[Convert.ToInt32(MoveToMake[1]), Convert.ToInt32(MoveToMake[2])].FillColor = Color.Black;
                        }
                        else
                        {
                            zonelist[Convert.ToInt32(MoveToMake[1]), Convert.ToInt32(MoveToMake[2])].FillColor = Color.Red;
                        }
                        txtChatBox.Text += MoveToMake[0] + " Made a Winning move at [" + MoveToMake[1] + "," + MoveToMake[2] + "]." + "\r\n";
                        MessageBox.Show(MoveToMake[0] + " Made a Winning move at[" + MoveToMake[1] + "," + MoveToMake[2] + "]!");
                        if (button2.Enabled == true)
                            ChangeTurns();
                        txtChatBox.Text += "Press the 'New Game' button to initiate a new game. \r\n";
                        btnNewGame.Visible = true;
                        WinningColors();
                        break;
                    #endregion
                    #region Command.List
                    case Command.List:
                        lstChatters.Items.Clear();
                        namehold = msgReceived.strMessage.Split('*');
                        foreach (string s in namehold)
                        {
                            lstChatters.Items.Add(s);
                        }
                        break;
                    #endregion
                }
                #region Begin receive, happens every time on receive happens
                if (msgReceived.strMessage != null && msgReceived.cmdCommand != Command.List && msgReceived.cmdCommand != Command.SendMove && msgReceived.cmdCommand != Command.BeginGame)
                    txtChatBox.Text += msgReceived.strMessage + "\r\n";
                byteData = new byte[1024];
                clientSocket.BeginReceive(byteData,
                                          0,
                                          byteData.Length,
                                          SocketFlags.None,
                                          new AsyncCallback(OnReceive),
                                          null);

            }
            catch (ObjectDisposedException)
            { }
            catch (System.ArgumentNullException)
            {
                MessageBox.Show("There are already two players in this game.");
                boot = true;
                Application.Exit();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SGSclientTCP: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 24
0
        private void OnReceive(IAsyncResult ar)
        {
            //try
            {
                if (!SocketConnected(_clientSocket))
                {
                    MessageBox.Show("Разорвано соединение с сервером!");
                    return;
                }
                _clientSocket.EndReceive(ar);
                Data msgToSend = new Data();
                Data msgReceived = new Data(byteData);
                if (msgReceived.cmdCommand == Command.PositionNext)
                    _clientSocket.BeginReceive(byteData,
                                          0,
                                          byteData.Length,
                                          SocketFlags.None,
                                          new AsyncCallback(OnReceive),
                                          null);
                //Accordingly process the message received
                switch (msgReceived.cmdCommand)
                {
                    case Command.Login:
                        if (msgReceived.strName != null)
                        {
                            lstChatters.Items.Add(msgReceived.strName);
                            txtChatBox.Text += msgReceived.strMessage + "\r\n";
                        }
                        break;

                    case Command.Logout:
                        lstChatters.Items.Remove(msgReceived.strName);
                        txtChatBox.Text += msgReceived.strMessage + "\r\n";
                        break;

                    case Command.Message:
                        txtChatBox.Text += msgReceived.strMessage + "\r\n";
                        break;

                    case Command.PositionNext:
                        mut.WaitOne();

                        using (Graphics g = Graphics.FromImage(pictureBox1.Image))
                        {
                            //g.Clear(Color);
                            g.DrawLine(new Pen(msgReceived.clientColor), msgReceived.pnt1, msgReceived.pnt2);
                        }
                        //_bitmap.SetPixel(X, Y, msgReceived.clientColor);
                        pictureBox1.Refresh();

                        mut.ReleaseMutex();

                        break;

                    case Command.BitmapInfoRequest:
                        ImageConverter icReq = new ImageConverter();
                        byte[] new_b = (byte[])icReq.ConvertTo(_bitmap, typeof(byte[]));

                        msgToSend.cmdCommand = Command.BitmapInfoResult;
                        msgToSend.strName = msgReceived.strName;
                        msgToSend.strMessage = "L " + new_b.Length.ToString();

                        byteData = msgToSend.ToByte();
                        _clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null);

                        System.Threading.Thread.Sleep(100);

                        msgToSend.bmp = new_b;
                        msgToSend.strMessage = "B";

                        byteData = msgToSend.ToByte();
                        _clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
                        break;

                    case Command.BitmapInfoResult:
                        if (msgReceived.strMessage[0] == 'L')
                            byteData = new byte[System.Convert.ToInt32(msgReceived.strMessage.Split(' ')[1])+50];
                        else
                        {
                            ImageConverter icRes = new ImageConverter();
                            Image img = (Image)icRes.ConvertFrom(msgReceived.bmp);
                            _bitmap = new Bitmap(img);
                            pictureBox1.Image = _bitmap;
                            pictureBox1.Refresh();
                        }
                        break;

                    case Command.ClientList:
                        lstChatters.Items.AddRange(msgReceived.strMessage.Split('*'));
                        lstChatters.Items.RemoveAt(lstChatters.Items.Count - 1);
                        txtChatBox.Text += "<<<" + strName + " has joined the room>>>\r\n";

                        msgToSend = new Data();
                        msgToSend.cmdCommand = Command.BitmapInfoRequest;
                        msgToSend.strName = strName;
                        msgToSend.strMessage = null;

                        byte[] byteDataLocal = new byte[1024];
                        byteDataLocal = msgToSend.ToByte();

                        _clientSocket.BeginSend(byteDataLocal, 0, byteDataLocal.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
                        break;

                    case Command.TestPackageSend:
                        _testNum++;
                        break;

                    case Command.TestPackageRecieve:
                        msgToSend = new Data();
                        msgToSend.cmdCommand = Command.TestPackageRecieve;
                        msgToSend.strName = strName;
                        msgToSend.strMessage = _testNum.ToString();

                        _testNum = 0;

                        _clientSocket.BeginSend(msgToSend.ToByte(), 0, msgToSend.ToByte().Length, SocketFlags.None, new AsyncCallback(OnSend), null);
                        break;
                }

                //if (msgReceived.strMessage != null && msgReceived.cmdCommand != Command.ClientList && msgReceived.cmdCommand != Command.PositionNext && msgReceived.cmdCommand != Command.BitmapInfoRequest && msgReceived.cmdCommand != Command.BitmapInfoResult)

                //byteData = new byte[2048];
                if (msgReceived.cmdCommand != Command.PositionNext)
                    _clientSocket.BeginReceive(byteData,
                                          0,
                                          byteData.Length,
                                          SocketFlags.None,
                                          new AsyncCallback(OnReceive),
                                          null);

            }
            /*catch (ObjectDisposedException)
            { }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SGSclientTCP: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }*/
        }
Esempio n. 25
0
        //Broadcast the message typed by the user to everyone
        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                //Fill the info for the message to be send
                Data msgToSend = new Data();
                var  ten       = "";
                msgToSend.strName = strName;
                if (txtMessage.Text == "1")
                {
                    txtMessage.Text = "one";
                }
                else if (txtMessage.Text == "2")
                {
                    txtMessage.Text = "two";
                }
                else if (txtMessage.Text == "3")
                {
                    txtMessage.Text = "three";
                }
                else if (txtMessage.Text == "4")
                {
                    txtMessage.Text = "four";
                }
                else if (txtMessage.Text == "5")
                {
                    txtMessage.Text = "five";
                }
                else if (txtMessage.Text == "6")
                {
                    txtMessage.Text = "six";
                }
                else if (txtMessage.Text == "7")
                {
                    txtMessage.Text = "seven";
                }
                else if (txtMessage.Text == "8")
                {
                    txtMessage.Text = "eight";
                }
                else if (txtMessage.Text == "9")
                {
                    txtMessage.Text = "nine";
                }
                else if (txtMessage.Text == "END")
                {
                    ten                  = txtMessage.Text;
                    txtMessage.Text      = "Good bye !!!";
                    msgToSend.strMessage = txtMessage.Text;
                    msgToSend.cmdCommand = Command.Message;

                    byte[] byteData1 = msgToSend.ToByte();

                    //Send it to the server
                    clientSocket.BeginSendTo(byteData1, 0, byteData1.Length, SocketFlags.None, epServer, new AsyncCallback(OnSend), null);

                    txtMessage.Text = null;

                    this.Close();
                }
                if (ten != "END")
                {
                    msgToSend.strMessage = txtMessage.Text;
                    msgToSend.cmdCommand = Command.Message;

                    byte[] byteData = msgToSend.ToByte();

                    //Send it to the server
                    clientSocket.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer, new AsyncCallback(OnSend), null);
                    txtMessage.Text = null;
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Khong the gui tin nhan toi server.", "SGSclientUDP: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 26
0
        private void SGSClient_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (SocketConnected(_clientSocket))
                {
                    //Send a message to logout of the server
                    Data msgToSend = new Data();
                    msgToSend.cmdCommand = Command.Logout;
                    msgToSend.strName = strName;
                    msgToSend.strMessage = null;

                    byte[] b = msgToSend.ToByte();
                    _clientSocket.Send(b, 0, b.Length, SocketFlags.None);
                }
            }
            catch (ObjectDisposedException)
            { }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SGSclientTCP: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 27
0
        private void Form1_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;

            this.Text = "SGSclient: " + strName;

            //The user has logged into the system so we now request the server to send
            //the names of all users who are in the chat room
            Data msgToSend = new Data ();
            msgToSend.cmdCommand = Command.List;

            msgToSend.strName = strName;
            msgToSend.strMessage = null;

            byteData = msgToSend.ToByte();

            clientSocket.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer,
                new AsyncCallback(OnSend), null);

            byteData = new byte[1024];
            //Start listening to the data asynchronously
            clientSocket.BeginReceiveFrom (byteData,
                                       0, byteData.Length,
                                       SocketFlags.None,
                                       ref epServer,
                                       new AsyncCallback(OnReceive),
                                       null);
        }
Esempio n. 28
0
        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                clientSocket.EndReceive(ar);
                Data msgReceived = new Data(byteData);
                //Accordingly process the message received
                switch (msgReceived.cmdCommand)
                {
                    #region Commands Login, Logout, Message
                case Command.Login:
                    lstChatters.Items.Add(msgReceived.strName);
                    break;

                case Command.Logout:
                    lstChatters.Items.Remove(msgReceived.strName);
                    break;

                case Command.Message:
                    break;

                    #endregion
                    #region Command.BeginGame
                case Command.BeginGame:
                    if (msgReceived.strMessage == "first")
                    {
                        this.txtChatBox.Text += "Server: You get to move first (Black). \r\n";

                        myColor = "Black";
                    }
                    else
                    {
                        ChangeTurns();
                        this.txtChatBox.Text += "Server: You will move second (Red). \r\n";
                        myColor = "Red";
                    }
                    Data msgToSend = new Data();
                    msgToSend.strName    = strName;
                    msgToSend.strMessage = txtMessage.Text;
                    msgToSend.cmdCommand = Command.List;
                    byte [] stuff = msgToSend.ToByte();
                    //Send it to the server
                    clientSocket.BeginSend(stuff, 0, stuff.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
                    break;

                    #endregion
                    #region Command.NewGame
                case Command.NewGame:
                    if (myColor == "Black")
                    {
                        this.txtChatBox.Text += "You will move first for the new game. \r\n";
                        if (button2.Enabled == false)
                        {
                            DisableMoveButtons();
                        }
                        currentTurn = true;
                    }
                    else
                    {
                        this.txtChatBox.Text += "You willl move second for the new game. \r\n";
                        if (button2.Enabled == true)
                        {
                            DisableMoveButtons();
                        }
                        currentTurn = false;
                    }
                    requestNewGame();
                    break;

                    #endregion
                    #region Command.SendMove
                case Command.SendMove:
                    MoveToMake      = msgReceived.strMessage.Split(',');
                    colorDrop       = MoveToMake[0];
                    _timer          = new System.Timers.Timer();
                    _timer.Interval = 100;
                    _timer.Elapsed += new System.Timers.ElapsedEventHandler(_timer_Elapsed);
                    _timer.Enabled  = true;
                    _timer.Start();
                    txtChatBox.Text += MoveToMake[0] + " Made a move at [" + MoveToMake[1] + "," + MoveToMake[2] + "]." + "\r\n";
                    ChangeTurns();
                    break;

                    #endregion
                    #region Command.Win
                case Command.Win:
                    MoveToMake = msgReceived.strMessage.Split(',');
                    if (MoveToMake[0] == "Black")
                    {
                        zonelist[Convert.ToInt32(MoveToMake[1]), Convert.ToInt32(MoveToMake[2])].FillColor = Color.Black;
                    }
                    else
                    {
                        zonelist[Convert.ToInt32(MoveToMake[1]), Convert.ToInt32(MoveToMake[2])].FillColor = Color.Red;
                    }
                    txtChatBox.Text += MoveToMake[0] + " Made a Winning move at [" + MoveToMake[1] + "," + MoveToMake[2] + "]." + "\r\n";
                    MessageBox.Show(MoveToMake[0] + " Made a Winning move at[" + MoveToMake[1] + "," + MoveToMake[2] + "]!");
                    if (button2.Enabled == true)
                    {
                        ChangeTurns();
                    }
                    txtChatBox.Text   += "Press the 'New Game' button to initiate a new game. \r\n";
                    btnNewGame.Visible = true;
                    WinningColors();
                    break;

                    #endregion
                    #region Command.List
                case Command.List:
                    lstChatters.Items.Clear();
                    namehold = msgReceived.strMessage.Split('*');
                    foreach (string s in namehold)
                    {
                        lstChatters.Items.Add(s);
                    }
                    break;
                    #endregion
                }
                #region Begin receive, happens every time on receive happens
                if (msgReceived.strMessage != null && msgReceived.cmdCommand != Command.List && msgReceived.cmdCommand != Command.SendMove && msgReceived.cmdCommand != Command.BeginGame)
                {
                    txtChatBox.Text += msgReceived.strMessage + "\r\n";
                }
                byteData = new byte[1024];
                clientSocket.BeginReceive(byteData,
                                          0,
                                          byteData.Length,
                                          SocketFlags.None,
                                          new AsyncCallback(OnReceive),
                                          null);
            }
            catch (ObjectDisposedException)
            { }
            catch (System.ArgumentNullException)
            {
                MessageBox.Show("There are already two players in this game.");
                boot = true;
                Application.Exit();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SGSclientTCP: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 29
0
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && _drawing)
            {
                try
                {
                    if (e.X > 0 && e.Y > 0 && e.X < pictureBox1.Width && e.Y < pictureBox1.Height && _numOfPnt == SENDNUM)
                    {
                        _numOfPnt = 0;
                        //Fill the info for the message to be send
                        Data msgToSend = new Data();

                        msgToSend.strName = strName;
                        msgToSend.pnt2 = new Point(e.X, e.Y);
                        msgToSend.cmdCommand = Command.PositionNext;
                        msgToSend.clientColor = clientClr;

                        byte[] byteData = msgToSend.ToByte();

                        //Send it to the server
                        _clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null);
                    }
                    _numOfPnt++;
                }
                catch (Exception)
                {
                    MessageBox.Show("Unable to send message to the server.", "SGSclientTCP: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
Esempio n. 30
0
        private void butSendXY_Click(object sender, EventArgs e)
        {
            try
            {
                //Fill the info for the message to be send
                Data msgToSend = new Data();
                msgToSend.strName = players[0].name;
                msgToSend.posX = Convert.ToByte(players[0].position.X);
                msgToSend.posY = Convert.ToByte(players[0].position.Y);
                msgToSend.byteColor = Convert.ToByte(comboBox1.SelectedIndex);
                //msgToSend.strMessage = players[0].position.X.ToString() + " " + players[0].position.Y.ToString() + " " + players[0].color.ToString();
                msgToSend.cmdCommand = Command.Draw;

                byte[] byteData = msgToSend.ToByte();

                //Send it to the server
                clientSocket.BeginSendTo(byteData, 0, byteData.Length, SocketFlags.None, epServer, new AsyncCallback(OnSend), null);
            }
            catch (Exception)
            {
                MessageBox.Show("Unable to send message to the server.", "SGSclientUDP: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 31
0
        private void Form1_Load(object sender, EventArgs e)
        {
            _bitmap = new Bitmap(pictureBox1.Size.Width, pictureBox1.Size.Height);
            pictureBox1.Image = _bitmap;

            this.Text = "Графический чат (клиент) ник: " + strName;

            //The user has logged into the system so we now request the server to send
            //the names of all users who are in the chat room
            Data msgToSend = new Data ();
            msgToSend.cmdCommand = Command.ClientList;
            msgToSend.strName = strName;
            msgToSend.strMessage = null;

            byteData = msgToSend.ToByte();

            _clientSocket.BeginSend(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnSend), null);

            byteData = new byte[1024];

            //Start listening to the data asynchronously
            _clientSocket.BeginReceive(byteData,
                                       0,
                                       byteData.Length,
                                       SocketFlags.None,
                                       new AsyncCallback(OnReceive),
                                       null);
        }
Esempio n. 32
0
        private void LoginForm_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;

            try
            {
                //Server is listening on port 1000
                foreach (IPAddress ip in Dns.GetHostAddresses(Dns.GetHostName()))
                    if (ip.AddressFamily == AddressFamily.InterNetwork)
                        IPHost = ip;
                IPEndPoint _localEndPoint = new IPEndPoint(IPHost, 0);
                IPEndPoint _ipEndPoint = new IPEndPoint(IPAddress.Broadcast, 11000);

                _findSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                _findSocket.Bind(_localEndPoint);
                _findSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
                //_findSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontRoute, 1);

                epServer = (EndPoint)_ipEndPoint;

                Data msgToSend = new Data();
                msgToSend.cmdCommand = Command.ServerList;
                msgToSend.strMessage = null;
                msgToSend.strName = null;

                _byteDataReg = msgToSend.ToByte();

                //Find the servers
                _findSocket.BeginSendTo(_byteDataReg, 0, _byteDataReg.Length,
                    SocketFlags.None, epServer, new AsyncCallback(OnSendReg), null);

                _byteDataReg = new byte[1024];
                //Start listening to the data asynchronously
                _findSocket.BeginReceiveFrom(_byteDataReg,
                                           0, _byteDataReg.Length,
                                           SocketFlags.None,
                                           ref epServer,
                                           new AsyncCallback(OnReceiveReg),
                                           null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SGSclient",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            txtName.Text = "user" + (new Random(DateTime.Now.Millisecond).Next() % 89 + 10);
        }