Simple code that doesn't work.(could this be..)

Started by
3 comments, last by iMalc 18 years, 8 months ago
Hi all. Below is the bit of the code for reading from a socket. The commented out part works fine, i.e copying contents of the read socket buffer to the main http message buffer, and appends in on multiple passes. But the line above it, one with memcpy, is to my knowledge supposed to do the same, but it doesn't. The data it copies is all wrong. It gives not just offset, but completely wrong values. filled_to is set to zero for first pass. (WSAGETSELECTEVENT(lParam)) { case FD_READ: ZeroMemory(reading_buf, 256); bytes_read=recv(clientSocket,reading_buf,256,0); memcpy(http_msg+filled_to,reading_buf,bytes_read);// what's wrong here? //for (i=0; i<bytes_read; i++) http_msg[filledto+i]=reading_buf; filled_to+=bytes_read; break; Any ideas? [Edited by - MadMaxz5 on August 7, 2005 8:43:05 AM]
Advertisement
what types are http_msg and reading_buf?
If they're larger than 1 byte then the for loop will copy more than the memcpy because memcpy takes the number of bytes, not the number of elements.

There's also the difference between filled_to and filledto on those two lines, so that's probably your bug.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Nope, its a typo.(corrected) Both buffers have 1 byte elements and both run without errors. Its just the memcpy one produces wrong output, while being in principle identical to looped copying. I'm trying to see why.

[Edited by - MadMaxz5 on August 7, 2005 8:37:57 AM]
I think the reason memcpy doesn't work properly is because its being called in the WinProc callback. In WinProc reads from socket may be dealt in short interval from the queued massages and memcpy doen't have time to write for the first call and is immedaitely called the second time? Possible?
Quote:Original post by MadMaxz5
I think the reason memcpy doesn't work properly is because its being called in the WinProc callback. In WinProc reads from socket may be dealt in short interval from the queued massages and memcpy doen't have time to write for the first call and is immedaitely called the second time? Possible?
Nope.
I've just realised that your problem must be that recv is returning SOCKET_ERROR which happens to equal -1. Now memcpy's last parameter is probably unsigned, so you'll be telling it to copy roughly 4GB of memory.
The for-loop works because i would be declared as a (signed) int, and i < -1 is false initially, thus the loop is never entered.
So your solution is to check for SOCKET_ERROR.
if (bytes_read != SOCKET_ERROR){    memcpy(http_msg+filled_to,reading_buf,bytes_read);// what's wrong here?    filled_to+=bytes_read;}
btw, please use [code]<insert code here>[/code] or [source]<insert code here>[/source] tags for posting code.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement