How do I set the timeout on a socket [duplicate]

3 weeks ago 20
ARTICLE AD BOX

I'm trying to make a web page that can query a game server for call of duty 2, i seem to get issues with create a socket_create() im using php 8.3 the error i get is:

Fatal error: Uncaught TypeError: socket_set_timeout(): Argument #1 ($stream) must be of type resource, Socket given in E:\laragon\www\index.php:19 Stack trace: #0 E:\laragon\www\index.php(19): socket_set_timeout(Object(Socket), 2) #1 E:\laragon\www\index.php(100): getServerInfo('116.203.79.194', '28960') #2 {main} thrown in E:\laragon\www\index.php on line 19

this is line 13 causing the error

socket_set_timeout($socket, 2);

full code can anyone tell me what im doing wrong to understand this error please.

<?php function getServerInfo($ip, $port) { // Validate IP address and port if (!filter_var($ip, FILTER_VALIDATE_IP)) { return "Invalid IP address."; } if (!is_numeric($port) || $port < 1024 || $port > 65535) { return "Invalid port number (must be between 1024 and 65535)."; } // Create UDP socket $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); if ($socket === false) { return "Failed to create socket: " . socket_strerror(socket_last_error()); } // Set timeout (e.g., 2 seconds) socket_set_timeout($socket, 2); // Build the query packet $header = pack("H*", "3F1C0000"); // Header $queryType = chr(0x65); // GetServerInfo query type $dataLength = pack("n", 0); // Data length (short, little-endian) // Calculate checksum $packetData = $header . $queryType . $dataLength; $checksum = calculateChecksum($packetData); $packetData .= pack("n", $checksum); // Checksum (short, little-endian) // Send the query packet if (@socket_sendto($packetData, strlen($packetData), SOCK_DGRAM, $ip, $port) === false) { return "Failed to send query: " . socket_strerror(socket_last_error()); } // Receive the response $response = ""; if (@socket_recvfrom($socket, $response, 1024, 0, $ip, $port) === false) { return "Failed to receive response: " . socket_strerror(socket_last_error()); } // Close the socket socket_close($socket); // Parse the response (very basic example - needs improvement!) if ($response) { $decoded = unpack("H*", $response); // Decode hex string return "<pre>" . print_r($decoded, true) . "</pre>"; // Display raw hex data for now. } else { return "No response received."; } } function calculateChecksum($data) { $checksum = 0; for ($i = 0; $i < strlen($data); $i++) { $checksum += ord($data[$i]); } $checksum = ~$checksum & 0xFFFF; // One's complement return $checksum; } ?> <!DOCTYPE html> <html> <head> <title>CoD2 Server Query</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> body { font-family: sans-serif; } form { margin: 20px; padding: 15px; border: 1px solid #ccc; border-radius: 5px; } input[type="text"] { width: 200px; padding: 8px; margin-bottom: 10px; } button { background-color: #4CAF50; color: white; padding: 10px 15px; border: none; cursor: pointer; border-radius: 5px; } button:hover { background-color: #3e8e41; } #result { margin-top: 20px; padding: 10px; border: 1px solid #eee; border-radius: 5px; } /* Responsive Design */ @media (max-width: 600px) { form { width: 90%; } input[type="text"] { width: 100%; } } </style> </head> <body> <h1>CoD2 Server Query</h1> <form method="post"> IP Address: <input type="text" name="ip"><br> Port: <input type="text" name="port"><br> <button type="submit">Query Server</button> </form> <?php if ($_SERVER["REQUEST_METHOD"] == "POST") { $ip = $_POST["ip"]; $port = $_POST["port"]; echo "<div id='result'>"; echo getServerInfo($ip, $port); echo "</div>"; } ?> </body> </html>
Read Entire Article