Sei sulla pagina 1di 3

Networked Java Game Protocol Specification

May 26, 2004

Upon startup, necessary status information is exchanged via TCP before the
start of the game. The TCP connection is closed when the setup is complete
and the UDP sockets for the exchange of game state are opened on both sides.

1 TCP Prologue
Server continuously listens on TCP port 10250 for new clients (multi-threaded).
• Client: “protocol: SpaceShooter client version 1.0”
• Server: “protocol: SpaceShooter server version 1.0”
• Client: “player: [player name]”
• Server: “player: name ok, ID [i], field size [x] [y]” or “player: name in use”
if it’s already taken. [i] is an identifier for the player, and [x] and [y] are
the size of the game field.
• Client: “port: [client UDP port]”
• Server: “port: [server UDP port]”
All strings should terminate with a newline character. The player ID is
an integer value. It is chosen by the server and should be different for each
client (i.e., start with random ID and increment by one for each new client).
The TCP connection is closed after this exchanges. As always, check that the
correct messages are exchanged and print error messages if not (e.g. wrong
protocol version, ...).
Server and client then open UDP sockets on the specified ports and start
sending updates.

2 Game Protocol (UDP)


The client sends an update whenever a user action happens (speed/heading
change, shot) and otherwise every 50ms (position). The update contains po-
sition, heading, and if the player is shooting or not (shotTime, see Space-
Ship.java). The server sends a combined update (above information for all

1
the spaceships) in a single packet every 50ms. This update interval should be
configurable for client as well as server.
Both client and server packets have a sequence number to detect packet loss
(note: they are independent; this is not used as an ACK). The sequence number
is increased by one for each consecutive message.

3 UDP Packet Format


General packet format:
packet size sequence number type unused data ...

• packet size: size of the packet including protocol header (2 bytes)


• sequence number: sequence number separate for client and server (2 bytes)
• type: type of the packet (1 byte)
• unused: just what is says (1 byte)

2 byte values are always first high-byte, then low-byte (network order; default
order in Java).

3.1 Client Update


Type: 0x10
Data:
• x-position of the player (2 bytes)
• y-position of the player (2 bytes)
• heading of the player (1 byte)
• shot of the player (1 byte) (for now: 0 = no shot, 1 = shot)

3.2 Server Update


Type: 0x20
Data:
• number of players (1 byte)
• ID of 1st player (1 byte)
• x-position of 1st player (2 bytes)

• y-position of 1st player (2 bytes)


• heading of 1st player (1 byte)

2
• shot time of 1st player (1 byte)
• status of 1st player (1 byte)
• score of 1st player (1 byte)

• ID of 2nd player (1 byte)


• x-position of 2nd player (2 bytes)
• y-position of 2nd player (2 bytes)
• heading of 2nd player (1 byte)
• shot time of 2nd player (1 byte)
• status of 2nd player (1 byte)
• score of 2nd player (1 byte)
• ...
Shot time specifies for how many milliseconds the shot should be displayed
at the client (0 = no shot). Status ranges from 0 to MAX HITS and counts the
number of hits. Once it reaches MAX SHOTS the player is dead. Score += 1
if the player hits another space ship. Score += 5 if this hit destroys the other
space ship.

3.3 Sever Error


Type: 0x50
Data:
• error number (1 byte)
• error message (string)

Potrebbero piacerti anche