Results 1 to 12 of 12
  1. #1
    Bear is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default Data loss when using sockets

    Hi, I am writing a game lobby for a upcoming turn based game, mostly for learning purposes.
    My problem is with sending data over sockets. For the most part It is working fine but in this one case I seam to lose data.

    One client has created a game lobby and others have joined. So now every client has a timer that sends a refresh request to the server.

    There is one server thread per client and it respond by sending information about this game, including what players are in it.
    (This information is located in a class "Lobby" with only static variables and static synchronized methods.)

    My problem is that this only seam to work once for every client/server pair. When entering a game the client gets all the correct info about the players in it. But if a new player enters the ones before him don't get his name and other data.

    When trying to debug this I added println()s to write out the names of clients, both in servers before sending and in clients upon reciving. I found that the servers send the full data but the clients only recive the data they fist got when entering the lobby.

    I have bolded the important parts and made the debbuging parts green.

    Any tips or sugestions would be higly regarded.
    Any information missing? Please ask.

    SEVER:

    else if(request.getType().equals(MessageType.REFRESH)) // Refresh Games
    {
    if(gameName == null)
    {
    ArrayList<String> array = new ArrayList<String>(Lobby.keySet());
    client.out().writeObject(new Message(MessageType.GAMENAMES, array ));
    }
    else
    {
    if (Lobby.get(gameName).started)
    {
    client.out().writeObject(new Message(MessageType.START, null));
    Lobby.get(gameName).releaseSem();
    System.out.println("Sent START msg!");
    }
    else
    {
    client.out().writeObject(new Message(MessageType.GAMEINFO, Lobby.getPublicInfo(gameName)));
    Iterator iter = Lobby.getPublicInfo(gameName).players().keySet().i terator();
    while(iter.hasNext())
    System.out.println(iter.next().toString());

    }
    }
    }


    CLIENT:

    else if (arg0.getSource() == timer)
    {
    server.out().writeObject(new Message(MessageType.REFRESH, null));
    Message answ = (Message) server.in().readObject();
    if(answ.getType().equals(MessageType.GAMENAMES))
    {
    this.listModel.clear();
    Iterator<String> iter = ((ArrayList<String>) answ.getData()).iterator();

    while(iter.hasNext())
    {
    this.listModel.addElement(iter.next());
    }
    }
    else if(answ.getType().equals(MessageType.GAMEINFO))
    {
    PublicGameInfo info = (PublicGameInfo) answ.getData();
    Iterator iter = info.players().keySet().iterator();
    while(iter.hasNext())
    System.out.println(iter.next().toString());

    players.setPlayers(info.players());
    this.validate();
    }
    else if(answ.getType().equals(MessageType.START))
    {
    /// Enter game world!!!!!
    }
    }
    Last edited by Bear; 07-07-2011 at 03:11 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default

    if a new player enters the ones before him don't get his name and other data.
    Sounds like a logic problem. Your code should consider that case and send data to the clients that have entered before the new client.
    the clients only recive the data they fist got when entering the lobby.
    You probably have to post some code that can compile and execute to test for this.
    Last edited by Norm; 07-07-2011 at 03:50 PM.

  3. #3
    Bear is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Sounds like a logic problem. Your code should consider that case and send data to the clients that have entered before the new client.
    That is the problem. I am using a timer in the clients to continualy request the info. The server then sends the correct info, but for some reason the clients get the old info every time.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default

    the clients get the old info every time.
    Is that old info being received or was it there on the client and being reused.
    Does your debug printout show what the server sends and what the client receives? And is it time stamped so you can correlate the printouts or can you run the server and a client on the same console so their print outs are sequenced naturally by time.
    Is the correct data sent and received? If it is not received, where does it go?

  5. #5
    Bear is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    Here is all source files if anyone would care to do the work.

    Thanks a lot in advance.
    Attached Files Attached Files

  6. #6
    Bear is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Is that old info being received or was it there on the client and being reused.
    Does your debug printout show what the server sends and what the client receives? And is it time stamped so you can correlate the printouts or can you run the server and a client on the same console so their print outs are sequenced naturally by time.
    Is the correct data sent and received? If it is not received, where does it go?
    My debug shows what the server has sent and what the client has recived. The prints are in different consoles. Isnt the in method for sockets "blocking" so the client printout must be after the server sent anything?

    The correct data is sent but the data recived is not the same. I know there must be some logical error there but i cant find it.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default

    The correct data is sent but the data recived is not the same
    Hard to imagine.
    Can you time stamp the data or the printouts to show when each was sent/received? Does the receiver immediately print out what was received?

  8. #8
    Bear is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    Ill try to do that.

  9. #9
    Bear is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    SERVER:
    2011-07-07 17:02:23.583
    Client1
    2011-07-07 17:02:29.57
    Client1
    Client2
    2011-07-07 17:02:32.743
    Client1
    Client2
    2011-07-07 17:02:35.57
    Client1
    Client2
    2011-07-07 17:02:38.743
    Client1
    Client2
    2011-07-07 17:02:41.57
    Client1
    Client2
    2011-07-07 17:02:44.743
    Client1
    Client2
    2011-07-07 17:02:47.57
    Client1
    Client2
    2011-07-07 17:02:50.743
    Client1
    Client2
    2011-07-07 17:02:53.57
    Client1
    Client2
    2011-07-07 17:02:56.743
    Client1
    Client2

    CLIENT1:
    2011-07-07 17:02:23.584
    Client1
    2011-07-07 17:02:29.57
    Client1
    2011-07-07 17:02:35.57
    Client1
    2011-07-07 17:02:41.57
    Client1
    2011-07-07 17:02:47.57
    Client1
    2011-07-07 17:02:53.57
    Client1

    Client2:
    2011-07-07 17:02:32.75
    Client1
    Client2
    2011-07-07 17:02:38.743
    Client1
    Client2
    2011-07-07 17:02:44.743
    Client1
    Client2
    2011-07-07 17:02:50.743
    Client1
    Client2
    2011-07-07 17:02:56.743
    Client1
    Client2

  10. #10
    Bear is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    I think these stamps look good except client1 is never getting client2 info.
    Last edited by Bear; 07-07-2011 at 05:10 PM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default

    Time for more debugging.

  12. #12
    Bear is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    Thanks for your help.
    I now create a time stamp in the server before i send the data. I send the Time stamp with the data and write out both in the servers console.

    So then the clients write out this time stamp and the recived data in the clients console.

    The recived time stamp is the same as the sent one but the userdata sent along it are not.

    I think ill give up for tonight.

Similar Threads

  1. Possible Loss of Precision?
    By jwb4291 in forum New To Java
    Replies: 6
    Last Post: 12-02-2010, 04:28 AM
  2. Possible loss of precision
    By jankidudel in forum New To Java
    Replies: 2
    Last Post: 08-07-2010, 10:15 AM
  3. Replies: 1
    Last Post: 01-09-2010, 02:16 PM
  4. data loss in the secure file transfer using java
    By antony75 in forum Networking
    Replies: 4
    Last Post: 02-09-2009, 06:56 AM
  5. Connection Loss
    By CrazyShells Slam in forum New To Java
    Replies: 0
    Last Post: 05-15-2008, 02:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •