unixloop.phpt 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. --TEST--
  2. Unix domain socket Loopback test
  3. --EXTENSIONS--
  4. sockets
  5. --SKIPIF--
  6. <?php
  7. if (substr(PHP_OS, 0, 3) == 'WIN') {
  8. die('skip.. Not valid for Windows');
  9. }
  10. ?>
  11. --FILE--
  12. <?php
  13. $sock_path = sprintf("/tmp/%s.sock", uniqid());
  14. if (file_exists($sock_path))
  15. die('Temporary socket already exists.');
  16. /* Setup socket server */
  17. $server = socket_create(AF_UNIX, SOCK_STREAM, 0);
  18. if (!$server) {
  19. die('Unable to create AF_UNIX socket [server]');
  20. }
  21. if (!socket_bind($server, $sock_path)) {
  22. die("Unable to bind to $sock_path");
  23. }
  24. if (!socket_listen($server, 2)) {
  25. die('Unable to listen on socket');
  26. }
  27. /* Connect to it */
  28. $client = socket_create(AF_UNIX, SOCK_STREAM, 0);
  29. if (!$client) {
  30. die('Unable to create AF_UNIX socket [client]');
  31. }
  32. if (!socket_connect($client, $sock_path)) {
  33. die('Unable to connect to server socket');
  34. }
  35. /* Accept that connection */
  36. $socket = socket_accept($server);
  37. if (!$socket) {
  38. die('Unable to accept connection');
  39. }
  40. socket_write($client, "ABCdef123\n");
  41. $data = socket_read($socket, 10, PHP_BINARY_READ);
  42. var_dump($data);
  43. socket_close($client);
  44. socket_close($socket);
  45. socket_close($server);
  46. @unlink($sock_path);
  47. ?>
  48. --EXPECT--
  49. string(10) "ABCdef123
  50. "