dimanche 12 juin 2016

Transmitting file, concept for Server/Client loop


Send:

do {
    filep = ftell(file);

    if(filesize <= buffsize){
        fread(buffer, 1, filesize, file);
        ts = UDT::send(socket, (char*) buffer, filesize, 0);
    }else{
        fread(buffer, 1, buffsize, file);
        ts = UDT::send(socket, (char*) buffer, buffsize, 0);
    }

    if (ts == UDT::ERROR) {
        cout << "sendfile error: " << UDT::getlasterror().getErrorMessage();
        return false;
    }

    if(filesize >= buffsize && ts!=buffsize){
        fseek(file,filep + (buffsize-ts) , SEEK_SET);
    }

    filesize -= ts;
} while (filesize > 0);

Recv:

do {
    ts = UDT::recv(socket, (char*) buffer, buffsize, 0);

    if (ts == UDT::ERROR) {
        cout << "recvfile error: " << UDT::getlasterror().getErrorMessage();
        return false;
    }

    fwrite(buffer, 1, ts, file);
    filesize -= ts;

} while (filesize > 0);

As you see I am using the UDT libary but I think thats not really important, it could be any other protocol. My problem is that this sometimes works, but sometimes the file does not get transmitted correctly. I guess the Recv function should work. In the Send function I save the filepositon, read the filedata into a buffer and send it, if the send function failed to send the whole buffersize, I set it back to the last position where relevant data was read. The filesize if always correct but sometimes the file itself is incorrect. I am also aware that there is a sendfile function in the udt libary but I cant use it becasue I have to manipulate the data in the future.


Aucun commentaire:

Enregistrer un commentaire