2003-09-22, 17:35
#1
Denna kod funkar hur bra som helst, utan MD5 checken.. Med den kan du inte ladda ner filer strre n 40mb.. Om du gr det stannar den p ca 90% och allt lgger av i programmet... Hr r koden:
void putFile(const char *fname,SOCKET fd2){
char szBuffer[1025];
ifstream fin(fname,ios::binary);
stringstream ss;
unsigned long long int fSize = fin.seekg(0, ios::end).tellg();
int nTotal=0, nBytesRead=0;
fin.seekg (0, ios::beg);
ss << fSize;
send(fd2,string(ss.str() + ";;;").c_str(),100,0);
if (fSize == 0) {
fin.close();
return;
}
if(fSize < 1024) {
fin.read(szBuffer, (fSize));
send(fd2,szBuffer,(fSize),0);
fin.close();
return;
}
while(1){
if (nTotal <= (fSize - 1024)) {
//If file isnt near end
fin.read(szBuffer, 1024);
send(fd2,szBuffer,1024,0);
getInput(fd2);
//Begin MD5 code
string MD5me;
bool ack;
MD5me=MD5text(szBuffer);
MD5me= MD5me +";;;";
sockText(MD5me.c_str(),fd2);
if(getInput(fd2) == "ACK") {ack=true;}
while(!ack) {
send(fd2,szBuffer,1024,0);
string MD5me;
MD5me=MD5text(szBuffer);
MD5me= MD5me +";;;";
sockText(MD5me.c_str(),fd2);
if(getInput(fd2) == "ACK") {ack=true;}
}
nTotal+=1024;
} else {
fin.read(szBuffer, (fSize%1024));
send(fd2,szBuffer,(fSize%1024),0);
getInput(fd2);
string MD5me;
bool ack;
MD5me=MD5text(szBuffer);
MD5me= MD5me +";;;";
sockText(MD5me.c_str(),fd2);
if(getInput(fd2) == "ACK") {ack=true;}
while(!ack) {
send(fd2,szBuffer,(fSize%1024),0);
string MD5me;
MD5me=MD5text(szBuffer);
MD5me= MD5me +";;;";
sockText(MD5me.c_str(),fd2);
if(getInput(fd2) == "ACK") {ack=true;}
}
nTotal+=fSize%1024;
fin.close();
return;
}
}
fin.close();
return;
}
-----------
---OCH----
-----------
ofstream fout2("C:\\WINSOCK\\debug.txt");
unsigned long long int numBytes,totalBytes = 0;
bool getMD5(char buffer[1025]){
sockText("md5;;;",fd2);
string MD5me;
string MD5remote;
MD5me=MD5text(buffer);
// MD5me= MD5me.substr(0,MD5me.size());
MD5remote=getInput(fd2);
fout2 << totalBytes << ": Remote MD5 " << MD5remote << " Local MD5 " << MD5me<< "\n";
if(MD5remote==MD5me) {
return true;
}else{
return false;
}
}
void getFile(const char* fname,SOCKET fd,unsigned long long int fileSize) {
fout2<< "Name: " << fname << "\nSize: " << fileSize << "\n";
transferBar->maximum(fileSize);
ofstream fout(fname,ios::binary);
char buffer[1025];
numBytes=0;
totalBytes=0;
if(fileSize == 0) {
fout.close();
return;
}
if(fileSize < 1024) {
recv(fd,buffer,fileSize,0);
buffer[numBytes] = '\0';
fout.write(buffer,fileSize);
transferBar->value(fileSize);
fout.close();
return;
}
while(1) {
Fl::check();
if(totalBytes <= (fileSize - 1024)) {
numBytes = recv(fd,buffer,1024,0);
buffer[numBytes] = '\0';
if(getMD5(buffer)) {
totalBytes += numBytes;
fout.write(buffer,1024);
fout2 << " OK , ACK\n";
sockText("ACK;;;",fd);
} else {
sockText("DEL;;;",fd);
}
transferBar->value(totalBytes);
} else {
recv(fd,buffer,(fileSize%1024),0);
buffer[numBytes] = '\0';
if(getMD5(buffer)) {
totalBytes += numBytes;
fout.write(buffer,(fileSize%1024));
fout.close();
fout2 << " OK , ACK\n";
sockText("ACK;;;",fd);
fout2.close();
transferBar->value(fileSize);
return;
} else {
sockText("DEL;;;",fd);
}
}
return;
//End
}
}
Har frskt lsa detta i timmar, vilket jag bara lyckats optimera lite kod.. r det nn som orkar hjlpa stakars mig?
void putFile(const char *fname,SOCKET fd2){
char szBuffer[1025];
ifstream fin(fname,ios::binary);
stringstream ss;
unsigned long long int fSize = fin.seekg(0, ios::end).tellg();
int nTotal=0, nBytesRead=0;
fin.seekg (0, ios::beg);
ss << fSize;
send(fd2,string(ss.str() + ";;;").c_str(),100,0);
if (fSize == 0) {
fin.close();
return;
}
if(fSize < 1024) {
fin.read(szBuffer, (fSize));
send(fd2,szBuffer,(fSize),0);
fin.close();
return;
}
while(1){
if (nTotal <= (fSize - 1024)) {
//If file isnt near end
fin.read(szBuffer, 1024);
send(fd2,szBuffer,1024,0);
getInput(fd2);
//Begin MD5 code
string MD5me;
bool ack;
MD5me=MD5text(szBuffer);
MD5me= MD5me +";;;";
sockText(MD5me.c_str(),fd2);
if(getInput(fd2) == "ACK") {ack=true;}
while(!ack) {
send(fd2,szBuffer,1024,0);
string MD5me;
MD5me=MD5text(szBuffer);
MD5me= MD5me +";;;";
sockText(MD5me.c_str(),fd2);
if(getInput(fd2) == "ACK") {ack=true;}
}
nTotal+=1024;
} else {
fin.read(szBuffer, (fSize%1024));
send(fd2,szBuffer,(fSize%1024),0);
getInput(fd2);
string MD5me;
bool ack;
MD5me=MD5text(szBuffer);
MD5me= MD5me +";;;";
sockText(MD5me.c_str(),fd2);
if(getInput(fd2) == "ACK") {ack=true;}
while(!ack) {
send(fd2,szBuffer,(fSize%1024),0);
string MD5me;
MD5me=MD5text(szBuffer);
MD5me= MD5me +";;;";
sockText(MD5me.c_str(),fd2);
if(getInput(fd2) == "ACK") {ack=true;}
}
nTotal+=fSize%1024;
fin.close();
return;
}
}
fin.close();
return;
}
-----------
---OCH----
-----------
ofstream fout2("C:\\WINSOCK\\debug.txt");
unsigned long long int numBytes,totalBytes = 0;
bool getMD5(char buffer[1025]){
sockText("md5;;;",fd2);
string MD5me;
string MD5remote;
MD5me=MD5text(buffer);
// MD5me= MD5me.substr(0,MD5me.size());
MD5remote=getInput(fd2);
fout2 << totalBytes << ": Remote MD5 " << MD5remote << " Local MD5 " << MD5me<< "\n";
if(MD5remote==MD5me) {
return true;
}else{
return false;
}
}
void getFile(const char* fname,SOCKET fd,unsigned long long int fileSize) {
fout2<< "Name: " << fname << "\nSize: " << fileSize << "\n";
transferBar->maximum(fileSize);
ofstream fout(fname,ios::binary);
char buffer[1025];
numBytes=0;
totalBytes=0;
if(fileSize == 0) {
fout.close();
return;
}
if(fileSize < 1024) {
recv(fd,buffer,fileSize,0);
buffer[numBytes] = '\0';
fout.write(buffer,fileSize);
transferBar->value(fileSize);
fout.close();
return;
}
while(1) {
Fl::check();
if(totalBytes <= (fileSize - 1024)) {
numBytes = recv(fd,buffer,1024,0);
buffer[numBytes] = '\0';
if(getMD5(buffer)) {
totalBytes += numBytes;
fout.write(buffer,1024);
fout2 << " OK , ACK\n";
sockText("ACK;;;",fd);
} else {
sockText("DEL;;;",fd);
}
transferBar->value(totalBytes);
} else {
recv(fd,buffer,(fileSize%1024),0);
buffer[numBytes] = '\0';
if(getMD5(buffer)) {
totalBytes += numBytes;
fout.write(buffer,(fileSize%1024));
fout.close();
fout2 << " OK , ACK\n";
sockText("ACK;;;",fd);
fout2.close();
transferBar->value(fileSize);
return;
} else {
sockText("DEL;;;",fd);
}
}
return;
//End
}
}
Har frskt lsa detta i timmar, vilket jag bara lyckats optimera lite kod.. r det nn som orkar hjlpa stakars mig?