Example #1
0
        private void Juego_Load(object sender, EventArgs e)
        {
            CargarGraficos();

            if (isMultiplayer == true && isServer == false)
            {
                try
                {
                    CheckForIllegalCrossThreadCalls = false;
                    strName      = player;
                    this.Text    = strName;
                    clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                    IPAddress ipAddress = IPAddress.Parse(serverIP);
                    //Server is listening on port 1000
                    IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, 1000);

                    //Connect to the server
                    clientSocket.BeginConnect(ipEndPoint, new AsyncCallback(OnConnect), null);

                    MessageBox.Show("Connected");

                    msgToSend            = new SocketFiles.Data();
                    msgToSend.cmdCommand = SocketFiles.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);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
Example #2
0
        private void OnConnect(IAsyncResult ar)
        {
            try
            {
                clientSocket.EndConnect(ar);

                //We are connected so we login into the server
                msgToSend            = new SocketFiles.Data();
                msgToSend.cmdCommand = SocketFiles.Command.Login;
                msgToSend.strName    = player;
                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);
            }
        }
Example #3
0
        private void OnReceive(IAsyncResult ar)
        {
            try
            {
                //MessageBox.Show("Entrada aa");
                Socket clientSocket = (Socket)ar.AsyncState;
                clientSocket.EndReceive(ar);

                //MessageBox.Show("Entrada bb");
                //Transform the array of bytes received from the user into an
                //intelligent form of object Data
                Data msgReceived = new Data(byteData);

                // MessageBox.Show("Entrada cc");
                //We will send this object in response the users request
                Data msgToSend = new Data();

                byte[] message;

                //MessageBox.Show("Entrada dd");
                //If the message is to login, logout, or simple text message
                //then when send to others the type of the message remains the same
                msgToSend.cmdCommand = msgReceived.cmdCommand;
                msgToSend.strName    = msgReceived.strName;

                //MessageBox.Show("Entrada ee");
                switch (msgReceived.cmdCommand)
                {
                case Command.Login:

                    //When a user logs in to the server then we add her to our
                    //list of clients

                    ClientInfo clientInfo = new ClientInfo();
                    clientInfo.socket  = clientSocket;
                    clientInfo.strName = msgReceived.strName;

                    clientList.Add(clientInfo);
                    //        MessageBox.Show("Entrada a1");
                    //Set the text of the message that we will broadcast to all users
                    msgToSend.strMessage = "<<<" + msgReceived.strName + " SE HA CONECTADO AL SERVIDOR>>>";
                    break;

                case Command.Logout:

                    //When a user wants to log out of the server then we search for her
                    //in the list of clients and close the corresponding connection

                    int nIndex = 0;
                    foreach (ClientInfo client in clientList)
                    {
                        if (client.socket == clientSocket)
                        {
                            clientList.RemoveAt(nIndex);
                            break;
                        }
                        ++nIndex;
                    }

                    clientSocket.Close();
                    //        MessageBox.Show("Entrada a2");
                    msgToSend.strMessage = "<<<" + msgReceived.strName + " has left the room>>>";
                    break;

                case Command.Message:
                    // textBox1.Text = msgReceived.strMessage.ToString();
                    //Set the text of the message that we will broadcast to all users
                    msgToSend.strMessage = msgReceived.strName + ": " + msgReceived.strMessage;


                    break;

                //         MessageBox.Show("Entrada a3");
                case Command.List:

                    //Send the names of all users in the chat room to the new user
                    msgToSend.cmdCommand = Command.List;
                    msgToSend.strName    = null;
                    msgToSend.strMessage = null;

                    //Collect the names of the user in the chat room
                    foreach (ClientInfo client in clientList)
                    {
                        //To keep things simple we use asterisk as the marker to separate the user names
                        msgToSend.strMessage += client.strName + "*";
                    }

                    message = msgToSend.ToByte();

                    //Send the name of the users in the chat room
                    clientSocket.BeginSend(message, 0, message.Length, SocketFlags.None,
                                           new AsyncCallback(OnSend), clientSocket);
                    //      MessageBox.Show("Entrada a4");
                    break;
                }

                if (msgToSend.cmdCommand != Command.List)   //List messages are not broadcasted
                {
                    message = msgToSend.ToByte();

                    foreach (ClientInfo clientInfo in clientList)
                    {
                        if (clientInfo.socket != clientSocket ||
                            msgToSend.cmdCommand != Command.Login)
                        {
                            //Send the message to all users
                            clientInfo.socket.BeginSend(message, 0, message.Length, SocketFlags.None,
                                                        new AsyncCallback(OnSend), clientInfo.socket);
                        }
                    }
                    //      MessageBox.Show("Entrada a5");
                    //txtLog.Text += msgToSend.strMessage + "\r\n";
                    //       MessageBox.Show("Entrada a6");
                }

                //If the user is logging out then we need not listen from her
                if (msgReceived.cmdCommand != Command.Logout)
                {
                    //Start listening to the message send by the user
                    clientSocket.BeginReceive(byteData, 0, byteData.Length, SocketFlags.None, new AsyncCallback(OnReceive), clientSocket);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "SGSserverTCP3", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }