Multiplayer Java Stream-Socket
Game

By : KARIM BAIDAR
Objective
The objective is to create a Multiplayer game
where two distinct users can play that game
from two distinct computers.
How It is Operated

CLIENT

CLIENT

SERVER
PLAYER ‘O’

PLAYER ‘X’
Stream Sockets ?
• With stream sockets, a process establishes a
connection to another process
• Stream sockets are said to provide a
connection-oriented service
• The protocol used for transmission is the
popular TCP (Transmission Control Protocol)
Class Framework of Game
• Box-listener Class ( Listens to user click Events
on the surface board)

• Box Class (Implements each of the nine
square boxes on the surface board)
• Client Class (Maintains its own GUI version)
Class Framework Cont..
• Player Class(Enable the clients to play the
game independently)
• Server Class(The server maintains the
information about the Surface Board and
Clients)
Implementation of Server
• Via server we establish a connection using
ServerSocket()
• The server maintains the information about
the surface board so it can determine whether
a player’s move is a valid or invalid move.
Implementation of Server Cont..
• The game main logic is placed in Server class
• Server class is the Main Class
• Server waits for connection to connect two
clients
ServerSocket
• ServerSocket(Port Number , Clients);
• Implements a socket that servers can use to
listen for and accept connections to clients
• A ServerSocket object establishes the port
where a server waits for connections from
clients.
ServerSocket Cont..
• The server assigns X to the first client to
connect and O to second respectively
• The second argument to the ServerSocket
constructor specifies the number of clients
that can wait for a connection and be
processed by the server.
ServerSocket Cont..
• It performs some operation based on that
request

• Returns a result to the requester after
processing
Implementation of Client
• Sockets are initialized in Client as below
– Socket connection = new Socket( server Address, port );
– Gets the socket's input and output stream and opens
readers and writers on them.

• Player X and Player O used in the game are
both clients
Implementation of Client Cont..
• Each client applet maintains its own GUI version of
the surface board on which it displays the state of
the game
• The clients can place a mark only in an empty square
on the board
Implementation of Players
• Runs a thread to handle Multi Players
• Gets the socket's input and output stream and opens readers
and writers on them.

• Show messages from both Players X and O
perspectives.
ServerSocket Client Server
Why we used Sockets in the Game ?
• A socket is one endpoint of a two-way
communication link between two programs
running on the network
• Stream-based connections are managed with
Socket objects.
• A Socket object connects a client to a server
by specifying the server name and port
number when creating the Socket object..
Note
• Code and complete package is given.
• Contact me at karimbaidar@yahoo.com

Multiplayer Java Game

  • 1.
  • 2.
    Objective The objective isto create a Multiplayer game where two distinct users can play that game from two distinct computers.
  • 3.
    How It isOperated CLIENT CLIENT SERVER PLAYER ‘O’ PLAYER ‘X’
  • 4.
    Stream Sockets ? •With stream sockets, a process establishes a connection to another process • Stream sockets are said to provide a connection-oriented service • The protocol used for transmission is the popular TCP (Transmission Control Protocol)
  • 5.
    Class Framework ofGame • Box-listener Class ( Listens to user click Events on the surface board) • Box Class (Implements each of the nine square boxes on the surface board) • Client Class (Maintains its own GUI version)
  • 6.
    Class Framework Cont.. •Player Class(Enable the clients to play the game independently) • Server Class(The server maintains the information about the Surface Board and Clients)
  • 7.
    Implementation of Server •Via server we establish a connection using ServerSocket() • The server maintains the information about the surface board so it can determine whether a player’s move is a valid or invalid move.
  • 8.
    Implementation of ServerCont.. • The game main logic is placed in Server class • Server class is the Main Class • Server waits for connection to connect two clients
  • 9.
    ServerSocket • ServerSocket(Port Number, Clients); • Implements a socket that servers can use to listen for and accept connections to clients • A ServerSocket object establishes the port where a server waits for connections from clients.
  • 10.
    ServerSocket Cont.. • Theserver assigns X to the first client to connect and O to second respectively • The second argument to the ServerSocket constructor specifies the number of clients that can wait for a connection and be processed by the server.
  • 11.
    ServerSocket Cont.. • Itperforms some operation based on that request • Returns a result to the requester after processing
  • 12.
    Implementation of Client •Sockets are initialized in Client as below – Socket connection = new Socket( server Address, port ); – Gets the socket's input and output stream and opens readers and writers on them. • Player X and Player O used in the game are both clients
  • 13.
    Implementation of ClientCont.. • Each client applet maintains its own GUI version of the surface board on which it displays the state of the game • The clients can place a mark only in an empty square on the board
  • 14.
    Implementation of Players •Runs a thread to handle Multi Players • Gets the socket's input and output stream and opens readers and writers on them. • Show messages from both Players X and O perspectives.
  • 15.
  • 16.
    Why we usedSockets in the Game ? • A socket is one endpoint of a two-way communication link between two programs running on the network • Stream-based connections are managed with Socket objects. • A Socket object connects a client to a server by specifying the server name and port number when creating the Socket object..
  • 17.
    Note • Code andcomplete package is given. • Contact me at karimbaidar@yahoo.com