Thread: Socket receives the same data twice (C++)

  1. #1
    Registered User
    Join Date
    Apr 2012

    Socket receives the same data twice (C++)

    I'm working on creating a maze game, where two players connect (one acts as host, the other the player). In this, I'm sending XML data as a string using the send() function. (I'm also using a pre-made Socket class, keeping in mind this is for non-profit activities, meaning it doesn't break the copyright.) Keep in mind the client & server are running on Windows 7 using the WinSock2.h package.

    The problem I'm encountering is fairly straightforward. I first send the Maze XML file, this reads properly and is able to save the maze in a series of tiles. After this, another XML file is sent, updating the position of the player (and enemy) of the other user's game. However, when I attempt to READ this line, it starts reading from the beginning of the buffer, and it seems as if the buffer isn't being cleared because it starts reading the Maze XML file again.

    Is there a way to clear the buffer that recv() uses? I can't think of any other reason why the Maze XML is being read twice, when it isn't being sent twice.

    Below is the code that receives the XML, character by character. This is the server's code, the client code just reverses the order of sending/receiving the data. Not sure if that's necessary or relevant.

    while (1) {    char r;
        switch(recv(s_, &r, 1, 0)) {
          case 0: // not connected anymore;
                  // ... but last line sent
                  // might not end in \n,
                  // so return ret anyway.
            return ret;
          case -1:
            return "";
    //      if (errno == EAGAIN) {
    //        return ret;
    //      } else {
    //      // not connected anymore
    //      return "";
    //      }
        ret += r;
        if (r == '<') {
            counter = 0;
            check = "";
        check += r;
        if (counter == 6 && == 0)
            return ret;
        //if (r == '\n')  return ret;
    And this is the code that sends/receives the different XML files.

    Socket* s=in.Accept();
    cout << "Accepted a Call from a Client." << endl;
    // Here is where we receive the first (Maze) XML File, and
    // send our maze as XML
    string mazeS = s->ReceiveLineMaze();
    TiXmlDocument testDoc;
    testDoc.Parse(mazeS.c_str(), 0, TIXML_ENCODING_UTF8);
    Tile** theirMaze = readXML(testDoc);
    TiXmlDocument theMaze = maze->mazeToXML();
    TiXmlPrinter printer;
    string toSend = printer.CStr();
    cout << toSend << endl;
    bool inOurMaze = false;
    while(boolValues->running) {
    // This next line is where I want to receive the update on position
    // but instead it reads the Maze XML file again, the one I read up
    // above
        string posReceive = s->ReceiveLineUpdate();
        TiXmlDocument theirPos;
          theirPos.Parse(posReceive.c_str(), 0, TIXML_ENCODING_UTF8);
                        ... This is where I process the update XML ...
        TiXmlDocument updatePos = maze->updatePositionXML();
        TiXmlPrinter printerPos;
        string posSend = printer.CStr();
    Any help is appreciated. If it isn't clear up top, let me summarize.

    I first swap an XML file that details the Maze itself. This works fine. Then I attempt to swap the update XML files, that update the player/enemy positions for the other user. But when I attempt to use recv(...), it starts to read the Maze file again, NOT the update file. It's...perplexing.

    Oh, and here's the send code (very simple):

    s += '\n';
    where s_ is the socket and s.c_str is the string that needs to be sent (in this case the different XML files).
    Last edited by SoupOrJuice; 04-15-2012 at 02:23 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Do you do anything like
    ret = "";
    in the receiver code before entering your recv loop?

    Not clearing out the old send / receive strings before appending more data to them (prior to calling send/recv) is one thing to check for.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Quote Originally Posted by Salem View Post
    Do you do anything like
    ret = "";
    in the receiver code before entering your recv loop?

    Not clearing out the old send / receive strings before appending more data to them (prior to calling send/recv) is one thing to check for.
    ret is a local variable within a function called ReceiveLine, it should be getting destroyed at the end of that method (when it is returned). What happens is, the second time that function is called, I go through the function and ret fills up with the XML again, it isn't already full.

  4. #4
    Registered User
    Join Date
    Jun 2005
    From the information you have provided, it is not even clear what the relationship between the first code segment and the second code segment is. Let alone possible to identify your problem.

    Try creating a small but complete code sample that exhibits your problem. By "complete" I mean a code fragment that may be compiled, linked, and run that exhibits the behaviour you describe.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. socket (recive data from httpd)
    By grytskiv in forum C Programming
    Replies: 1
    Last Post: 03-12-2011, 01:30 PM
  2. Cannot read incoming data from socket
    By fnoyan in forum C++ Programming
    Replies: 6
    Last Post: 03-06-2006, 02:42 AM
  3. jumble socket data
    By stormy in forum C Programming
    Replies: 10
    Last Post: 08-23-2005, 10:07 AM
  4. Client closes before server receives
    By LuckY in forum Networking/Device Communication
    Replies: 3
    Last Post: 07-06-2004, 04:08 PM
  5. Reverse-Engineering socket data
    By filler_bunny in forum A Brief History of
    Replies: 5
    Last Post: 04-04-2003, 04:05 AM