views:

136

answers:

4

So I'm trying to create a simple file transfer method. It's completely working for small files (a few bytes). But if I want to transfer a file with the size of 2 kB, it returns unicode characters instead of that what's inside the file.

Server:


void DownloadFile(SOCKET Socket){
    if(Socket == NULL){
        return;
    }
    while(1){
        char filename[1024];
        recv(Socket, filename, sizeof(filename), 0);
        if(filename[0] == '.'){
            break;
        }
        FILE* fp = fopen(filename, "r");
        fseek(fp, 0, SEEK_END);
        long FileSize = ftell(fp);
        char GotFileSize[1024];
        _itoa_s(FileSize, GotFileSize, 10);
        send(Socket, GotFileSize, 1024, 0);
        rewind(fp);


        long SizeCheck = 0;
        char* mfcc;

        if(FileSize > 1499){
            mfcc = (char*)malloc(1500);
            while(1){
                if(SizeCheck >= FileSize){
                    fclose(fp);
                    Sleep(500);
                    free(mfcc);
                    break;
                }
                fread_s(mfcc, 1499, sizeof(char), 1499, fp);
                send(Socket, mfcc, FileSize, 0);
                SizeCheck += strlen(mfcc);
            }
        }
        else{
            mfcc = (char*)malloc(FileSize + 1);
            fread_s(mfcc, FileSize, sizeof(char), FileSize, fp);
            send(Socket, mfcc, FileSize, 0);
            fclose(fp);
            Sleep(500);
            free(mfcc);
        }
    }
    return;
}

Client:


void DownloadFile(SOCKET Socket){
    if(Socket == NULL){
        return;
    }
    while(1){
        printf("Input local filename: ");
        char localfile[1024];
        gets_s(localfile, 1024);
        if(localfile[0] == '.'){
            send(mySocket, localfile, sizeof(localfile), 0);
            break;
        }
        printf("Input remote filename: ");
        char filename[1024];
        gets_s(filename, 1024);
        if(filename[0] == '.'){
            send(mySocket, filename, sizeof(filename), 0);
            break;
        }
        send(mySocket, filename, sizeof(filename), 0);
        char GotFileSize[1024];
        recv(mySocket, GotFileSize, 1024, 0);
        long FileSize = atoi(GotFileSize);
        long SizeCheck = 0;
        FILE *fp = fopen(localfile, "w");
        char* mfcc;
        if(FileSize > 1499){
            mfcc = (char*)malloc(1500);
            while(1){
                if(SizeCheck >= FileSize){
                    fclose(fp);
                    Sleep(500);
                    free(mfcc);
                    break;
                }
                recv(mySocket, mfcc, 1499, 0);
                fprintf(fp, "%s", mfcc);
                SizeCheck += strlen(mfcc);
            }
        }
        else{
            mfcc = (char*)malloc(FileSize + 1);
            recv(mySocket, mfcc, FileSize, 0);
            fprintf(fp, "%s", mfcc);
            fclose(fp);
            Sleep(500);
            free(mfcc);
        }
    }
}

Now, if I transfer this file:


Testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest

The transfered file contains this:


Testtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
𭺫«««««««îþîþtesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesº««««««««îþîþttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttestesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttesttest
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
ð­º
𭺫«««««««îþîþ
+3  A: 

Why this:

            fread_s(mfcc, 1499, sizeof(char), 1499, fp);
            send(Socket, mfcc, FileSize, 0);

you are sending FileSize bytes but you don't even know how much you've read. Check the return value of the read, don't use strlen() for this for you don't know if the file contains 0 bytes.

mvds
For this kind of problem I usually suggest people to print values in hex for better readability: fprintf(fp, "0x%x", mfcc);
karlphillip
Well, I found that the default socket size is 1500 bytes. So if the file's larger than 1500 bytes, split it in chunks. I might've done it wrong though.@Karlphillip: I'll try that.
Wazakindjes
@karl but what use is it to print the memory offset of the buffer? (mfcc is a pointer..)
mvds
@Waza: if you call `send` you need to tell it how much you want to send. If you call `read`, it tells you how much it actually read. So take the return value of the read, and pass it to send instead of FileSize. (also, please develop a habit of checking all return values, always)
mvds
btw, if you just `write` to the socket, the network stack will do this chopping for you.
mvds
@Mvds: With read, I take it that you mean fread? Or am I mistaken? Also, it works the way it is if FileSize < 1500. It's just the part when FileSize >= 1500. My compiler can't find write() nor read(). What header file do I need?
Wazakindjes
@waza: yes, I'm referring to how these things work in general; the following in no way replaces a proper explanation, but in short: `read`-like functions give the number of bytes read, and you must give `send`- or `write`-like functions the number of bytes you want to write. Try `fwrite` or `fwrite_s` in your environment. `write` like functions treat the socket and data as a stream, `send` like functions treat it as a channel to send packets on. functions starting with `f...` generally work on stream pointers, functions without the `f` generally work on the raw fd.
mvds
So, I can use fwrite and fread on the socket?
Wazakindjes
@waza: I think you have enough pointers to go and experiment.
mvds
A: 
Wazakindjes
If you didn't change the sender, it is still missing the return value checks for fread_s and might send the unitialized buffer behind what's actually filled by the read. You might want to post your current code.
Frank
A: 

Ok, I found where the error was. I fail. Seriously. I had

fread_s(mfcc, 1499, sizeof(char), FileSize, fp);

instead of

fread_s(mfcc, 1499, sizeof(char), 1499, fp);

Anyways, that's fixed now. It sends the file perfectly. One problem: I have to close the receiver before it actually writes.

Sender:

while(SizeCheck {issmallerthan} FileSize){
    int Read = fread_s(mfcc, 1499, sizeof(char), 1499, fp);
    send(Socket, mfcc, Read, 0);
    SizeCheck += Read;
}

Receiver:

while(SizeCheck {issmallerthan} FileSize){
        int Received = recv(mySocket, mfcc, 1499, 0);
        fwrite(mfcc, 1, Received, fp);
        SizeCheck += Received;
    }

For some reason, when it contains < it doesn't work. :S

After the while loop, there's

fclose(fp);
Sleep(500);
free(mfcc);

Also, I put this in the loop:

printf("Filesize: %d\nSizecheck: %d\nReceived: %d\n\n", FileSize, SizeCheck, Received);

Returns:

Filesize: 4874
Sizecheck: 1260
Received: 1260

Filesize: 4874
Sizecheck: 2759
Received: 1499

Filesize: 4874
Sizecheck: 4258
Received: 1499

Filesize: 4874
Sizecheck: 4526
Received: 268
Wazakindjes
Nobody knows? D:
Wazakindjes
A: 

Fixed it. A newline is 2 bytes in size, but my program only counted it as one. I figured this because the difference between Filesize and Sizecheck is in fact the amount of lines in the file.

New loop:

char* mfcc;
mfcc = (char*)malloc(FileSize + 1);
while(SizeCheck {issmallerthan} FileSize){
    int Received = recv(mySocket, mfcc, FileSize, 0);
    int Written = fwrite(mfcc, sizeof(char), Received, fp);
    SizeCheck += Written;
    for(int i = 0; i {issmallerthan} Written; i++){
        if(mfcc[i] == '\n'){
            SizeCheck += 1;
        }
    }
}
fclose(fp);
free(mfcc);

The sender part is like this one.

Thanks for the help all. :3

Case closed. :D

Wazakindjes