transilvlad
transilvlad

Reputation: 14532

PHP socket alternate listen/connect

I have built a small PHP server/client code. When I say client server I mean it acts bought as client and a server alternating for 5 seconds in each mode.

Now the code runs on two servers and is triggered by a cron.

On rare occasions they manage to get in perfect sync with each other and they either establish a connection at the very last microsecond but by then the PHP code has already passed to the client mode or they never manage to establish a connection.

Before this whole dance starts they run some database queries to select some information that can be big or small and never identical on them so adding some randomness in the timings has only made this incidents happen more rarely but not disappear completely.

Anyone ever manage to do something like this successfully? How?

Upvotes: 0

Views: 290

Answers (1)

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84151

You have designed a race condition here. No matter how you try to synchronize these, you'll get in trouble eventually.

The way to solve this is to have each process acting as a servere all the time, and doing client functionality on demand.

Upvotes: 1

Related Questions