Client/Server File Send - Losing Data

I am having a few issues with a file transfer over a network. I am losing data at the end of each file that I send. Here is my code:

Client:

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdafx.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include<windows.h>
#include<winsock2.h>

#pragma comment(lib,"ws2_32.lib") //Winsock Library
#pragma comment(lib,"wininet.lib") /*This links wininet lib only works on Visual Studio VC++ */
#include<wininet.h>
#define Port 6000
using namespace std;
SOCKET Socket;
WSADATA Winsock;
sockaddr_in Addr;
int Addrlen = sizeof(Addr);

int main() {

    WSAStartup(MAKEWORD(2, 2), &Winsock);    // Start Winsock

    if(LOBYTE(Winsock.wVersion) != 2 || HIBYTE(Winsock.wVersion) != 2)    // Check version
    {
    }

     Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    ZeroMemory(&Addr, sizeof(Addr));    // clear the struct
    Addr.sin_family = AF_INET;    // set the address family
    Addr.sin_addr.s_addr = inet_addr("192.168.15.241");
    Addr.sin_port = htons(6000);    // set the port

    if(connect(Socket, (sockaddr*)&Addr, sizeof(Addr)) < 0)
    {
    
    }
 

FILE *File;
        char *Buffer;
        unsigned long Size;

        File = fopen("C:\\windows\\temp\\image.bmp", "rb");
        if(!File)
        {
        }

        fseek(File, 0, SEEK_END);
        Size = ftell(File);
        fseek(File, 0, SEEK_SET);

        Buffer = new char[Size];

        fread(Buffer, Size, 1, File);
        char cSize[MAX_PATH];
        sprintf(cSize, "%i", Size);

        fclose(File);

      

  send(Socket, cSize, MAX_PATH, 0); // File size
        
        send(Socket, Buffer, Size, 0);  
delete [] Buffer;
        
        closesocket(Socket);
    }


Server:

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93

#include <stdafx.h>
#include <WinSock2.h>
#include <Windows.h>
#include <stdio.h>
//#include <iostream>
using namespace std;

#pragma comment(lib, "Ws2_32.lib")
#define Port 6000

SOCKET Socket, Sub;
WSADATA Winsock;
sockaddr_in Addr;
sockaddr_in IncomingAddress;
int AddressLen = sizeof(IncomingAddress);

int main()
{
    WSAStartup(MAKEWORD(2, 2), &Winsock);    // Start Winsock

    if(LOBYTE(Winsock.wVersion) != 2 || HIBYTE(Winsock.wVersion) != 2)    // Check version
    {
        WSACleanup();
        return 0;
    }

Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    ZeroMemory(&Addr, sizeof(Addr));
    Addr.sin_family = AF_INET;
    Addr.sin_port = htons(Port);  
    bind(Socket, (sockaddr*)&Addr, sizeof(Addr));

    if(listen(Socket, 1) == SOCKET_ERROR)
    {
        printf("listening error\n");
    }
    else
    {
        printf("listening ok\n");
    }
    
    
        if(Sub = accept(Socket, (sockaddr*)&IncomingAddress, &AddressLen))
    {
        char *ClientIP = inet_ntoa(IncomingAddress.sin_addr);
        int ClientPort = ntohs(IncomingAddress.sin_port);
        printf("Client conncted!\n");
        printf("IP: %s:%d\n", ClientIP, ClientPort);

        printf("Receiving file .. \n");
        
        int Size;
    char *Filesize = new char[1024];


    int Offset = 0;
      if (recv(Sub, Filesize, 1024, 0)) // File size
    {
        Size = atoi((const char*)Filesize);
        printf("File size: %d\n", Size);
    }

    char *Buffer = new char[Size];
    
    while (Size > Offset)
    {
        int Amount = recv(Sub, Buffer + Offset, Size - Offset, 0);

        if (Amount <= 0)
        {
            break;
        }
        else
        {
            Offset += Amount;
            printf("Received %d\n", Offset);
        }
    } 

    FILE *File;
    File = fopen("c://test.bmp", "wb");
    fwrite(Buffer, 1, Size, File);
    fclose(File);

    getchar();
    closesocket(Socket);
    WSACleanup();
    return 0;
}
}


Any and all help would be greatly appreciated!!!

Many Thanks,

Googie.

P.S. I tried posting to the beginners forum but was unable to for an unknown reason.
Last edited on
> send(Socket, Buffer, Size, 0);
You need to check the return result.

send() doesn't guarantee to send the whole buffer in a single call.

You typically need something like
1
2
3
4
pos = 0;
while ( pos != Size && (n=send(Socket, &Buffer[pos], Size-pos, 0)) > 0 ) {
    pos += n;
}
Thank you soooo much for your reply... i will try and get this to work as I am a learner at C++. Any simpler ways to acheive this would be great!

Many Thanks,

Googie.
Topic archived. No new replies allowed.