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
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
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
Share on other sites

Hi btherl/other interested parties.

This is looking like it's a webhost problem, and I've lodged a support ticket with them. 

To verify this, I uploaded the full script to another host, and it works perfectly.

Thank you to all who helped.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.