Jump to content
  • Advertisement
Sign in to follow this  
Thevenin

.NET Sockets implementation fault. (Solved)

This topic is 4700 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

So I'm currently going though my socket code trying to find a small bug thats causing my offset to be off by 1 byte, when I suddenly feel this code is quite hazardous...
    public void sReceiveCallback( IAsyncResult MyAsyncResult ) 
    {
        try
        {
            /* End the recieve so we can tally up the data read. */
            int TheBytesRead = MyGlobalSocket.EndReceive(MyAsyncResult);

            /* Increment the TCP offset. */
            TheTCPOffset += TheBytesRead;

            /* Get ready to recieve more data. */
            MyGlobalSocket.BeginReceive(TheTCPBuffer,TheTCPOffset, TheTCPStackSize - TheTCPOffset, SocketFlags.None,
                new AsyncCallback(sReceiveCallback), null);
        } 
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }
    }

... where the TheTCPBuffer is a linear byte array, TheTCPStackSize is an integer const containing the size of the array, and TheTCPOffset being the number of bytes used in the array. The problem I'm seeing is that... It will read in say, 512 bytes of data and the recieve function will be called again to store the next packet of data at index=512. Which is all good so far. However, now lets say my packet processing procedures are called. They will read in the 512 (assuming this is a complete packet) and shift the index downto 0. Now, the server sends us the next packet, and instead of being put at the index=0, its now being put in some absurdly random location (index=512). Am I right? Or am I going paranoid... [Edited by - Thevenin on January 28, 2006 3:57:59 PM]

Share this post


Link to post
Share on other sites
Advertisement
Oh, haha, ahhahahah!

Why don't I just put my sCheckForCompletePackets() function inside the function posted above; as opposed to putting it in my game loop.

I don't know why I couldn't see to do that earlier... >.<

Edit: Oh wow, this simple switch actaully fixed the 1 byte offset problem; I was quite sure this problem was irrelvent to it, but I guess not. Horray! [grin]

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!