PHP sockets problem -
hey guys, trying socket programming in php.
so running socket "server":
$address = '127.0.0.1'; $port = '9999'; $mastersocket = socket_create(af_inet, sock_stream, sol_tcp); socket_set_option($mastersocket, sol_socket, so_reuseaddr, 1); socket_bind($mastersocket, $address, $port); socket_listen($mastersocket, 5); $clientsocket = socket_accept($mastersocket);
so open ssh , run script. running, no errors.
then have php script attempts connect this:
$fp = fsockopen("me.com", 9999, $errno, $errstr, 30); fclose($fp);
but it's giving me:
warning: fsockopen(): unable connect me.com:9999 (connection refused)
how begin fix this?
you haven't finished listening socket sequence, need call socket_accept
accept new connections. there example in comments in php documentation.
$clients = array(); $socket = socket_create(af_inet,sock_stream,sol_tcp); socket_bind($socket,'127.0.0.1',$port); socket_listen($socket); socket_set_nonblock($socket); while(true) { if(($newc = socket_accept($socket)) !== false) { echo "client $newc has connected\n"; $clients[] = $newc; } }
Comments
Post a Comment