views:

192

answers:

2

Good time of the day, dear developers!

I am not any kind of network programming pro, but it happened that I have faced necessity to develop socket-server on php (no way for using Java) for flash multiplayer browser-game (standard features like locations, team battles, etc).


The main problem is that TCP is point-to-point protocol and it completely occupies given port.

Of course it is possible to create some kind of queue, which will manage connections to the socket, but this solution doesn't seems to be the fittest one.

It seems to me that using interval of "fair" ports (from 2000 to 2200, for example) is more fitting solution, because one request may take a lot of time to execute and players won't be happy to wait in queue.

But how can I implement this "port inteval" strategy? The solutions that I see are:

  • launcing php-script per every port (he-he, 2 hundreds of launched scripts!);

  • somehow forking the initial process to new processes (2 hundreds of processed? Not nice too), one for every port;

  • additional while-loop, which listens all the ports (looks very bad);
  • using threading or something like that (the problem is that php is single-threaded, as far as I know; pcntl?).

But somehow I don't like any of them, or at least don't know how to implement them in the best possible way.

What is the best existing strategy to handle multiple requests from multiple users per time unit without delays, and how to implement this strategy in php? We have our own Debian-server, so it is possible to use any required php extensions.

Any advice about development, planning and implementation of such kind of systems is highlhy appreciated.

Thank You!


Edit 1: By the way, I've forgotten to mention some extra details.

For example, if we are trying to develop chat application, we need some sort of fixed (I mean persistent) connections for each user. For example, we have 80 users in chat, and then one of them posted a message, which server tries to handle and send to all other connected users, also putting an entry to the history file or something like that.

In this situation polling server for new messages every 10 seconds from each of 80 users is craziness, so the need in persistent connection gets obvious. But I don't know what is the best way to implement something like this, considering that not all requests are handled instanlty.

+1  A: 

have you looked at http://www.smartfoxserver.com/ or http://www.electro-server.com/ ?

Daniel
They are Java-based. As I said, we are tied to php. :(
Mixo123
I think real time app would be hard to write in PHP.
Maciek Sawicki
+1  A: 

Best way for multi user communication for Flash is RTMP. Look for FMS or Red5. It's not in php, but I think this is correct way.

Maciek Sawicki
By the way, RTMP is made primarily for something like streaming videos, isn't it?As far as I understand, this feature results in overheads when used to deal with a plain text (or binary) data.
Mixo123
from Wikipedia: The RTMP defines several channels on which packets may be sent/received, and which operate independently of each other. For example, there is a channel dedicated for handling RPC requests and responses, a channel for video stream data, a channel for audio stream data, a channel for out-of-band control messages (fragment size negotiation, etc.), and so on.So it can be use for anything where two direction communication is needed. For example chat or multiplier game.
Maciek Sawicki