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()?
Related
I have the following code
It essentially connects to a local server , sends a command and reads the data back (128bytes.)
However, Sometimes its 1000 bytes, others its 4 bytes.
How can I get it so it just reads for a few seconds and closes then outputs the data it received?
I cant find anything on google to do this.
The server just sends the data and then essentially goes silent (no disconnect etc) so i just want to read for a few seconds into a variable and thats it.
<?php
$fp = fsockopen("localhost", 3332, $errno, $errstr, 3);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, "FSBC004000");
echo fgets($fp, 128);
fclose($fp);
}
}
?>
Figured it out!
ALways the case! Spend days looking for the answer.. Post to Stackoverflow... answer it myself within minutes,. ffs
Need to use:
stream_set_timeout($fp, 2);
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.
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.
I'm playing around with the IMAP protocol in PHP using fsockopen to send and receive commands. My preliminary experiments work but are insanely slow. It takes about 2 minutes for the simple function below to run. I've tried several different IMAP servers and have gotten the same result. Can anyone tell me why this code is so slow?
<?php
function connectToServer($host, $port, $timeout) {
// Connect to the server
$conn = fsockopen($host, $port, $errno, $errstr, $timeout);
// Write IMAP Command
$command = "a001 CAPABILITY\r\n";
// Send Command
fputs($conn, $command, strlen($command));
// Read in responses
while (!feof($conn)) {
$data .= fgets($conn, 1024);
}
// Display Responses
print $data;
// Close connection to server
fclose($conn);
}
connectToServer('mail.me.com', 143, 30);
?>
This is the response I get back:
macinjosh:Desktop Josh$ php test.php
* OK [CAPABILITY mmp0613 IMAP4 IMAP4rev1 ACL QUOTA LITERAL+ NAMESPACE UIDPLUS CHILDREN BINARY UNSELECT SORT LANGUAGE IDLE XSENDER X-NETSCAPE XSERVERINFO X-SUN-SORT X-SUN-IMAP X-ANNOTATEMORE X-UNAUTHENTICATE XUM1 AUTH=PLAIN STARTTLS] Messaging Multiplexor (Sun Java(tm) System Messaging Server 6.3-6.03 (built Jun 5 2008))
* CAPABILITY mmp0613 IMAP4 IMAP4rev1 ACL QUOTA LITERAL+ NAMESPACE UIDPLUS CHILDREN BINARY UNSELECT SORT LANGUAGE IDLE XSENDER X-NETSCAPE XSERVERINFO X-SUN-SORT X-SUN-IMAP X-ANNOTATEMORE X-UNAUTHENTICATE XUM1 AUTH=PLAIN STARTTLS
a001 OK CAPABILITY completed
It seems like feof won't return true until the remote side times out and closes the connection. The $timeout parameter that you are passing only applies to the initial connection attempt.
Try changing your while loop to print the status directly:
while (!feof($conn)) {
print fgets($conn, 1024);
}
Or change your loop exit condition to break after its read the full reply. It would probably have to be smarter about the protocol.
Finally, I have to ask, why aren't you using PHP's built-in IMAP client?
Are there any alternatives to using curl on hosts that have curl disabled?
To fetch content via HTTP, first, you can try with file_get_contents ; your host might not have disabled the http:// stream :
$str = file_get_contents('http://www.google.fr');
Bit this might be disabled (see allow_url_fopen) ; and sometimes is...
If it's disabled, you can try using fsockopen ; the example given in the manual says this (quoting) :
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
Considering it's quite low-level, though (you are working diretly with a socket, and HTTP Protocol is not that simple), using a library that uses it will make life easier for you.
For instance, you can take a look at snoopy ; here is an example.
http://www.phpclasses.org is full of these "alternatives", you can try this one: http://www.phpclasses.org/browse/package/3588.html
You can write a plain curl server script with PHP and place it on curl-enabled hosting, and when you need curl - you make client calls to it when needed from curl-less machine, and it will return data you need. Could be a strange solution, but was helpful once.
All the answers in this thread present valid workarounds, but there is one thing you should keep in mind. You host has, for whatever reason, deemed that making HTTP requests from your web server via PHP code is a "bad thing", and have therefore disabled (or not enabled) the curl extension. There's a really good chance if you find a workaround and they notice it that they'll block your request some other way. Unless there's political reasons forcing you into using this particular host, seriously consider moving your app/page elsewhere if it need to make http requests.