Winsock file transfer

Hi!

I'm working on a simple project - I want to transfer binary files using winsock. But I've reached a major problem.

I've studied this and googled for a couple of hours, but I do NOT understand what the problem is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
int recvDll(SOCKET s)
{
    int file_size;
    char fileLength[50] = "";
    FILE *fp;
    int bytes_recieved, bytes_sent, bytes_written;
    
    int recs = recv(s, fileLength, 32, 0);
    fileLength[recs] = '\0';
    file_size = atoi(fileLength);
    
    fp = fopen("testrecv.dll", "wb");
    
    printf("[+] Size of recieved DLL: %d\n", file_size);

    while(file_size > 0)
    {
                    printf("filesize: %d\n", file_size);
                    char buf[512];
                    if(file_size >= 512)
                    {
                           bytes_recieved = recv(s, buf, 512, 0);
                           fwrite(buf, 512, 1, fp);
                    } else {
                           printf("x: %d\n", file_size);
                           bytes_recieved = recv(s, buf, file_size, 0);
                           buf[file_size] = '\0';
                           fwrite(buf, file_size, 1, fp);
                           
                    }
                    
                    file_size -= bytes_recieved;
    }

    fclose(fp);
    return 0;
}


The server that sends the file is written i VB, and splits up the file รก 512 byte. But the transfer seems to be discontinued right in the middle of the download.

This is a typical output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[+] Size of recieved DLL: 122861
filesize: 122861
filesize: 122349
filesize: 121863
filesize: 121351
filesize: 120839
filesize: 120327
filesize: 120324
filesize: 119812
filesize: 119300
filesize: 118864
filesize: 118785
filesize: 118273
filesize: 117761
filesize: 117249
filesize: 117246
filesize: 116734
filesize: 116222
filesize: 115786
filesize: 115707
filesize: 115195
filesize: 114683
filesize: 114171
filesize: 114168
filesize: 113656
filesize: 113144
filesize: 112632
filesize: 112629
filesize: 112117
filesize: 111605
filesize: 111093
filesize: 111090
filesize: 110579


I would appreciate any help, since this has been bugging me for a long time now.
I've noticed this:

This file starts off at size: 122861 bytes

So you read the file in 512 bytes chuncks - but if you check the difference
between the values in the printout you will see that 512 bytes is not being
received each time, but you are witing the full buffer (of 512 bytes) to the file - which would mean a corrupt file.

Here is the offending bit of code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
printf("filesize: %d\n", file_size);
                    char buf[512];
                    if(file_size >= 512)
                    {
                           bytes_recieved = recv(s, buf, 512, 0);
                           fwrite(buf, 512, 1, fp); //Error - writing 512 bytes but 512 bytes may not have been received
                    } else {
                           printf("x: %d\n", file_size);
                           bytes_recieved = recv(s, buf, file_size, 0);
                           buf[file_size] = '\0'; //??
                           fwrite(buf, file_size, 1, fp); //similar error
                           
                    }
                    
                    file_size -= bytes_recieved;


I think you should be writing the actual number of bytes received like this:

fwrite(buf, bytes_received, 1, fp);
Last edited on
Topic archived. No new replies allowed.