'image corrupted in socket c

I trying to send files between server to the socket in c. Sometimes the bmp file is transferred completely, but most of the time i get a corrupted image.

server:

#define BufferLength 100
.
.
.
eofFlag=0;
bufferPointer=(char*)buffer;
do
{
    for (i=0;i<BufferLength;++i)
    {
        ++toSend;
        buffer[i]=getc(f1);
        if (buffer[i]==EOF)
        {
            eofFlag=EOF;
            i=BufferLength;
        }
    }
    sendCode=send(*socketId,bufferPointer,BufferLength*sizeof(int),0);
}while ( (sendCode>0) && (eofFlag!=EOF) );

client:

#define BufferLength 100
.
.
.
eofFlag=0;
bufferPointer=(char*)buffer;
do
{
    receiveCode=recv(*socketId,bufferPointer,(BufferLength)*sizeof(int),0);
    for (i=0;i<BufferLength;++i)
    {
        putc(buffer[i],f1);
        if (buffer[i]==EOF)
        {
            eofFlag=EOF;
            i=BufferLength;
        }
    }
}while( (receiveCode != SOCKET_ERROR) && (eofFlag!=EOF) );

The image that received: http://i44.tinypic.com/33jtmk5.jpg



Solution 1:[1]

I just looked at your sending code.

eofFlag=0;
bufferPointer=(char*)buffer;
do
{

Don't do that, just do a read() to try to fill your entire buffer instead. It's faster.

    for (i=0;i<BufferLength;++i)
    {
        ++toSend;

since buffer is a char, you can't use EOF. That only works because getch can return values from 0 to 255 if valid, and -1 if not valid, once you cast them to char, -1=255 so you confuse EOF with actual 255 stored in the file. Just replace this entire loop with a read().

        buffer[i]=getc(f1);
        if (buffer[i]==EOF)
        {
            eofFlag=EOF;
            i=BufferLength;
        }
    }

Here you send the entire buffer, regardless of how many bytes of it you have filled in the last read.

    sendCode=send(*socketId,bufferPointer,BufferLength*sizeof(int),0);
}while ( (sendCode>0) && (eofFlag!=EOF) );

Solution 2:[2]

Your code is incorrect. The canonical way to copy from FD to a socket in C is as follows:

int count;
char buffer[4096]; // or more, whatever you like
int count;
while ((count = read(inFD, buffer, sizeof buffer)) > 0)
{
    send(outFD, buffer, 0, count, 0);
}

Solution 3:[3]

first of all what will happen if your buffer length is more than the size you have defined thus we always have to use the DMA in this case. Then you have to open the file. Go to SEEK_END using fseek(). Tell the size of file in bytes using ftell() & then allocate the buffer of size told by ftell() + 1. Read the file and then send it over the socket with the length told by ftell().

While receiving the data over other end you need to use select call and continue to read until you get any data to read over the receiving socket (since you are not sending the size of file, I would recommend you to send the file after applying http header over it so the receiver could know how much data it needs to receive).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 LtWorf
Solution 2
Solution 3 Ankit Tripathi