File Transfer - TCP

I am writing a program that transfers a .BMP file over sockets and am having an issue with the received file. It says that the transfer is complete and the resulting bitmap is the exact same size as the file that was sent. I will post my code in hope of some assistance! I know the code is a little bit sloppy but as long as it does the job its fine by me.

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
#include <WinSock2.h>
#include <Windows.h>
#include <stdio.h>
//#include <iostream>
using namespace std;

#pragma comment(lib, "Ws2_32.lib")

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
    {
        WSACleanup();
        return 0;
    }

     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("127.0.0.1");
    Addr.sin_port = htons(6000);    // set the port

    if(connect(Socket, (sockaddr*)&Addr, sizeof(Addr)) < 0)
    {
        printf("Connection failed !\n");
        getchar();
        return 0;
    }

    printf("Connection successful !\n");

    printf("Receiving file .. \n");



    int Size;
    char *Filesize = new char[1024];

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

    char *Buffer = new char[Size];

    //int len = Size;
    //char *data = Buffer;
     int Offset = 0;
    while(Size > Offset)
        {
            int Amount = send(Socket, Buffer + Offset, Size - Offset, 0);

            if(Amount <= 0)
            {

                break;
            }
            else
            {
                Offset += Amount;
                printf("2\n");
            }
        }

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

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


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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

#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("Sending file .. \n");

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

        File = fopen("C://users/user/desktop//Hello.bmp", "rb");
        if(!File)
        {
            printf("Error while readaing the file\n");
            getchar();
            return 0;
        }

        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(Sub, cSize, MAX_PATH, 0); // File size

        //int len = Size;
        //char *data = Buffer;

   
        int Offset = 0;
    while(Size > Offset)
    {
        int Amount = recv(Socket, Buffer + Offset, Size - Offset, 0);

        if(Amount <= 0)
        {

            break;
        }
        else
        {
            Offset += Amount;
            printf("2\n");
        }
    }


        free(Buffer);
        closesocket(Sub);
        closesocket(Socket);
        WSACleanup();
    }

    getchar();
    return 0;
}




Many Thanks,

Googie.
Last edited on
It says that the transfer is complete and the resulting bitmap is the exact same size as the file that was sent.

Isn't that what you want?
*BUMP*

Hi Guys!

Just revisited this post and still havn't solved the problem. The file size is an exact copy of the original although the data is corrupted and I cannot open the file in paintbrush.

Any ideas?

Many Thanks!
You could start by inspecting the data in the corrupted file and see what it looks like compared to the valid one. You might see some offset error or some other difference in the data.
Whats the best place to post these images?
In Client you send the size of the image [to the server], but no actual data.

In Server (on line 53) you create Buffer and send it uninitialized [to the client].
I am a newbie, could you elaborate a little bit?

Many Thanks,

Googie.
Server:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    if(recv(Socket, Filesize, 1024, 0)) // File size
    {
        Size = atoi((const char*)Filesize);
        printf("File size: %d\n", Size);
    }

    char *Buffer = new char[Size]; // Here you allocate memory for the Buffer
// Where do you fill the Buffer? I.e. a recv(Socket, Buffer, Size, ...)

    //int len = Size;
    //char *data = Buffer;
     int Offset = 0;
    while(Size > Offset)
        {
            int Amount = send(Socket, Buffer + Offset, Size - Offset, 0); // You send the uninitialized Buffer 
Still dosn't work... Any chance of a full edit of my code?

Many Thanks!
Anyone? :)
If you want help: Do not mark your topic as solve (the green check mark)

Still dosn't work... Any chance of a full edit of my code?
Nope, do it yourself and understand what you do.

It is actually simple:

After line 53 in Server: Use exact the loop on line 82 in Client.
Line 58: Remove the loop so that a single line remains:

send(Socket, Buffer, Size, 0);
I have moved this topic to a more suitable forum. I cant delete this topic, please dont get mad at me for posting in 2 forums. Please tell me how to delete my posts and I will delete this post promptly!
Topic archived. No new replies allowed.