php - connect to udp server - php

I am about to write a function which returns true or false. If the function is able to connect to the game server (doesn't need anything further, just to check if it's running) via UDP and a specific ip, it'll return true. If not, it'll return false. Unfortunately, I didn't find anything useful by searching the web.
Edit: For TCP, I'm using this and it's working like a charm.
function loginServer()
{
$ipAddr = "localhost";
$port = 1234;
$fp = fsockopen($ipAddr, $port, $errno, $errstr);
if(!$fp)
{
return false;
}
else
{
fclose($fp);
return true;
}
}

Related

PHP stream_select alwasy returns with server socket

When using stream_select() on an array which contains the server socket created with stream_socket_server() and also the client sockets it always returns instantly with the server socket selected.
My understanding of stream_select() is that it should only return when the server socket state has changed (or timeout is reached). This should only happen if a new client is trying to connect or one client has sent data.
I have written the following code:
$socket = stream_socket_server("tcp://localhost:8888");
$connections['master'] = $socket;
while(true) {
$w = $e = null;
$r = $connections;
if (stream_select($r, $w, $e, 1) !== false) {
foreach ($r as $socket) {
if ($socket === $connections['master']) {
$conn = stream_socket_accept($socket);
$connections[] = $conn;
} else {
// Handle communication with client...
}
}
}
}
With this, I get a non-stop running loop where $r everytime contains the server socket, but stream_socket_accept() throws an error because there is no client trying to connect.
What am I doing wrong? Or is my understanding about stream_select() wrong?

PHP fsocketopen - returns false when port is open

I made this code which is supposed to show whether the program running on the specified port is running.
Now the problem is that it shows "Offline" when my port is clearly open. It however does show online for Google on port 80. What am I doing wrong?
$server = '77.251.97.234';
$port = 43594;
$timeOut = 1;
if ($socket = #fsockopen($server, $port, $errorNo, $errorStr, $timeOut)) {
echo '<span style="color:green;">Online</span>';
} else {
echo '<span style="color:red;">Offline</span>';
}

Query if Shoutcast Server is Offline or Online with PHP

I'm trying to use a script to query if a Shoutcast Server is online or offline. The code below is what I'm using at the moment.
$vt_ip = "ip";
$vt_port = "port";
$output = #fsockopen($vt_ip, $vt_port, $errno, $errstr, 2);
if (!$output) {
echo "<FONT CLASS=f1 COLOR=#DD0000><B>OFFLINE</B></FONT>";
} else {
echo "<FONT CLASS=f1 COLOR=#00DD00><B>ONLINE</B></FONT>";
}
#fclose($output);
But it doesn't update, it is stuck on Offline status.
Any help would be greatly appreciated.
$vt_ip = "ip";
$vt_port = "port";
$conn = fsockopen($vt_ip, $vt_port, $errno, $errstr, 2);
if(!$conn){
echo $errno;
}else{
fwrite($conn, "\n");
$output = fread($conn, 1024);
fclose($conn);
if ($output == "") {
echo "<FONT CLASS=f1 COLOR=#DD0000><B>OFFLINE</B></FONT>";
} else {
echo "<FONT CLASS=f1 COLOR=#00DD00><B>ONLINE</B></FONT>";
}
}
It isn't enough to simply make a TCP connection to a SHOUTcast server to determine if the stream is working. In fact, a SHOUTcast server that is running will always accept yourTCP connection, even if there is no stream for playback.
You must connect, request the stream, and then check the return status code. Once connected, send this data:
GET /; HTTP/1.0
Follow that by a \r\n\r\n. Now, read data back from the stream until you get the \r\n\r\n. Then, you can disconnect. Check the status code from the first response line and see if it's 200. If it is, you've got an active stream.

Checking service fsockopen()

I'm trying to check service on port 9998. To do so I'm using fsockopen() function.
Like this:
$host = "1.1.1.1";
$port = "9998";
$checkconn = fsockopen($host, $port, $errno, $errstr, 1);
if($checkconn >= 1){
echo 'ok';
} else {
echo "$errstr";
}
It always returns "Connection Timed Out - 110", but the port is open and there is service running there.
If I change the port (like 80), it returns successful, but why not on the 9998 port?
A 1 second connect timeout is not very long. Network lag can be enough to trigger that even when the host/port is valid. I would suggest using at least 2.5 - 5 seconds instead.
Also, you should be checking the result of fsockopen() for FALSE instead of < 1 to know when it fails.
Try this:
$host = "1.1.1.1";
$port = "9998";
$checkconn = fsockopen($host, $port, $errno, $errstr, 5);
if(!$checkconn){
echo "($errno) $errstr";
} else {
echo 'ok';
}

Closing a HTTP connection after data is received

I am sending and receiving data using fsockopen and fwrite. My application receives the proper response, but seems to wait until some sort of timeout before continuing. I suspect that I am not closing the connection properly after it finishes receiving the response, which explains the wait. Could someone take a look at my code?
private static function Send($URL, $Data) {
$server = parse_url($URL, PHP_URL_HOST);
$port = parse_url($URL, PHP_URL_PORT);
// If the parsing the port information fails, we will assume it's on a default port.
// As such, we'll set the port in the switch below.
if($port == null) {
switch(parse_url($URL, PHP_URL_SCHEME)) {
case "HTTP":
$port = 80;
break;
case "HTTPS":
$port = 443;
break;
}
}
// Check if we are using a proxy (debug configuration typically).
if(\HTTP\HTTPRequests::ProxyEnabled) {
$server = \HTTP\HTTPRequests::ProxyServer;
$port = \HTTP\HTTPRequests::ProxyPort;
}
// Open a connection to the server.
$connection = fsockopen($server, $port, $errno, $errstr);
if (!$connection) {
die("OMG Ponies!");
}
fwrite($connection, $Data);
$response = "";
while (!feof($connection)) {
$response .= fgets($connection);
}
fclose($connection);
return $response;
}
The problem is with this line:
while (!feof($connection)) {
Sockets are streams; unless the other side closes the connection first, feof will never return true and your script will eventually time out.
To perform an orderly shutdown both parties have to close their end of the connection; one of them can do it as a response to the other, but clearly if both are waiting for the other to close first then noone ever will.
Does the program on the other side close the connection after outputting something? It seems not.

Categories