1

Possible Duplicate:
A PHP Socket Server with Flash Clients

I am building an app in my server with the help of a flash developer, and he asked me to build a socket server to communicate with the database. He recommended me JAVA but I am not very good at JAVA and I was wondering if it was possible to build a Socket Server in PHP.

I should allow connections to multiple TCP client connections. I know in JAVA this is done thought threading, but I am not sure if this can also be achieved with PHP.

Could someone please show me the basic skeleton of a PHP Socket Server with these characteristics?

The connection has to be TCp (persistent) from the beginning of the connection to the app, until the end.

Community
  • 1
  • 1
Enrique Moreno Tent
  • 24,127
  • 34
  • 104
  • 189
  • 1
    Yes, php supports sockets but not multithreading. If you are free in your choice, I would not recommend building socket server with PHP. It's quite big headache. Java or C# would be best choices here, IMO. Anyway, [here's link to manual](http://php.net/manual/en/book.sockets.php). – Leri Oct 05 '12 at 09:33
  • I've a bad experience with socket server in php. It's possibile to create a multiclient server but it's very limited. I recommend you to use nodejs or Haxe/Neko target – Luca Rainone Oct 05 '12 at 09:35
  • PS: Haxe has a syntax like ActionScript... so your friend flash developer can do it easily http://haxe.org/doc/flash/chat – Luca Rainone Oct 05 '12 at 09:37
  • Have you looked at [React](http://nodephp.org/)? – Leigh Oct 05 '12 at 09:57
  • The question isnt about flash, the question is how to build a PHP Socket Server with persistent connections to multiple clients – Enrique Moreno Tent Oct 05 '12 at 10:31

3 Answers3

2

You have to run your socket server as a service from the command line. This is a part of what I have used before. It closes the socket after a read but can easy be modified to keep a array of connections.

  • You would have to build some kind of watchdog to see if a connection is still alive.
  • You need a identifying mechanism to identify different connections.

The code:

set_time_limit( 0 );
// Set the ip and port we will listen on
$address = '127.0.0.1';
$port = 6789;

// Create a TCP Stream socket
$sock = socket_create( AF_INET, SOCK_STREAM, 0 ); // 0 for  SQL_TCP
// Bind the socket to an address/port
socket_bind( $sock, 0, $port ) or die( 'Could not bind to address' );  //0 for localhost
// Start listening for connections
socket_listen( $sock );

//loop and listen
while (true) {
  /* Accept incoming  requests and handle them as child processes */
  $client = socket_accept( $sock );
  // Read the input  from the client – 1024000 bytes
  $input = socket_read( $client, 1024000 );

  // from here you need to do your database stuff
  // and handle the response 

   // Display output  back to client
  socket_write( $client, $response );
  socket_close( $client );
}
// Close the master sockets
socket_close( $sock );
JvdBerg
  • 21,777
  • 8
  • 38
  • 55
  • I forgot to say I need it to be a persistent connection. It not a simple ask->answer->close connection. sorry. – Enrique Moreno Tent Oct 05 '12 at 09:40
  • Even thought the kind of protocol is TCP, you treat it as a UDp. You basically wait for the client to connect, for him to say something, give him an answer and close the connection. If he wants something else, he needs to connect again. I never said that the client wanted to make requests. The connection I want to stablish is to keep informed the clients of the updates in the DB. Meaning that it is the client who is waiting for messages for the server, without sending any request. – Enrique Moreno Tent Oct 05 '12 at 09:50
  • What you want to do is send push messages to a client without clients having to connect first. I think that cannot be done. – JvdBerg Oct 05 '12 at 10:16
  • That is definitely the very backbone logic for a socket server in PHP, but it won't make client connections persistent, and I am afraid, if made persistent, which I already did in my application, PHP seems encounter FILE DESCRIPTOR limit for more than 1024 concurrent client connections! Any luck around that? – Broken Arrow Nov 29 '22 at 18:56
0

There is a WebSocket Server and Client library for PHP At Google code . It supports flash clients . Not sure whether it solves your problem .

If you want a basic tutorial here is the link to learn

How to create a socket server in php

EDIT :- after looking at your comment ( running a socket server continuously as a service )

Here is the link which describes the way of creating a socket server and running as a process

Create a socket server in php and run as a service

Aravind.HU
  • 9,194
  • 5
  • 38
  • 50
0

Rather than building a "socket server", you should really build a set of web APIs (SOAP, REST/JSON, whatever) that provides limited and well-defined access to the DB. Then have the Flash app use that.

Your flash app sends JSON over a RESTful interface, or SOAP/XML requests. The server receives them, interacts appropriately with the database, and returns any required results again as XML or JSON.

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778