@@ -9,53 +9,66 @@ if (substr(PHP_OS, 0, 3) == 'WIN') {
9
9
die ('skip Not valid for Windows ' );
10
10
}
11
11
require 'ipv6_skipif.inc ' ;
12
+ ?>
12
13
--FILE--
13
14
<?php
14
- $ socket = socket_create (AF_INET6 , SOCK_DGRAM , SOL_UDP );
15
- if (!$ socket ) {
16
- die ('Unable to create AF_INET6 socket ' );
17
- }
18
- if (!socket_set_nonblock ($ socket )) {
19
- die ('Unable to set nonblocking mode for socket ' );
20
- }
21
- var_dump (socket_recvfrom ($ socket , $ buf , 12 , 0 , $ from , $ port )); // false (EAGAIN, no warning)
22
- $ address = '::1 ' ;
23
- socket_sendto ($ socket , '' , 1 , 0 , $ address ); // cause warning
24
- if (!socket_bind ($ socket , $ address , 1223 )) {
25
- die ("Unable to bind to $ address:1223 " );
26
- }
15
+ $ socket = socket_create (AF_INET6 , SOCK_DGRAM , SOL_UDP );
16
+ if (!$ socket ) {
17
+ die ('Unable to create AF_INET6 socket ' );
18
+ }
27
19
28
- $ msg = "Ping! " ;
29
- $ len = strlen ($ msg );
30
- $ bytes_sent = socket_sendto ($ socket , $ msg , $ len , 0 , $ address , 1223 );
31
- if ($ bytes_sent == -1 ) {
32
- die ('An error occurred while sending to the socket ' );
33
- } else if ($ bytes_sent != $ len ) {
34
- die ($ bytes_sent . ' bytes have been sent instead of the ' . $ len . ' bytes expected ' );
35
- }
20
+ if (!socket_bind ($ socket , '::1 ' , 0 )) {
21
+ die (sprintf (
22
+ 'An error occurred while binding socket: %s ' ,
23
+ socket_strerror (socket_last_error ($ socket ))));
24
+ }
36
25
37
- $ from = "" ;
38
- $ port = 0 ;
39
- socket_recvfrom ($ socket , $ buf , 12 , 0 ); // cause warning
40
- socket_recvfrom ($ socket , $ buf , 12 , 0 , $ from ); // cause warning
41
- $ bytes_received = socket_recvfrom ($ socket , $ buf , 12 , 0 , $ from , $ port );
42
- if ($ bytes_received == -1 ) {
43
- die ('An error occurred while receiving from the socket ' );
44
- } else if ($ bytes_received != $ len ) {
45
- die ($ bytes_received . ' bytes have been received instead of the ' . $ len . ' bytes expected ' );
46
- }
47
- echo "Received $ buf from remote address $ from and remote port $ port " . PHP_EOL ;
26
+ socket_getsockname ($ socket , $ address , $ port );
48
27
49
- socket_close ($ socket );
50
- --EXPECTF --
51
- bool (false )
28
+ $ msg = "Ping! " ;
29
+ $ len = strlen ($ msg );
30
+ $ sent = socket_sendto ($ socket , $ msg , $ len , 0 , $ address , $ port );
31
+ if ($ 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
+ }
52
40
53
- Warning: Wrong parameter count for socket_sendto() in %s on line %d
41
+ $ wants = $ len ;
42
+ $ recvd = 0 ;
43
+ $ buf = null ;
54
44
55
- Warning: socket_recvfrom() expects at least 5 parameters, 4 given in %s on line %d
45
+ while ($ recvd < $ len ) {
46
+ $ bytes = socket_recvfrom (
47
+ $ socket , $ buffering , $ wants , 0 , $ address , $ port );
56
48
57
- Warning: Wrong parameter count for socket_recvfrom() in %s on line %d
58
- Received Ping! from remote address ::1 and remote port 1223
59
- --CREDITS --
60
- Falko Menge <mail at falko-menge dot de>
61
- PHP Testfest Berlin 2009 -05 -0 9
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
+
65
+ if ($ recvd != $ len ) {
66
+ die (sprintf (
67
+ '%d bytes have been received instead of the %d bytes expected ' ,
68
+ $ recvd , $ len ));
69
+ }
70
+
71
+ echo "Received $ buf from remote address $ address and remote port $ port " . PHP_EOL ;
72
+ ?>
73
+ --EXPECTF--
74
+ Received Ping! from remote address %s and remote port %d
0 commit comments