Ejemplo n.º 1
0
        private void LeerSocket()
        {
            // Declaro un array de bytes para contener los mensajes de entrada
            var buffer = new byte[100];

            while (true)
            {
                try
                {
                    // Me quedo esperando a que llegue algún mensaje
                    int cantidadRecibida = socket.Receive(buffer, buffer.Length, SocketFlags.None);
                    if (cantidadRecibida > 0)
                    {
                        // Decodifico el mensaje usando UTF-8 (https://es.wikipedia.org/wiki/UTF-8)
                        string mensaje = Encoding.UTF8.GetString(buffer, 0, cantidadRecibida);
                        // Disparo el evento DatosRecibidos, pasando como arg el mensaje que llegó desde el servidor
                        DatosRecibidos?.Invoke(this, new DatosRecibidosEventArgs(mensaje));
                    }
                }
                catch
                {
                    socket.Close();
                    break;
                }
            }

            Conectado = false;
            // Finalizó la conexión, por lo tanto genero el evento correspondiente
            ConexionTerminada?.Invoke(this, new EventArgs());
        }
Ejemplo n.º 2
0
        public void LeerSocket()
        {
            byte[] buffer = new byte[100]; //instancia de buffer donde colocaremos los datos
            while (true)
            {
                try
                {
                    // Me quedo esperando a que llegue algún mensaje
                    int cantidadRecibida = socket.Receive(buffer: buffer, size: buffer.Length, SocketFlags.None); //retorna numero de bytes

                    if (cantidadRecibida > 0)
                    {
                        string mensaje = Encoding.UTF8.GetString(
                            bytes: buffer,                                                  /*QUE SE MANDA*/
                            index: 0,                                                       /*EN QUE POSICION*/
                            count: cantidadRecibida /*LENGHT*/);                            //se decodifica el mensaje
                        DatosRecibidos?.Invoke(this, new DatosRecibidosEventArgs(mensaje)); //y se invoca con el mensaje
                    }
                }
                catch
                {
                    socket.Close();
                    break;
                }
            }

            conectado = false;
            ConexionTerminada?.Invoke(this, new EventArgs());
        }
Ejemplo n.º 3
0
        private void LeerSocket(IPEndPoint endPoint)
        {
            var buffer  = new byte[100];      // Array a utilizar para recibir los datos que llegan
            var cliente = clientes[endPoint]; // Información del cliente que se va a escuchar

            while (cliente.Socket.Connected)
            {
                try
                {
                    // Me quedo esperando a que llegue un mensaje desde el cliente
                    int cantidadRecibida = cliente.Socket.Receive(buffer, buffer.Length, SocketFlags.None);

                    // Me fijo cuántos bytes recibí. Si no recibí nada, entonces se desconectó el cliente
                    if (cantidadRecibida > 0)
                    {
                        // Decodifico el mensaje recibido usando UTF-8 (https://es.wikipedia.org/wiki/UTF-8)
                        var datosRecibidos = Encoding.UTF8.GetString(buffer, 0, cantidadRecibida);

                        // Disparo el evento de la recepción del mensaje
                        DatosRecibidos?.Invoke(this, new DatosRecibidosEventArgs(endPoint, datosRecibidos));
                    }
                    else
                    {
                        // Disparo el evento de la finalización de la conexión
                        ConexionTerminada?.Invoke(this, new ServidorEventArgs(endPoint));
                        break;
                    }
                }
                catch
                {
                    if (!cliente.Socket.Connected)
                    {
                        // Disparo el evento de la finalización de la conexión
                        ConexionTerminada?.Invoke(this, new ServidorEventArgs(endPoint));
                        break;
                    }
                }
            }
            // Elimino el cliente del dictionary que guarda la información de los clientes
            clientes.TryRemove(endPoint, out cliente);
        }
Ejemplo n.º 4
0
        private void LeerSocket()
        {
            IPEndPoint idReal = _idClienteActual;
            var        with2  = (InfoDeUnCliente)_clientes[idReal];;

            while (true)
            {
                var recibir = new byte[LengBufferSocket];
                try
                {
                    var ret = with2.Socket.Receive(recibir, recibir.Length, SocketFlags.None);

                    if (ret > 0)
                    {
                        with2.UltimosDatosRecibidos = BitConverter.ToString(recibir);

                        _clientes[idReal] = with2;
                        DatosRecibidos?.Invoke(idReal);
                    }
                    else
                    {
                        ConexionTerminada?.Invoke(idReal);
                        break;
                    }
                }
                catch (Exception e)
                {
                    if (!with2.Socket.Connected)
                    {
                        ConexionTerminada?.Invoke(idReal);
                        break;
                    }
                }
            }

            CerrarThread(idReal);
        }