Hello all together

I'm currently trying to get a network game going.
I'm using Sockets and Objekt Serialization, because thats what we are doing in class and I need to use them later in exams.
After alot of working and trying out, I got the Server to send an Object and the Client to recieve it.
But for some miraculous reason, it will send the Object and recieve it, but will not update it.
So its something client sided. Because the server only recieves the old object.
So it will send the Player Object: with x 100 and y 100. Eventhough I updated it on the client side to eg x 140 and y 132
Oh and when your checking it, maybe someone could tell me why I get two connections when connecting to the server?
Thanks alot

Heres the code:
Server:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| import java.io.*; import java.net.*;
public class Server extends Thread {
private ServerSocket arrayServer;
public static void main(String argv[]) throws Exception { new Server(); }
public Server() throws Exception { arrayServer = new ServerSocket(9999); System.out.println("Server listening on port 4000."); this.start(); }
public void run() { while(true) { try { System.out.println("Waiting for connections."); Socket client = arrayServer.accept(); System.out.println("Accepted a connection from: "+ client.getInetAddress()); Connect c = new Connect(client); } catch(Exception e) {} } } }
class Connect extends Thread { private Socket client = null; private ObjectInputStream ois = null; private ObjectOutputStream oos = null; public Connect() {}
public Connect(Socket clientSocket) { client = clientSocket; try { ois = new ObjectInputStream(client.getInputStream()); oos = new ObjectOutputStream(client.getOutputStream()); } catch(Exception e1) { try { client.close(); }catch(Exception e) { System.out.println(e.getMessage()); } return; } this.start(); }
public void run() { while (true) { Object obj = null; try { obj = ois.readObject(); System.out.println((Player)obj); if (obj instanceof Player) { oos.writeObject((Player)obj); } } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } } |
Client (with Slick2D):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
| import java.awt.Point; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.net.Socket; import java.util.ArrayList;
import org.newdawn.slick.Color; import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics; import org.newdawn.slick.Image; import org.newdawn.slick.Input; import org.newdawn.slick.SlickException; import org.newdawn.slick.Sound; import org.newdawn.slick.geom.Circle; import org.newdawn.slick.geom.Rectangle; import org.newdawn.slick.particles.ConfigurableEmitter; import org.newdawn.slick.particles.ParticleEmitter; import org.newdawn.slick.particles.ParticleIO; import org.newdawn.slick.particles.ParticleSystem; import org.newdawn.slick.particles.effects.FireEmitter; import org.newdawn.slick.state.BasicGameState; import org.newdawn.slick.state.StateBasedGame; public class GameplayState extends BasicGameState { int stateID = -1; GameplayState( int stateID ) { this.stateID = stateID; } @Override public int getID() { return stateID; } GameClient client = null; Player me; ArrayList<Player> players = new ArrayList<Player>(); String debugStr = "";
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException { client = new GameClient(); me = new Player(0,100,100); me.nick = "ad"; } public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException { for (Player p: players) { g.drawRect(p.x, p.y, p.width, p.height); } g.drawString(debugStr, 10, 20); } public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException { Input input = gc.getInput(); if (input.isKeyDown(input.KEY_W)) { me.moveUp(); } if (input.isKeyDown(input.KEY_S)) { me.moveDown(); } if (input.isKeyDown(input.KEY_A)) { me.moveLeft(); } if (input.isKeyDown(input.KEY_D)) { me.moveRight(); } debugStr = me.toString(); client.sendTCP(me); } public class GameClient { private String host; private int port; private ReaderThread reader; private Socket socket; ObjectOutputStream oos = null; ObjectInputStream ois = null; public GameClient(){ starteClient("127.0.0.1",9999); }
public boolean starteClient(String host, int port){ try { socket = new Socket(host, port); this.host= host; this.port= port; oos = new ObjectOutputStream(socket.getOutputStream()); ois = new ObjectInputStream(socket.getInputStream()); reader = new ReaderThread(); reader.start(); return true; } catch (IOException e){ System.err.println(e); return false; } }
public void sendTCP(Object o){ try { oos.writeObject(o); } catch (IOException ex) { }
}
class ReaderThread extends Thread { public void run(){ while (true) { try { Player obj = (Player)ois.readObject(); boolean b = false; int i = 0; for (Player p: players) { if (p == obj) { b = true; players.set(i, obj); } i++; } if (!b) { players.add(obj); System.out.println("Recieved New Player"); } } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } }
public void beendeClient() { try { oos.close(); ois.close(); socket.close(); } catch (IOException e){ System.err.println(e); } } } } |
And the Player Class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| import java.awt.Rectangle; import java.io.Serializable;
public class Player implements Serializable {
int id; String nick;
float x,y; double aimAngle;
int width = 50, height = 50;
Rectangle r;
public Player(int id, float x, float y) { this.id = id; this.x = x; this.y = y; }
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getNick() { return nick; }
public void setNick(String nick) { this.nick = nick; }
public float getX() { return x; }
public void setX(float x) { this.x = x; }
public float getY() { return y; }
public void setY(float y) { this.y = y; }
public double getAimAngle() { return aimAngle; }
public void setAimAngle(double aimAngle) { this.aimAngle = aimAngle; }
public int getWidth() { return width; }
public void setWidth(int width) { this.width = width; }
public int getHeight() { return height; }
public void setHeight(int height) { this.height = height; }
public void moveUp() { y -= 1; } public void moveDown() { y += 1; }
public void moveLeft() { x -= 1; } public void moveRight() { x += 1; } public Rectangle getRect() { return new Rectangle((int)x,(int)y,width,height); }
public String toString() { return this.nick +";"+ this.x +";"+ this.y +";"+ this.aimAngle +";"+ this.width +";"+ this.height +";"+ this.id;
} } |