File: socket_sentto_recvfrom_ipv6_udp.phpt

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (74 lines) | stat: -rw-r--r-- 1,816 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
--TEST--
Test if socket_recvfrom() receives data sent by socket_sendto() via IPv6 UDP
--EXTENSIONS--
sockets
--SKIPIF--
<?php

if (substr(PHP_OS, 0, 3) == 'WIN') {
    die('skip Not valid for Windows');
}
require 'ipv6_skipif.inc';
?>
--FILE--
<?php
$socket = socket_create(AF_INET6, SOCK_DGRAM, SOL_UDP);
if (!$socket) {
    die('Unable to create AF_INET6 socket');
}

if (!socket_bind($socket, '::1', 0)) {
    die(sprintf(
        'An error occurred while binding socket: %s',
        socket_strerror(socket_last_error($socket))));
}

socket_getsockname($socket, $address, $port);

$msg = "Ping!";
$len = strlen($msg);
$sent = socket_sendto($socket, $msg, $len, 0, $address, $port);
if ($sent === false) {
    die(sprintf(
        'An error occurred while sending to the socket: %s',
        socket_strerror(socket_last_error($socket))));
} else if ($sent != $len) {
    die(sprintf(
        '%d bytes have been sent instead of the %d bytes expected',
        $sent, $len));
}

$wants = $len;
$recvd = 0;
$buf   = null;

while ($recvd < $len) {
    $bytes = socket_recvfrom(
        $socket, $buffering, $wants, 0, $address, $port);

    if (($bytes === false) && ($errno = socket_last_error($socket))) {
        if ($errno = SOCKET_EAGAIN) {
            socket_clear_error($socket);
            continue;
        }

        die(sprintf(
            'An error occurred while sending to the socket: %s',
            socket_strerror($errno)));
    }

    $recvd += $bytes;
    $wants -= $bytes;
    $buf .= $buffering;
}

if ($recvd != $len) {
    die(sprintf(
        '%d bytes have been received instead of the %d bytes expected',
        $recvd, $len));
}

echo "Received $buf from remote address $address and remote port $port" . PHP_EOL;
?>
--EXPECTF--
Received Ping! from remote address %s and remote port %d