Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: List not being sent over socket correctly?
    primarykey
    data
    text
    <p>I have another socket issue. This is actually rather strange compared to my other issues. Ok so I have a client connected to a server which is sending data at a constant rate through an ObjectOutputStream and recieving data through an ObjectInputStream. I have a list filled with Player objects which contain the players coordinates, game mode, etc. When the client moves its player, the updated coordinates get sent to the server and that player's object in the list is updated. Well, is supposed to be updated... I print out the changed coordinates on the server side, but when I send the list to the player to print out, the coordinates haven't changed. Here is the player class:</p> <pre><code>public static class Player implements Serializable { private static final long serialVersionUID = 8841477887428285969L; public int x, y, gamemode, ID; public String name; public Player(int x, int y, int gamemode, int ID, String name) { this.x = x; this.y = y; this.gamemode = gamemode; this.ID = ID; this.name = name; } public void setPosition(int x, int y) { this.x = x; this.y = y; } } </code></pre> <p>Where server recieves updated coordinates and prints the before and after out:</p> <pre><code>Package.Client_Server main_packet = (Package.Client_Server) obj; System.out.println(S00.getPlayerByID(ID).x + ", " + S00.getPlayerByID(ID).y); S00.getPlayerByID(ID).setPosition(main_packet.x, main_packet.y); System.out.println(S00.getPlayerByID(ID).x + ", " + S00.getPlayerByID(ID).y); </code></pre> <p>And here is where the client prints out its own coordinates sent from the server. <strong>The problem is here...</strong> The updated coordinates are not displayed here but are still the old coordinates. Am I doing something wrong? The List still only has one object in it so it isn't just creating more objects somewhere...</p> <pre><code>Package.Server_Client main_packet = (Package.Server_Client) obj; ID = main_packet.client_ID; C00.players = main_packet.players; System.out.println(C00.players.get(ID).x + ", " + C00.players.get(ID).y); System.out.println(main_packet.players.get(ID).x + ", " + main_packet.players.get(ID).y); //I tried printing the list directly from the package as well but it isn't updated either... </code></pre> <p>Heck, I'll even show the package sent from the server to the client that the list is held in:</p> <pre><code>public static class Server_Client implements Serializable { private static final long serialVersionUID = 5198487406288606819L; public int client_ID; public int online; public List&lt;Player&gt; players; public Server_Client(int client_ID, int online, List&lt;Player&gt; players) { this.client_ID = client_ID; this.online = online; this.players = players; } } </code></pre> <p>My whole issue is that the updated coordinates are in the List of players on the server side but once the list is sent to the client, the updated data is no longer updated... It is the same it was before the whole process started. Is this a problem with how sockets work or am I just horribly stuck on a small error? The List can't be the problem because I exchanged it for an array of Player objects but it still wouldn't be read properly on the client side. Help?</p> <p>EDIT: This effect can also be seen because when another client connects to the server, client 1 cannot see client 2 because the list is not updated but client 2 can see client 1 as he was when he first connected to the server. The console of client 1 always says he is the only player connected (list has 1 item in it) while console 2 always says there are 2 players connected (even if client 1 has disconnected). Therefore, the list is not being updated. So what could cause this?</p> <p>And here is the method 'getPlayerByID(int ID)':</p> <pre><code>public static Package.Player getPlayerByID(int ID) { for(int i = 0; i &lt; players.size(); i++) { if(players.get(i).ID == ID) { return players.get(i); } } throw new NoSuchElementException("No player has the ID: " + ID); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload