C/Visual C - Problema al mandar archivo del servidor al cliente

 
Vista:

Problema al mandar archivo del servidor al cliente

Publicado por Pamela (3 intervenciones) el 20/07/2007 15:41:59
Hola,

Estoy haciendo una aplicacion cliente servidor y tengo un problema. Para mandar archivos
del servidor al cliente utilizo el siguiente codigo:

parte servidor:

CFile myFile;
myFile.Open("prueba.html", CFile::modeRead | CFile::typeBinary);

int myFileLength = myFile.GetLength(); // Going to send the correct File Size

m_sConnectSocket.Send(&myFileLength, 4); // 4 bytes long

byte* data = new byte[myFileLength];

myFile.Read(data, myFileLength);

m_sConnectSocket.Send(data, myFileLength); //Send the whole thing now

myFile.Close();
delete data;

parte cliente:

CFile file;

int dataLength;
m_sConnectSocket.Receive(&dataLength, 4); //Now we get the File Size first

file.Open("C:\\prueba.html",CFile::modeWrite |CFile::modeCreate| CFile::typeBinary);
byte* data=new byte[dataLength];

m_sConnectSocket.Receive(data,dataLength);

file.Write(data, dataLength);

file.Close();

la variable m_sConnectSocket es tipo CAsyncSocket. El problema q tengo es q a veces anda y a veces llega mal el archivo.
A veces llega perfecto, a veces mas corto y a veces mas corto y con caracteres extranios...

POR FAVOR SI ALGUIEN PUEDE AYUDARME PORQ LA VERDAD NO PUEDO ENCONTRAR EL ERROR....

GRACIAS DE ANTEMANO,

PAMELA
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Problema al mandar archivo del servidor al clie

Publicado por fernando.gomez (1603 intervenciones) el 20/07/2007 22:11:36
Pues de entrada tienes un error:

byte* data = new byte[myFileLength];
...
delete data; // <---------------------

Recuerda que un array es una suceción contínua de datos. Aquí solo eliminaste el primer segmento, y ya tienes un comportamiento indefinido. Lo correcto sería:

byte* data = new byte[myFileLength];
...
delete [] data; // <--------------------- con corchetes [ ]

Es posible que esa sea una primera causa.

Saludos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar