How can I transfer a file with the .exe extension, say, 30mb in size over a socket? What features to use? What is the algorithm?
// ------------------------------------------------ --------
Faced such a problem: I can not send a file in pieces, that is, I have a buffer of 1024, and a file, say, 17 * 1024. It is impossible to read in turn 1024 bytes from the file . ReadFile & fread reads the entire file, not paying attention to the size of the written buffer. Tried to play with the position in the stream and fgets? but nothing happened either. Who knows how to send the file in parts?
// ------------------------------------------------ ---------
Already figured out, thank you all for your help) The final file transfer code:
FILE *in = fopen("SocketServer.exe","rb"); while(!feof(in)) { b=fread(bufer,1,sizeof(bufer),in); size=ftell(in); printf("bytes read: %d, part:%d, pos: %ld \n",b,i,size); if(b!=0) send(current,bufer,b,0); i++; } Well, the reception of the file:
while (1) { int nbytes = recv( soc, buf, sizeof(buf), 0 ); if ( nbytes == 0) { cout<<"Disconnected."<<endl; return; } if (nbytes < 0) { cout<<"Error: "<<WSAGetLastError()<<endl; return ; } WriteFile(F2,buf,nbytes,&j,NULL); cout<<nbytes<<","<<i<<endl; i++; }
nc -l 2000 > outfile, in another -nc localhost 2000 <infile- this is the transfer of the file through a tcp-socket in a continuous stream. (nc is a netcat utility) Listing it in c ++ is lazy. - alexlz