elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Rompecabezas de Bitcoin, Medio millón USD en premios


  Mostrar Temas
Páginas: [1]
1  Programación / .NET (C#, VB.NET, ASP) / Problema con Sockets C# en: 9 Noviembre 2010, 21:26 pm
Buenas.

Tengo un servidor, que recibe conexiones de muchos clientes, lo hago usando las clases TcpListener para el servidor y TcpClient para los clientes con su respectivo NetworkStream para enviar y recibir datos.

Entonces, quiero que veais el codigo a ver si veis algo mal, por que si lo depuro, y voy linea por linea con el F11 dandole paso yo funciona perfecto, en el momento que lo dejo libre, falla, por lo cual no puedo saber donde es, un ejemplo, para enviar un string a la conexion hago esto en el que va a recibir:

Código
  1. public string recibirString()
  2.        {
  3.            if (stream == null)
  4.                stream = conexion.GetStream();
  5.            int longitud = recibirLongitud();
  6.            //stream = conexion.GetStream();
  7.            Byte[] bytes = new Byte[longitud];
  8.            Int32 i;
  9.            String datos = String.Empty;
  10.            if ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
  11.            {
  12.                datos = System.Text.Encoding.UTF8.GetString(bytes, 0, i);
  13.                return datos;
  14.            }
  15.            return datos;
  16.        }
  17.  

y recibirLongitud es:

Código
  1. private int recibirLongitud()
  2.        {
  3.            if (stream == null)
  4.                stream = conexion.GetStream();
  5.            Int32 longitud = 0;
  6.            Byte[] longitudByte = new Byte[4];
  7.            //stream = conexion.GetStream();
  8.            stream.Read(longitudByte, 0, longitudByte.Length);
  9.            longitud = BitConverter.ToInt32(longitudByte, 0);
  10.            return longitud;
  11.        }
  12.  

Eso el que lo recibe, y el que envia hace esto cuando ese esta esperando a recibir:

Código
  1. public bool enviarString(string mensaje)
  2.        {
  3.            if (stream == null)
  4.                stream = conexion.GetStream();
  5.            try
  6.            {
  7.                Byte[] paquete = System.Text.Encoding.UTF8.GetBytes(mensaje);
  8.                enviarLongitud(paquete);
  9.                stream.Write(paquete, 0, paquete.Length);
  10.                stream.Flush();
  11.                return true;
  12.            }
  13.            catch { return false; }
  14.        }
  15.  

donde enviarLongitud es:

Código
  1. private bool enviarLongitud(Byte[] paquete)
  2.        {
  3.            if (stream == null)
  4.                stream = conexion.GetStream();
  5.            try
  6.            {
  7.                int longitud = paquete.Length;
  8.                byte[] longitudBytes = BitConverter.GetBytes(longitud);
  9.                stream.Write(longitudBytes, 0, longitudBytes.Length);
  10.                stream.Flush();
  11.                return true;
  12.            }
  13.            catch { return false; }
  14.        }
  15.  

Graaaacias por leer.

Algun comentario?  :(

EDITO: stream en este caso seria el NetworkStream y conexion es el TcpClient.
Páginas: [1]
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines