socket connection issue+php - php

I am using PHP socket programming and able to write data to open socket but i have to wait for a long time(or stuck it)for the response or some time getting error like "Maximum execution time of 30 seconds exceeded line number where this code is placed fgets($fp, 128), i have check the server it seems it has sent the response as expected but i am not getting why i m unable to get response.following the code using for socket connection and reading data.
functon scoket_connection() {
$fp = fsockopen(CLIENT_HOST,CLIENT_PORT, $errno, $errstr);
fwrite($fp,$packet);
$msg = fgets($fp, 128);
fclose($fp)
return $msg;
}
any idea???

Is by any chance your client on a different platform than the server? When I say different I mean Windows/Linux/Mac. These each have different line endings. fgets() is supposed to read a line which means it expects to find a certain line ending before it returns anything. If one system is sending for example \n and the other expects \r\n it could cause this problem.

Related

PHP fgets() always times out on a socket even after reading some data

I am creating a socket in this way:
$fp = fsockopen("tcp://".$ip, $port, $errno, $errstr);
stream_set_timeout($fp, 1); // timeout is one second
stream_set_blocking($fp, 1); // block until data is available
However, when I read I always get a timeout, even after reading data:
while(true) {
$result = fgets($fp, 4096);
if(empty($result)) {
break;
}
else {
echo $result;
}
}
I think my method of reading is wrong because I don't fully understand how php sockets work in the background. My intent is to read until there's no more data, or give up if I've tried to read data for a certain amount of time but no data has become available.
In various PHP socket examples I've seen the use of a while(!feof($fp)) to stop a read loop, but as far as I'm concerned the other end will never send an eof marker. They only send newline delimited messages.
Any ideas?
Thanks.

PHP function fsockopen never returning false

Could somebody help me with the php function fsockopen?
if I call the function like this :
$fp = fsockopen('xywqnda.com', 80, $errno, $errstr, 10);
With an unavailable host domain, it will never return false and I don't understand why!
Ah, you are using UDP. Your original example didn't show this. This changes things. From the PHP manual:
Warning
UDP sockets will sometimes appear to have opened without an error, even if the remote host is unreachable. The error will only
become apparent when you read or write data to/from the socket. The
reason for this is because UDP is a "connectionless" protocol, which
means that the operating system does not try to establish a link for
the socket until it actually needs to send or receive data.
try this
ini_set("display_errors","on")
it will show up a warning if the domain is invalid, other than that the function will return TRUE because the file pointer is returned, meaning file was created with success, FALSE will be returned only if it can't create the file.
// displays all warnings, notices and errors
ini_set('display_errors', 1);
error_reporting(E_ALL);
$fp = fsockopen('xywqnda.com', 80, $errno, $errstr, 10);
The connection attempt will timeout after 10 seconds.
You will get a warning because the domain is unavailable.
$errno and $errstr will contain the system error number and error message.
The function will return false, so $fp will be equal to false.
Documentation: fsockopen

Socket PHP hangs on fgets

I have server and client application in JAVA, what working with this server. On first look, it's no problems - JAVA uses socket.getInputStream() for receiving data and socket.getOutputStream() for sending data.
I need to write same client on PHP. All examples from manuals didn't help me. I can succesfully connect to server, but when i trying to read something - page hangs. For example:
$fp = stream_socket_client($addr, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, $data);
while (!feof($fp)) {
var_dump(fgets($fp, 1024));
}
fclose($fp);
}
This code hangs even without while.
What can be wrong?
Does your server really send bytes?
fgets($fp, 1024)
returns, if one of these conditions happens:
- EOF or newline received
- 1024-1 bytes read
or the far side closed the connection.
If these conditions do not happen, the call blocks.
How about changing 1024 to a lower number or use fgetc()?

Fetch errors from APNS with PHP

I use a self written script to send push notifications to APNS with PHP. In order to be able to process errors I use the extended format for the Push notifications and would like to fetch results from the stream:
// $apns = a stream_socket_client connection
$apnsMessage = pack('CNNnH*', 1, $i, $pnDetails['expiration_time'], 32, $pnDetails['token']);
$apnsMessage .= pack('n', strlen($pnDetails['payload']));
$apnsMessage .= $pnDetails['payload'];
fwrite($apns, $apnsMessage);
// Check for errors
$errorResponse = #fread($apns, 6)
if ($errorResponse != FALSE) {
$unpackedError = unpack('Ccommand/CstatusCode/Nidentifier', $errorResponse);
}
I have seen a very similar practice in the apns-php project, however, in my case the script always waits indefinitely at the fread line because it tries to read data which is not there (Apple only sends a response if there was an error). I have looking for ways to tell if there is any new data to read from a TCP stream, however, I could find none and the stream callback methods available for HTTP calls are not available for "raw" TCP connections either.
How can I transform my script to make sure it only calls fread when there actually is data to read? How does the apns-php project solve this issue (from what I could tell they were just calling fread as well)?
Figured it out, the final hint came from Erwin. The trick was to deactivate the blocking with stream_set_blocking, now I just need to wait some time before fetching the results with fread to make sure that Apple has enough time to respond.
Are you connecting to the right host ssl://feedback.push.apple.com:2196 ?
They are using the following calls to connect and read data:
stream_context_create -> stream_socket_client -> stream_set_blocking (0) -> stream_set_write_buffer (0) -> while (!feof($socket)) {} -> fread (8192) -> stream_select (with timeout)

Detecting early failure in php fwrite

Earlier today I noticed some calls to php fwrite failing as the destination socket was in a mixed state. There were numerous connections stuck in SYN_SENT and were seemingly not coming back as failures.
What is the best way to detect this and simply time out the connection if x bits haven't bit transmitted over the wire?
I believe you are looking for stream_set_timeout. An example:
stream_set_timeout($fp, 2);
fwrite($fp, "GET / HTTP/1.0\r\n\r\n");
You can check whether a time-out happens by checking the meta data of the stream:
$info = stream_get_meta_data($fp);
// $info['timed_out'] == true : time-out has happened

Categories