示例#1
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.cmdCommand = Command.List;
            msgToSend.strName = strName;
            msgToSend.strMessage = null;
            msgToSend.clientName = 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);
        }
示例#2
0
文件: LoginForm.cs 项目: test08/udp
        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);
            }
        }
示例#3
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);
            }
        }
示例#4
0
        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                _clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                //Server is listening on port 11000
                _ipServer = IPAddress.Parse(listServers.SelectedItem.ToString().Split(':')[1].Trim());
                IPEndPoint ipEndPoint = new IPEndPoint(_ipServer, 11000);

                Data msgToSend = new Data();

                msgToSend.cmdCommand = Command.Login;
                msgToSend.strName = txtName.Text;
                msgToSend.strMessage = txtPwd.Text;
                msgToSend.clientColor = panelClr.BackColor;

                //Connect to the server
                _clientSocket.BeginConnect(ipEndPoint, new AsyncCallback(OnConnect), msgToSend);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SGSclient", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
示例#5
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);
     }
 }
示例#6
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);
            }
        }
示例#7
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;
     
 }
示例#8
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);
            }
        }
示例#9
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);
            }
        }
示例#10
0
        private void OnReceiveReg(IAsyncResult ar)
        {
            try
            {
                _findSocket.EndReceive(ar);

                //Convert the bytes received into an object of type Data
                Data msgReceived = new Data(_byteDataReg);

                listServers.Items.Add(msgReceived.strName + " : " + msgReceived.strMessage);

                _byteDataReg = new byte[1024];

                //Start listening to receive more data from the user
                _findSocket.BeginReceiveFrom(_byteDataReg, 0, _byteDataReg.Length, SocketFlags.None, ref epServer,
                    new AsyncCallback(OnReceiveReg), null);
            }
            catch (ObjectDisposedException)
            { }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SGSclient: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            listServers.SetSelected(0, true);
        }
示例#11
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);
            }
        }
示例#12
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);
            }
        }
示例#13
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);
        }
示例#14
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);
            }*/
        }
示例#15
0
        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                clientSocket.EndReceive(ar);

                //Convert the bytes received into an object of type Data
                Data msgReceived = new Data(byteData);

                //Accordingly process the message received
                switch (msgReceived.cmdCommand)
                {
                    case Command.Login:
                        lstChatters.Items.Add(msgReceived.strName);

                        int tmp=  lstChatters.Items.Count;
                        for (int i = players.Count; i < tmp; i++)
                        {
                            players.Add(new Player(lstChatters.Items[i].ToString()));
                        }
                        //Invalidate();
                        _isMove = true;
                        break;

                    case Command.Logout:
                        players.RemoveAt(GetUser(msgReceived.strName));
                        lstChatters.Items.Remove(msgReceived.strName);

                        _isMove=true;
                        break;

                    case Command.Message:
                        break;

                        case Command.Draw:
                            {
                                switch (msgReceived.byteColor)
                                {
                                    case 0: color = Color.Red;
                                        break;
                                    case 1: color = Color.Yellow;
                                        break;
                                    case 2: color = Color.Green;
                                        break;
                                    case 3: color = Color.Blue;
                                        break;
                                    case 4: color = Color.Magenta;
                                        break;
                                }
                                int userPosition = GetUser(msgReceived.strName);

                                players[userPosition].position = new Point(msgReceived.posX,msgReceived.posY);
                                players[userPosition].color = color;
                                _isMove = true;
                            }
                        break;

                    case Command.List:
                        lstChatters.Items.AddRange(msgReceived.strMessage.Split('*'));

                        lstChatters.Items.RemoveAt(lstChatters.Items.Count - 1);
                        for (int masha = 0; masha < lstChatters.Items.Count; masha++ )
                            players.Add(new Player(lstChatters.Items[masha].ToString()));
                        players.RemoveAt(players.Count - 1);
                        txtChatBox.Text += "<<<" + strName + " has joined the room>>>\r\n";
                       // Invalidate();
                        _isMove = true;
                        break;
                }

                if (msgReceived.strMessage != null && msgReceived.cmdCommand != Command.List)
                    txtChatBox.Text += msgReceived.strMessage + "\r\n";

                byteData = new byte[1024];

                //Start listening to receive more data from the user
                clientSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epServer,
                                           new AsyncCallback(OnReceive), null);
            }
            catch (ObjectDisposedException)
            { }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SGSclient: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
示例#16
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);
                }
            }
        }
示例#17
0
        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                clientSocket.EndReceive(ar);

                Data msgReceived = new Data(byteData);
                //Accordingly process the message received
                switch (msgReceived.cmdCommand)
                {
                    case Command.Login:
                        lstChatters.Items.Add(msgReceived.strName);
                        break;

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

                    case Command.Message:
                        break;

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

                if (msgReceived.strMessage != null && msgReceived.cmdCommand != Command.List)
                    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 (Exception ex)
            {
                MessageBox.Show(ex.Message, "SGSclientTCP: " + strName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
示例#18
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);
        }
示例#19
0
        private void OnReceive(IAsyncResult ar)
        {
            _clientSocket.EndReceive(ar);
            Data msgReceived = new Data(_byteData);

            if (msgReceived.cmdCommand == Command.LoginError)
                MessageBox.Show("Wrong Password!", "Oops!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                strName = txtName.Text;
                DialogResult = DialogResult.OK;
                _clientClr = panelClr.BackColor;
                Close();
            }
        }