0
$input = socket_read($clients[$i]['socket'],2048);
$lent = strlen($input);
echo "\n\n$lent\n";
echo "\nDATA: ".$input."\n";

My actual data length to read is >512.

Can somebody help me?

simonc
  • 41,632
  • 12
  • 85
  • 103
epoy
  • 11
  • 1
  • 6
  • 1
    How do you know the length is exactly 2048 bytes? I don't use this method myself, but I do know that if you use variable byte lengths, you first must read the first 4 (i believe) bytes to get the complete packet length. – Benz Apr 02 '13 at 07:19
  • the length really of my data is not fixed, and as i check the length of my record it is already 1361 as i used strlen function, but when i used: socket_recv($clients[$i]['socket'], $buff, 2048, MSG_WAITALL); i can receive all the data i sent, the problem is my socket_write function, it will prompt an error once i used socket_recv.. thank for the reply..@j0k – epoy Apr 02 '13 at 07:30
  • Okay, so you know the bytes length and use the socket_read() function to read all bytes. But what is the actual question? Is it going wrong? Are you not getting any output or something else :) ? – Benz Apr 02 '13 at 07:34
  • i got an output but not complete, that is really my problem.. the data is cut not complete.. thank you Benz.. – epoy Apr 02 '13 at 07:41
  • Ah, maybe the total amount of bytes coming trough multiple sockets/connections. What if you try to create a loop with the socket_read function? – Benz Apr 02 '13 at 07:55
  • yeah you are right, my design is multiple sockets.. so that is the problem now..i try already but it will hung up after it echo.. i have still other codes to do like socket_write() after i echo the $input.. my codes is this one: while ($input = socket_read($clients[$i]['socket'], 2048)) { echo "\nDATA: ".$input."\n"; } – epoy Apr 02 '13 at 08:15
  • o try already your example and it works in reading all my data.. but i got an error when i use socket_write..i try to echo the error and it will prompt "Broken pipe" my value of $ret is "YES" socket_write($clients[$i]['socket'], $ret,$len); $errorcode = socket_last_error(); $errormsg = socket_strerror($errorcode); – epoy Apr 02 '13 at 08:36
  • Broken pipe indicates that the connection is closed when sending the bytes. I see you are looping when writing, does the error occur at the first loop or after the first loop? – Benz Apr 02 '13 at 08:46
  • good morning @Benz actually this is my codes: while($input = socket_read($clients[$i]['socket'], 1024)) { $str1 .= $input; if (strpos($str1, "\n") !== false) break;} ....setting the data to write... socket_write($clients[$i]['socket'], $ret,$len); $errorcode = socket_last_error(); $errormsg = socket_strerror($errorcode); echo "\n\n$errormsg"; unset($clients[$i]); the error is "Broken pipe"..i think i didn't use loop in writing i just use "for loop" in selecting socket..thank you.God bless! – epoy Apr 03 '13 at 01:14

1 Answers1

2

Please take a look at this thread:

Can't read from socket (hangs)

while($resp = socket_read($clients[$i]['socket'], 1000)) {
   $str .= $resp;
   if (strpos($str, "\n") !== false) break;
}
socket_close($sock);
die("Server said: $str");

This set of code loops until the response is empty.

Community
  • 1
  • 1
Benz
  • 2,317
  • 12
  • 19