1--TEST--
2Test if socket_recvfrom() receives data sent by socket_sendto() via IPv6 UDP
3--EXTENSIONS--
4sockets
5--SKIPIF--
6<?php
7
8if (substr(PHP_OS, 0, 3) == 'WIN') {
9    die('skip Not valid for Windows');
10}
11require 'ipv6_skipif.inc';
12?>
13--FILE--
14<?php
15$socket = socket_create(AF_INET6, SOCK_DGRAM, SOL_UDP);
16if (!$socket) {
17    die('Unable to create AF_INET6 socket');
18}
19
20if (!socket_bind($socket, '::1', 0)) {
21    die(sprintf(
22        'An error occurred while binding socket: %s',
23        socket_strerror(socket_last_error($socket))));
24}
25
26socket_getsockname($socket, $address, $port);
27
28$msg = "Ping!";
29$len = strlen($msg);
30$sent = socket_sendto($socket, $msg, $len, 0, $address, $port);
31if ($sent === false) {
32    die(sprintf(
33        'An error occurred while sending to the socket: %s',
34        socket_strerror(socket_last_error($socket))));
35} else if ($sent != $len) {
36    die(sprintf(
37        '%d bytes have been sent instead of the %d bytes expected',
38        $sent, $len));
39}
40
41$wants = $len;
42$recvd = 0;
43$buf   = null;
44
45while ($recvd < $len) {
46    $bytes = socket_recvfrom(
47        $socket, $buffering, $wants, 0, $address, $port);
48
49    if (($bytes === false) && ($errno = socket_last_error($socket))) {
50        if ($errno = SOCKET_EAGAIN) {
51            socket_clear_error($socket);
52            continue;
53        }
54
55        die(sprintf(
56            'An error occurred while sending to the socket: %s',
57            socket_strerror($errno)));
58    }
59
60    $recvd += $bytes;
61    $wants -= $bytes;
62    $buf .= $buffering;
63}
64
65if ($recvd != $len) {
66    die(sprintf(
67        '%d bytes have been received instead of the %d bytes expected',
68        $recvd, $len));
69}
70
71echo "Received $buf from remote address $address and remote port $port" . PHP_EOL;
72?>
73--EXPECTF--
74Received Ping! from remote address %s and remote port %d
75