Reusable socket
I tryed to create a socket in php and reuse it from other process. I know this can be done with a daemon script but I want to do this without.
I created a socket and binded it to a specific port.
$sock = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option ($sock, SOL_SOCK开发者_JAVA技巧ET, SO_REUSEADDR, 1);
socket_bind ($sock, 'xx.xx.xx.xx', 10000);
socket_connect ($sock, $host, $port);
And from another php file I did the same thing. But the packets that I send from the 2 file are not "validated" by host. I sniffed all ports and I see that it uses same local and destination port. I don't understand where is the problem.
Can you help me with this? It's ok in any other programming language, or any other solution for this.
Andrew
Sockets are not symmetrical. The server side listens on a specific port for a client to conect - the client does not specify the local port - only the remote port and address. Its nothing to do with the language you implement it in.
There's a very good socket server implementation available at http://www.phpclasses.org/browse/package/5758.html with examples.
C.
You can't really use persistent sockets in php. When you execute a php file, a new process is created which cannot access the variables - or sockets - of a different php process so it won't know if there already exists a socket and just creates it.
精彩评论