2

Code:

$str = "example";
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
          socket_connect($socket, "127.0.0.1", "2107");
          socket_listen($socket);
          socket_write($socket, $str, strlen($str));
$resp   = socket_read($socket, strlen($str), PHP_READ_NORMAL);
echo $resp;

Without socket_read it works perfect and i get the $str value in my console application as output (response from console application is then success). But so far i add socket_read the script is just loading and hanging up. And also i does not see the $str value in the console application although this should be done (with socket_write) before the socket is listining to the response. There is then just the notify in my console application that a connection has been established but it hanging up - without stream_read -> perfect.

Any ideas?

1 Answers1

1

Just looking at some of my old code I did this which seemed to work:

//for connecting
$serverConn = @stream_socket_client("tcp://127.0.0.1:2107", $errno, $errstr, 1);

//for sending
fwrite($serverConn, $message);


//for reading
$response = stream_get_contents($serverConn);

This assumes you already have some service running on that port.

Otherwise perhaps try using socket_bind instead of socket_connect

andrew
  • 9,313
  • 7
  • 30
  • 61