can't send files with sockets
hi! I need to send files from a java client to a c++ server. What i do is: I divide the size of the files into blocks. For example if the client wants to send a file of 100 KB he to this:
1º que sends the size of the block (an int). For examplee 30 KB 2º que sends the amount of bloacks ( 3 ) 3º the bytes that were not send ( 10 ) 4º sends all blocks one each time 5º finish The problem is this: the server receive the file, i mean, the size of the file is perfect but i can't open it. for exampleif its a JPG when you open it, it is all black. What can be happening ?? Please, i really need to solve this. I use kdevelop in suse 10. This client code is this: Code:
import java.io.*; and the server: int Transmisor::recibirArch(char* archNuevo,char* archTexto, int socket) { /**Se encarga de recibir un archivo desde destinatario*/ int resp; cout<<"archNUevo: "<<archNuevo<<endl; cout<<"socket: "<<socket<<endl; ofstream out(archNuevo, ios::out | ios::binary | ios::trunc); int cant_bloques,tam_bloque,i,cant_restantes; char buf[MAX_TAM_BUFFER]; bzero(buf,MAX_TAM_BUFFER); if(Socket::recibir(socket,buf,4)==-1) perror("Error"); tam_bloque=0; memcpy(&tam_bloque,buf,4); tam_bloque=ntohl(tam_bloque); cout <<"tam bloquue: "<<tam_bloque<<endl; bzero(buf,MAX_TAM_BUFFER); if(Socket::recibir(socket,buf,4)==-1) perror("Error"); cant_bloques=0; memcpy(&cant_bloques,buf,4); cant_bloques=ntohl(cant_bloques); cout <<"cant_bloques: "<<cant_bloques<<endl; bzero(buf,MAX_TAM_BUFFER); if(Socket::recibir(socket,buf,4)==-1) perror("Error"); cant_restantes=0; memcpy(&cant_restantes,buf,4); cant_restantes=ntohl(cant_restantes); cout <<"cant_restantes: "<<cant_restantes<<endl; char buffer[tam_bloque]; for(i=0; i<cant_bloques; i++) { bzero(buffer,tam_bloque); Socket::recibir(socket,buffer,tam_bloque); out.write(buffer,tam_bloque); } cout<<"okok,I: "<<i<<endl; char buffer2[cant_restantes]; bzero(buffer2,cant_restantes); Socket::recibir(socket,buffer2,cant_restantes); out.write(buffer2,cant_restantes); out.flush(); out.close(); /*una vez que recibi bien el arch llamo a un metodo para que me lop convuierta a arch de texto plano*/ resp = this->convertirATexto(archNuevo,archTexto); return resp; } lots of thanks |
Why are you breaking the file down?
Are you using raw sockets, if so do you have any error checking, why not use tcp which has some error checking ad flow control? I'd suggest that you send the file as a whole. See if that works then investigate the other requirements. As for your problem why not do a diff between the two files? |
i use tcp sockets. the second file is because y need to convert the original file to a txt file. But i need to store both of them.
I heard that tcp/ip doesn't let you send more than 64KB in one send(). I mean, you can send more but in more sends(). is this ok ?? thanks |
All times are GMT -5. The time now is 03:48 PM. |