Thread: recving gibberish...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267

    recving gibberish...

    I posted this question on GameDev, the threads been going on for 20 posts or so and I still couldn't get it fixed...

    It's my first time using async sockets
    I'm writing a game and I'm trying to make it send a message whenever the opponent moves
    I hardcoded it to send "mma" for debugging purposes and I'm recving the "mma"'s but once in a while, I get these strings of random(ish) chars within the "mma" string

    Screen shot
    the top right window is the data I'm recving
    I put a putchar('\n'); after each recv call

    I'm outputing the data right after I recv it so I know it's got nothing to do with not processing it properly
    It seems to have something to do with the recvbuffer's size

    What happens if the data received is larger than your buffer?
    Does recv stop recving once it encounters a \0?

    [edit]I just counted the chars, I get the D-" every 255th char and the recvbuffer is an array of 256 chars[/edit]
    Last edited by h_howee; 09-17-2007 at 12:11 PM.

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    What does your recv code look like?

  3. #3
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    Code:
        char temprecv[256] = {0};
        int BytesSent;
    
            case FD_READ:
            {
                if (recv(wParam, temprecv, sizeof(temprecv), 0) == 0)
                {
                    std::cout<<"You opponent has left the game."<<std::endl;
                    PostQuitMessage(0);
                }
                std::cout<<temprecv<<std::endl;
                std::string a(temprecv);
                for (int i = 0; i < a.size(); i++)
                {
                    RecvBuffer.push_back(a[i]);
                }
                this->ProcessRecv();
                break;
            }

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  4. #4
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    damnit, I always find the solution right after i post here...

    Code:
            case FD_READ:
            {
                BytesSent = recv(wParam, temprecv, sizeof(temprecv), 0);
                if (BytesSent == 0)
                {
                    std::cout<<"You opponent has left the game."<<std::endl;
                    PostQuitMessage(0);
                }
                for (int i = 0; i < BytesSent; i++)
                {
                    RecvBuffer.push_back(temprecv[i]);
                }
                this->ProcessRecv();
                break;
            }

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Yep. Thats what I though the problem was also.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using inFile to read from TXT yields gibberish
    By Zzaacchh in forum C++ Programming
    Replies: 4
    Last Post: 08-05-2008, 07:59 PM
  2. How to avoid gibberish?
    By duffmckagan in forum C Programming
    Replies: 8
    Last Post: 06-24-2006, 10:53 AM
  3. Why is it gibberish?
    By ober in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-10-2005, 01:24 PM