Fsocket char/string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mrcastrovinci
Forum Newbie
Posts: 3
Joined: Fri Sep 17, 2010 3:42 pm

Fsocket char/string

Post by mrcastrovinci »

All,
I am having trouble with an fsocket connection. I can use the code below without a hitch to grab the actual data. Were the problem lies is......I can only seem to read data from my device using FgetC. So it returns each character one by one because the other methods hang. I am ok with using this method but i would need to some how store each character returned and then make (combine) into one big string so I can manupulate later on. Now I know there are the functions of Socket_recv, which seems like what would be ideal to use. My device does not send a LF command but rather a carriage return only. Can someone post an example of socket_receive that accepts data until a carriage return is sent. I am not opening a file, this is data streaming from a device (ip-rs485). If this is not possible then can someone post an example of how to put the characters all in one string using the code below. Thanks so much!

$current_line = fgetc($fp);
while (!feof($fp)) {
// process current line
$current_line = fgetc($fp);
if($current_line === "\r") break;
echo $current_line;
}
fclose($fp);
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Fsocket char/string

Post by VladSun »

Code: Select all


$line = '';
while (!feof($fp)) 
{
    $current_line = fgetc($fp);
    if($current_line === "\r") break;
    $line .= $current_line;
}
fclose($fp);

echo $line;
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Fsocket char/string

Post by requinix »

What if you try using fgets after

Code: Select all

ini_set("auto_detect_line_endings", true);
stream_set_blocking($fp, 0);
mrcastrovinci
Forum Newbie
Posts: 3
Joined: Fri Sep 17, 2010 3:42 pm

Re: Fsocket char/string

Post by mrcastrovinci »

VladSun wrote:

Code: Select all


$line = '';
while (!feof($fp)) 
{
    $current_line = fgetc($fp);
    if($current_line === "\r") break;
    $line .= $current_line;
}
fclose($fp);

echo $line;

THIS WORKS PERFECTLY!!!! Thank you so much!
mrcastrovinci
Forum Newbie
Posts: 3
Joined: Fri Sep 17, 2010 3:42 pm

Re: Fsocket char/string

Post by mrcastrovinci »

tasairis wrote:What if you try using fgets after

Code: Select all

ini_set("auto_detect_line_endings", true);
stream_set_blocking($fp, 0);
tasairis wrote:ini_set("auto_detect_line_endings", true);
stream_set_blocking($fp, 0);

Thanks for the reply. I tried this and it just terminates the socket right away no data received. Weird.....Im not sure why. Any ideas?
Post Reply