Socket receives the same data twice (C++)

This is a discussion on Socket receives the same data twice (C++) within the Networking/Device Communication forums, part of the General Programming Boards category; I'm working on creating a maze game, where two players connect (one acts as host, the other the player). In ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    2

    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.

    Code:
    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 && check.compare(ender) == 0)
        {
            
            return ret;
        }
        //if (r == '\n')  return ret;
        counter++;
      }
    And this is the code that sends/receives the different XML files.

    Code:
    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);
    
    
    testDoc.SaveFile("ServerTestDoc.xml");
    
    
    //testDoc.SaveFile("testXMLFromString.xml");
    Tile** theirMaze = readXML(testDoc);
    
    
    TiXmlDocument theMaze = maze->mazeToXML();
    //theMaze.SaveFile("ClientTestWrite.XML");
    TiXmlPrinter printer;
            
    theMaze.Accept(&printer);
    
    
    string toSend = printer.CStr();
    cout << toSend << endl;
    s->SendLine(toSend);
    
    
                
    //RENDER STUFF IN THIS LOOP
    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;
        updatePos.Accept(&printerPos);
    
    
        string posSend = printer.CStr();
        s->SendLine(posSend);
    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):

    Code:
    s += '\n';
    send(s_,s.c_str(),s.length(),0);
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    2
    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
    Posts
    6,252
    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%.

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, 12:30 PM
  2. Cannot read incoming data from socket
    By fnoyan in forum C++ Programming
    Replies: 6
    Last Post: 03-06-2006, 01: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 Cprogramming.com
    Replies: 5
    Last Post: 04-04-2003, 03:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21