do loop in read file and send socket

Hi

What's wrong here?
hfile = CreateFileW(TheFile,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_SEQUENTIAL_SCAN,(HANDLE) NULL);

success = ReadFile(hfile,thebuf,sizeof(thebuf),&rlen,NULL);
while(success & rlen)
{

            send(sock, (char *)thebuf, 1029, 0);
       success = ReadFile(hfile, thebuf, sizeof(thebuf), &rlen, NULL);

}



and I start reading. thebuf is unsigned char. thebuf size is 1029.

If file is below 1029 bytes this code just works fine. If it's larger, if it's modulus of 1029, again (I THINK!!) it works, sometimes I see it works and some times not...But when it's not modulus of 1029, mostly it fails...

It fails means Apache don't receive file properly.

Any ideas?
LVL 17
CSecurityAsked:
Who is Participating?
 
pgnatyukConnect With a Mentor Commented:
In the code you posted the first line looks strange:
hfile = CreateFileW(TheFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);

I'd set the buffer size as 1024. It looks better and, sometimes, works better.
Also for the next line:
success = ReadFile(hfile,thebuf,1024,&rlen,NULL);

I think in this line:
send(sock, (char *)thebuf, 1029, 0);

you should write:
send(sock, (char *)thebuf, rlen, 0);

the code will be better if it will be something like that:

hfile = CreateFileW(TheFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hfile == INVALID_HANDLE_VALUE)
    return;

do {
       success = ReadFile(hfile, thebuf, 1024, &rlen, NULL);
       if (!success)
           break;
       send(sock, (char*)thebuf, rlen, 0);
} while(rlen == 1024);

maybe, if you will apply these changes, everything will be okay with the sending.
0
 
CSecurityAuthor Commented:
Not worked... I did all you said and I tried also another method (using _wfopen and fread)...

No luck again!

This send is in a loop, now new strange thing...

It only sends 8192 bytes (8kb), nothing more...
0
 
pgnatyukCommented:
Seems like you need to show more code.
Why 8192? the buffer is 1024. What is the file size 1024 * 8 = 8192?

For a test purpose, to be sure that there is no problem with the sending, you can create new file and save all read data there. The input file and this new file should be identical.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
The simple loop you have constructed is likely to fail for a number of reasons; however, you don't perform any error checking at any stage. There could be a number of reasons for failure, which could either be related to reading from the file or sending via the socket. You should augment your code to check all return values and report what they are if they are not success values. Further, GetLastError() will give you more specific information on the failure.

Also, note that when sending via a socket the underlying protocol must be able to support the size of data you are sending. If the amount of data you are trying to send exceeds the packet size the send will fail. You need to ensure your data size is <= to the max packet size.

http://msdn.microsoft.com/en-us/library/ms740149(VS.85).aspx
For message-oriented sockets (address family of AF_INET or AF_INET6, type of SOCK_DGRAM, and protocol of IPPROTO_UDP, for example), care must be taken not to exceed the maximum packet size of the underlying provider. The maximum message packet size for a provider can be obtained by calling getsockopt with the optname parameter set to SO_MAX_MSG_SIZE to retrieve the value of socket option. If the data is too long to pass atomically through the underlying protocol, the error WSAEMSGSIZE is returned, and no data is transmitted.
0
 
CSecurityAuthor Commented:
Thanks... Now it works
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> Thanks... Now it works
For the curious among us, what was the problem/solution?
0
 
CSecurityAuthor Commented:
Content-Length in HTTP header :))
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> Content-Length in HTTP header :))
Hahahha :)

Sometimes, it's the simple things eh? Glad you solved it.

Merry xmas.
0
 
CSecurityAuthor Commented:
Yeah... Thanks you all for helping me out! Merry xmas
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.