Jump to content
  • Advertisement
Sign in to follow this  
denebo

[C#] Sent & Received data

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

I ran into some trouble checking the received data (Server <-> Client) for my ORPG. For example when the client presses the right arrow, it sends a packet containing a "r" value to the server, and the server is supposed to approve it by sending it back, therefore allowing the player on the client to actually move. The problem is in the client when I receive the packet data and convert it into a string, I then check 'if(data == "r") { moveRight(); }' (etc. for all directions). The client always seems to return 'false' when it checks if data == "r", when data indeed IS == "r". I used Console.WriteLine(data) to check its value and it indeed is "r", I'm really confused. Client receive data and checking code -------------------------------------
    public void onDataReceive(IAsyncResult result)
    {
        try
        {
            Packet socketPacket = (Packet)result.AsyncState;
            int dataSize = 0;
            dataSize = socketPacket.socket.EndReceive(result);
            char[] chars = new char[dataSize + 1];
            System.Text.Decoder decoder = System.Text.Encoding.UTF8.GetDecoder();
            int charLen = decoder.GetChars(socketPacket.buffer, 0, dataSize, chars, 0);
            string fData = new string(chars);

            System.Console.WriteLine(fData); // this outputs r

            if(fData == "r")
                  mainPlayer.moveRight(); // this never happens even though fData IS EQUAL TO "r"!!!!!!!!!!

            waitForData(playerSocket);
        }
        catch (SocketException se)
        {
            MessageBox.Show("Lost connection to server.");
        }
    }

Share this post


Link to post
Share on other sites
Advertisement
Quote:
new char[dataSize + 1]

Your cvec will have a null on the back. You need to test for "r\0". Also, I hope this is UDP because in TCP the packets get run together and messages get conflated.

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!