Getting 'Address already in use' when I try to bind a socket to localhost in php -


i'm trying create socket connection php script local server (qt, qlocalserver) i'm having trouble creating connection on php side.

<?php error_reporting(e_all); ini_set('display_errors', 'on');   set_time_limit(0); ob_implicit_flush();  echo 'usr='. get_current_user().'<br/>';  $address = 'localhost'; $port = 4444; //different port numbers produce same result  if (($sock = socket_create(af_unix, sock_stream, 0)) === false)  {     echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";     exit(); }   if (socket_bind($sock, $address, $port) === false)  {     echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";     exit(); }  ... 

this results in

usr=root warning: socket_bind(): unable bind address [98]: address in use in /var/www/nbr/socket.php on line 28 socket_bind() failed: reason: address in use

i've tried number of things give indications of problem may be, not how resolve it. socket_getsockname produces garbage when try echo address , port information, if change af_unix af_inet, , add

$addr = ""; $pt = "";     echo "socket name ok: " . socket_getsockname($sock, &$addr, &$pt) . '<br/>'; echo $addr .    ", " . $pt . '<br/>'; 

the result is

socket name ok: 1

0.0.0.0, 0

so address/port never set somehow? also, subsequent socket_get_option($sock, 0, so_reuseaddr) fails af_unix, succeeds af_inet still address unavailable error.

what doing wrong?

socket_bind used bind socket on local machine. binding socket means reserve address/port socket. use listener (server) not client. in case, since server (qt) started, address in use, socket_bind fail.

if want connect socket, use socket_connect:

$socket = socket_create(af_inet, sock_stream, sol_tcp); if (!socket_connect($socket, 'localhost', 4444)) {     die('failed'); } 

if want connect local socket (i.e.: through socket file, , not through tcp/udp):

$socket = socket_create(af_unix, sock_stream, 0); if (!socket_connect($socket, '/var/run/mysqld/mysqld.sock')) {     die('failed'); } 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -