/    Sign up×
Community /Pin to ProfileBookmark

SOCKETS: handling multiplayer a game

if I want my server to communicate with two different clients at once and pass information between them, how do I get the two different sessions to share data without writing a file? in other words, if i have a replica of the classic Atari tank combat game and there are two players…

Player 1 (where session_id = ‘foo’)
Player 2 (where session_id = ‘bar’)

and they both access the socket i’ve created on port 1234 with my file ‘socket.php’, how do I let Player 1 know that Player 2 is shooting at him? Do I need to consume 2 ports for this or is 1 sufficient?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NevermoreApr 17.2004 — It would be possible to use two ports and pass data through them, however if your server supports it the easiest way would be to have them both downloading a file that contains instructions for the game, and which adds each instruction to the end.
Copy linkTweet thisAlerts:
@sneakyimpauthorApr 17.2004 — thanks for your response!

I'm hoping to make the communication with the server fairly quick....anything that requires the webserver to intermediate would probably be too slow.

i fully understand that the clients must share some kind of protocol to 1)minimize the amount of data that gets communicated and 2) to keep the data clean and avoid data corruption, delays, or any behavior that might interrupt data flow. i don't think it will be possible to share real-time game data (like location of the opponents' paddle in PONG, for instance) by writing it to a file that would be downloaded. it would simply be too slow.

i'm hoping to create a PHP script to create a socket that two clients might be able to access in order to communicate with each other.

I'm confused about sockets though. i've located some PHP code which creates sockets to listen for access requests on some port (e.g., 1234) but i am not sure at all how those sockets behave.

does the concept of a session apply where each client is in its own memory space or does everyone who accesses the socket have access to the same variable environment? how many people can access the socket? is access multiplexed? do i need to create some kind of access manager with semaphores and locking?

or do i need to create a separate socket for each client that connects?
×

Success!

Help @sneakyimp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...