Jump to content

fread/fwrite problem


marked

Recommended Posts

Hi all,

This is driving me spare.

Using the following code:

[code]
function openServerConnection($ip,$port){
    global $serverResponse;
    $socket = fsockopen("udp://".$ip,$port,$errno,$errstat,30);
    if ($socket === false) echo "Connection fault: $errno - $errstr<br>\n";
    else {
        socket_set_timeout($socket, 3);
        $startTime = getTime();
        fwrite($socket, "\xFF\xFF\xFF\xFFgetstatus\x00");
        $serverString = fread($socket, 2048);
        $endTime = getTime();
    }
    fclose($socket);
    $serverResponse = (int)(($endTime - $startTime) * 1000);

    if($serverString) {
        sortData($serverString);
        return true;
    } else return false;
}
[/code]

I have a LAMP setup at home, and on my localhost test server this code works fine.  However uploading to my web host, it doesn't work at all.  The error seems to be in the fwrite or fread lines, because the serverString variable never contains a value, while endTime and startTime are populated correctly.

Is it likely that my host doesn't allow fread/fwrite or something?  Anyone else had similar?

Thanks
mark
Link to comment
https://forums.phpfreaks.com/topic/26262-freadfwrite-problem/
Share on other sites

Hi btherl,

Thanks for the reply.  I don't think the UDP is the issue, because I can view the page from both my localhost server (works), and my webhost server (fails) from the same machine with different results.  I would have thought that if it was a firewall issue, the localhost version would also fail?

I'll try the TCP connection when I get back onto my dev box, but I'm not sure how it will go, given that it's a game server I'm connecting to (retrieving server stats, player listing etc) and I think it's a UDP connection only, but I'll try it anyway.

Thanks again,
mark
Link to comment
https://forums.phpfreaks.com/topic/26262-freadfwrite-problem/#findComment-120158
Share on other sites

I forgot the most obvious thing to check..

"fwrite() returns the number of bytes written, or FALSE on error."
fread() "Returns the read string or FALSE in case of error."

Once we're sure the calls are succeeding, then it's time to look at whether or not the packets are getting through :)
Link to comment
https://forums.phpfreaks.com/topic/26262-freadfwrite-problem/#findComment-120160
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.