Ver Mensaje Individual
  #1 (permalink)  
Antiguo 09/09/2008, 08:19
ZeroZ
 
Fecha de Ingreso: junio-2002
Ubicación: España
Mensajes: 348
Antigüedad: 21 años, 11 meses
Puntos: 8
Es correcto cómo leo del socket

Hola estoy leyendo de un socket al que anteriormente le he enviado datos con el siguiente método:

Código PHP:
        public Byte[] GetLongMsg(Socket server)
        {
            
Byte[] header = new Byte[8];
            
Byte[] bytesReceived = new Byte[150000];
            
int recibido 0;
            
int indice 0;

            try
            {
                
recibido server.Receive(headerheader.LengthSocketFlags.None); //header
                
indice += recibido;
                
recibido server.Receive(bytesReceivedbytesReceived.LengthSocketFlags.None); //mensaje
                
indice += recibido;
               
                while (
server.Available 0//si quedan datos por recibir
                
{
                    
Byte[] bytesAvailable = new Byte[server.Available];
                    
recibido server.Receive(bytesAvailablebytesAvailable.LengthSocketFlags.None);
                    
indice += recibido;

                    
bytesAvailable.CopyTo(bytesReceivedindice);                      
                }

                if (
header[0] == 255 && header[1] == 255)
                {
                    
MessageBox.Show(Encoding.ASCII.GetString(bytesReceived), "Error");
                    return 
null
                }
                
server.Close();
            }
            catch (
SocketException e)
            {
                
MessageBox.Show(e.Message"Error");
                return 
null;
            }            
            return 
bytesReceived;
        } 
Con el cojo un XML, pero a veces da problemas y es que corta los XML. Hago un debug y cuando server.Available vale 0 el while deja de ejecutarse, le doy a F10 dos o tres veces y server.Available tiene algun valor, por eso me deja los XML cortados. Hay alguna forma de saber cuando va a terminar realmente de enviar datos un socket? ya que me gustaria inicializar bytesReceived[] con un valor adecuado segun los bytes que vaya a recibir y no con una constante(150000). Hay alguna cosa que hago mal?
Gracias
__________________
Bicis nuevas y de segunda mano: Bicibid