Multiplayer Java Stream-Socket
By : KARIM BAIDAR
The objective is to create a Multiplayer game
where two distinct users can play that game
from two distinct computers.
How It is Operated
Stream Sockets ?
• With stream sockets, a process establishes a
connection to another process
• Stream sockets are said to provide a
• 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
• Server Class(The server maintains the
information about the Surface Board and
Implementation of Server
• Via server we establish a connection using
• 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
• 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
• 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.
• It performs some operation based on that
• Returns a result to the requester after
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
Implementation of Client Cont..
• Each client applet maintains its own GUI version of
the surface board on which it displays the state of
• 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
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
• A Socket object connects a client to a server
by specifying the server name and port
number when creating the Socket object..
• Code and complete package is given.
• Contact me at firstname.lastname@example.org