Jump to content
  • Advertisement
Sign in to follow this  
MadMaxz5

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

This topic is 4851 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!