Skip to main content
GameDev.net gamedev.net
🔒 Locked

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

Started by MadMaxz5 Aug 6, 2005 at 1:40 PM 3 replies 700+ views
Original Post
MadMaxz5
MadMaxz5
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; filled_to+=bytes_read; break; Any ideas? [Edited by - MadMaxz5 on August 7, 2005 8:43:05 AM]
iMalc
iMalc
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.
MadMaxz5
MadMaxz5
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]
MadMaxz5
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?
iMalc
iMalc
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][/code] or [source][/source] tags for posting code.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.