Jump to content

[C, Win32] Reading a file to a Socket

- - - - -

  • Please log in to reply
2 replies to this topic

#1
OnyxDragun

OnyxDragun

    Newbie

  • Members
  • PipPip
  • 15 posts
I'm trying to properly read from a file (in Win32 using C) and writing it out to a open TCP socket.

I can create and bind the socket between client/server.

But this is sorta what I have for the client:


    HANDLE fFile;

    char  *sp;

    int iFileLength;

    ...

    hFile = CreateFile (szFileName,

                        GENERIC_READ,

                        FILE_SHARE_READ,

                        NULL,

                        OPEN_EXISTING,

                        0,

                        NULL);

    if(hFile == INVALID_HANDLE_VALUE)

    {

        MessageBox(hwnd, "Cannot open file", "File", MB_OK);

    }

    iFileLength = GetFileSize(hFile, NULL) ;

    sp = (char*)malloc(sizeof(iFileLength+2));

    while(ReadFile(hFile, sp, BUFSIZE, &dwBytesRead, NULL) && iFileLength > 0)

    {

        send(sd, sp, BUFSIZE, 0);

        iFileLength -= dwBytesRead;

        sp += dwBytesRead;

    }

    CloseHandle(hFile);

    free(sp);


Within the server, I have a while loop that reads from recv()


#define BUFSIZE 512


char *bp, buf[BUFSIZE];

...

bp = buf;

while((n = recv(send_sd, bp, BUFSIZE, )) <= BUFSIZE)

{


    if(n == 0)

        break;

    printf("%s\n", bp);

    bp += n;

    bytes_to_read -= n;

}


Now what I am seeing is
1. the server stops after 512 byte are read (i would assume it has something to do with the while loop going to BUFSIZE)
2. the client tends to die a horrific death as I think I am corrupting the heap.

I"m trying to learn... i've spent all day trying to get my client (GUI) to send a file to server (console) and just display it to stdout.

lol

#2
OnyxDragun

OnyxDragun

    Newbie

  • Members
  • PipPip
  • 15 posts
Well I found that if I

memset((char*)sp, 0, sizeof(sp));

in the client, that seemed to have corrected the heap corruption issue

Though, I am not sure how I can get the server to keep reading the packets that are arriving. Right now it closes the connection atfer 512 bytes (as is the condition of the while loop) but running wireshark, tells me that the client is sending the entire file, ie there are multiple tcp data packets being sent in 1024 chunks.

What logic am I missing in my server to output the data from the client?

#3
kernelcoder

kernelcoder

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 282 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal
The only flaw in your receiving code is 'bp += n;'. Consider the following situation...
  • Client sends 512 bytes.
  • Server receives 512 bytes (note bp now points to the end of the buffer after execution of line 'bp += n;' as the buffer length is 512).
  • and next recv is returning zero. So as n is 0, server is breaking from the while loop.

The solutions is that you just need to omit the line 'bp += n;' from the server receiving loop.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users