Click here to Skip to main content
16,016,570 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: need help in CListCtrl Pin
firebolt7723-Aug-05 19:42
firebolt7723-Aug-05 19:42 
GeneralRe: need help in CListCtrl Pin
ThatsAlok23-Aug-05 19:58
ThatsAlok23-Aug-05 19:58 
GeneralUpdating a field in Access database Pin
joy00722-Aug-05 18:12
joy00722-Aug-05 18:12 
GeneralRe: Updating a field in Access database Pin
Jose Lamas Rios22-Aug-05 18:27
Jose Lamas Rios22-Aug-05 18:27 
GeneralRecieving a file over TCP Pin
Member 183908222-Aug-05 17:56
Member 183908222-Aug-05 17:56 
GeneralRe: Recieving a file over TCP Pin
Jose Lamas Rios22-Aug-05 18:04
Jose Lamas Rios22-Aug-05 18:04 
GeneralRe: Recieving a file over TCP Pin
Member 183908222-Aug-05 18:13
Member 183908222-Aug-05 18:13 
GeneralRe: Recieving a file over TCP Pin
Jose Lamas Rios22-Aug-05 19:10
Jose Lamas Rios22-Aug-05 19:10 
farklem@gmail.com wrote:
I get the error: "Access violation reading location 0xcccccccc"

Which still doesn't give us much information...

What happens when you break into the debugger after the exception? What is shown in the call stack? At what point in your code was the problem?

In any case, looking again at your code, I noticed this:
while(recv(ClientSocket, xrec, 2048, 0) > 0)
{
   FILE *fp;
   fp = fopen("C:\\TestFile.dat", "ab");
   fprintf(fp, xrec);
   fclose(fp);
 
   memset(xrec, 0x0, 1025);
}

If you are receiving binary data, it doesn't make sense to use fprintf, which will treat xrec as a null-terminated char string. It will stop at the first byte with zero. It may encounter a zero way before the end of the buffer, or worse, way after the end. Further, if it happens to find what it thinks is a format specification (e.g.: %d, %s, etc.) it will try to extract an additional parameter from the stack and very bad things could happen. Even if you are using this for text files, I don't think recv will put a null terminator after each fragment it reads.

You should a) use the return value from the recv function to know exactly how many bytes were copied to the buffer (and so how many to write to the local file), and b) write to the file with a function that can handle binary data, like fwrite(xrec, 1, nBytesReceivedWithRecvFunction, fp).

Also, avoid using "magic numbers" (like 2048, 1025, etc.), specially if you use them more than once in the code. Use defines, or better yet, const int variables. That way, if you need to change a value, you can be sure the new value will be used everywhere it's needed. For example, why are you reading from the socket up to 2048 bytes in each iterations and then resetting the memory for the first 1025? I don't think you need to reset the buffer before each read, but if you actually needed it, you would be doing it wrong. Seems like you changed the buffer size and forgot to update the resetting?

Those problems can be avoided if you simply declare a const int nBufLen = 2048; and use nBufLen wherever you need to refer to the buffer size.

Hope that helps,

--
jlr
http://jlamas.blogspot.com/[^]
GeneralRe: Recieving a file over TCP Pin
Member 183908223-Aug-05 7:44
Member 183908223-Aug-05 7:44 
GeneralRe: Recieving a file over TCP Pin
Member 183908223-Aug-05 17:10
Member 183908223-Aug-05 17:10 
Questionhow to create a listbox using GLUI. Pin
jasrina22-Aug-05 17:45
jasrina22-Aug-05 17:45 
AnswerRe: how to create a listbox using GLUI. Pin
Jose Lamas Rios22-Aug-05 18:02
Jose Lamas Rios22-Aug-05 18:02 
GeneralTree List Control Pin
Nishad S22-Aug-05 17:45
Nishad S22-Aug-05 17:45 
GeneralRe: Tree List Control Pin
Jose Lamas Rios22-Aug-05 18:08
Jose Lamas Rios22-Aug-05 18:08 
QuestionHow to make text link in dialog box with Win32api? Pin
Member 159461422-Aug-05 16:55
Member 159461422-Aug-05 16:55 
AnswerRe: How to make text link in dialog box with Win32api? Pin
Jose Lamas Rios22-Aug-05 17:05
Jose Lamas Rios22-Aug-05 17:05 
GeneralRe: How to make text link in dialog box with Win32api? Pin
Member 159461422-Aug-05 18:01
Member 159461422-Aug-05 18:01 
GeneralRe: How to make text link in dialog box with Win32api? Pin
Jose Lamas Rios22-Aug-05 18:05
Jose Lamas Rios22-Aug-05 18:05 
QuestionInternet game? Pin
gr8coaster32922-Aug-05 16:14
gr8coaster32922-Aug-05 16:14 
QuestionHow can i chang the type of int to type of POSITION Pin
ebinaini22-Aug-05 15:14
ebinaini22-Aug-05 15:14 
AnswerRe: How can i chang the type of int to type of POSITION Pin
Anonymous22-Aug-05 15:33
Anonymous22-Aug-05 15:33 
AnswerRe: How can i chang the type of int to type of POSITION Pin
Jose Lamas Rios22-Aug-05 15:40
Jose Lamas Rios22-Aug-05 15:40 
GeneralRe: How can i chang the type of int to type of POSITION Pin
ebinaini23-Aug-05 15:25
ebinaini23-Aug-05 15:25 
AnswerRe: How can i chang the type of int to type of POSITION Pin
Christian Graus22-Aug-05 15:59
protectorChristian Graus22-Aug-05 15:59 
GeneralRe: How can i chang the type of int to type of POSITION Pin
David Crow23-Aug-05 3:29
David Crow23-Aug-05 3:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.