G.Spansky
G.Spansky

Reputation: 902

Programming simple Java Socket Server Game with multiClients

Im trying to write a simple java application with multiClient Server. My first steps was successfull. I wrote simple apps which operate with multipleClients in console. Something like simple Chat.

Now Im working on another, more complex project. I wrote simple GUI program, which has Board with moving rectangles. Each Player is represented by his own rectangle which he can move across the Board. My goal is that if the other player will join the server, I'll be able to watch him on the board.

I also wrote Server program. When Server is running and Player starts the application, he is trying to connect to Game Server. If connection is successfull Server starts new Thread which represents each client (Player). Then Player receives uID from Server and then send his Player object to Server ArrayList which stores all connected players.

To this moment everything is fine, because transmission of data between the player and the server is invoked only once, in a specific order (get uID from server, then send Player object to Server ArrayList).

My problem starts here. I wrote other Server and Player methods to sending and recieving data.

Main program has it own gameThread with a simple gameloop. Player sending and recieving methods are in this gameloop.

On the other site I have clientThread which also has loop with Server sending and recieving methods.

I have this stack Trace when I run client (game):

  java.net.SocketException: Socket closed
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:116)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
    at java.io.ObjectOutputStream$BlockDataOutputStream.drain(ObjectOutputStream.java:1877)
    at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(ObjectOutputStream.java:1786)
    at java.io.ObjectOutputStream.<init>(ObjectOutputStream.java:247)
    at GUI.GameBoard.sendData(GameBoard.java:193)
    at GUI.GameBoard.run(GameBoard.java:74)
    at java.lang.Thread.run(Thread.java:745)
java.net.SocketException: socket closed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    at java.net.SocketInputStream.read(SocketInputStream.java:170)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at java.io.ObjectInputStream$PeekInputStream.read(ObjectInputStream.java:2313)
    at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2326)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2797)
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:802)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
    at GUI.GameBoard.getServerData(GameBoard.java:181)
    at GUI.GameBoard.run(GameBoard.java:75)
    at java.lang.Thread.run(Thread.java:745)

Im not closing any Socket. I guess that the problem may be synchronizing data transfer between server and client.

Any advices? Maybe some good tutorial (with sending Objects to Server in loop)?

Upvotes: 0

Views: 1380

Answers (1)

user207421
user207421

Reputation: 310893

  java.net.SocketException: Socket closed

This has exactly one meaning. You closed the socket and then continued to use it.

Bug in your code.

I'm not closing any Socket.

Yes you are. Possibly you are unaware that closing an input stream or output stream obtained from a socket closes the other stream and the socket.

I guess that the problem may be synchronizing data transfer between server and client.

Nothing to do with it.

Upvotes: 2

Related Questions