?
Solved

do loop in read file and send socket

Posted on 2009-12-24
9
Medium Priority
?
505 Views
Last Modified: 2012-05-08
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?
0
Comment
Question by:CSecurity
  • 4
  • 3
  • 2
9 Comments
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 1200 total points
ID: 26120631
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
 
LVL 17

Author Comment

by:CSecurity
ID: 26120646
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
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 26120718
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 800 total points
ID: 26121763
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
 
LVL 17

Author Closing Comment

by:CSecurity
ID: 31669835
Thanks... Now it works
0
 
LVL 40

Expert Comment

by:evilrix
ID: 26121773
>> Thanks... Now it works
For the curious among us, what was the problem/solution?
0
 
LVL 17

Author Comment

by:CSecurity
ID: 26121790
Content-Length in HTTP header :))
0
 
LVL 40

Expert Comment

by:evilrix
ID: 26121793
>> Content-Length in HTTP header :))
Hahahha :)

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

Merry xmas.
0
 
LVL 17

Author Comment

by:CSecurity
ID: 26122024
Yeah... Thanks you all for helping me out! Merry xmas
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question