forked from danopia/php-ircd
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_socket.php
More file actions
50 lines (42 loc) · 1.32 KB
/
test_socket.php
File metadata and controls
50 lines (42 loc) · 1.32 KB
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
<?php
echo "Testing socket creation...\n";
// Create socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
$errorCode = socket_last_error();
$errorMsg = socket_strerror($errorCode);
echo "Socket creation failed: {$errorCode} - {$errorMsg}\n";
exit(1);
}
echo "Socket created successfully\n";
// Set socket options
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
echo "Socket options set\n";
// Bind socket
if (!socket_bind($socket, '127.0.0.1', 6667)) {
$errorCode = socket_last_error();
$errorMsg = socket_strerror($errorCode);
echo "Socket bind failed: {$errorCode} - {$errorMsg}\n";
socket_close($socket);
exit(1);
}
echo "Socket bound successfully\n";
// Set socket to listen
if (!socket_listen($socket, 50)) {
$errorCode = socket_last_error();
$errorMsg = socket_strerror($errorCode);
echo "Socket listen failed: {$errorCode} - {$errorMsg}\n";
socket_close($socket);
exit(1);
}
echo "Socket listening successfully\n";
// Set non-blocking mode
socket_set_nonblock($socket);
echo "Socket set to non-blocking mode\n";
echo "Socket test completed successfully!\n";
echo "You can now try connecting with: telnet 127.0.0.1 6667\n";
// Keep the socket open for a few seconds to test
sleep(5);
socket_close($socket);
echo "Socket closed\n";
?>