Socket communication PHP As a client, C + + as a service-side communication problem ...
Recently want to do a title such things, now PHP send can succeed, C + + to make the correct corresponding: send a JSON string, but the C + + message sent by PHP can not receive ... Don't know why this is. Would you please help me? Is there a problem with the message format that C + + passes to PHP? The key question I feel on the socket_read. But I can't figure it out.
Error_reporting (E_all);
Set_time_limit (0);
echo "TCP/IP Connection
\ n ";
$port = 6000;
$ip = "127.0.0.1";
$socket = Socket_create (Af_inet, Sock_stream, sol_tcp);
if ($socket < 0) {
echo "Socket_create () Failed:reason:". Socket_strerror ($socket). "\ n";
}else {
echo "ok.\n";
}
echo "Attempted to connect ' $ip ' port ' $port ' ... \ n";
$result = Socket_connect ($socket, $ip, $port);
if ($result < 0) {
echo "Socket_connect () Failed.\nreason: ($result)". Socket_strerror ($result). "\ n";
}else {
echo "Connect ok\n";
}
$in = ' R ';
$out = ";
if (!socket_write ($socket, $in, strlen ($in))) {
echo "Socket_write () Failed:reason:". Socket_strerror ($socket). "\ n";
}else {
echo "Sent to server information successfully! \ n ";
echo "sends the content as: $in
";
}
while ($out = Socket_read ($socket, 8192)) {
echo "Receive server backhaul information successfully! \ n ";
$obj =json_decode ($out);
echo "accepts the content as:", $out;
Echo $out;
Echo $obj->0;
}
/*
if (false!== ($bytes = Socket_recv ($socket, $buf, 2048, Msg_waitall))) {
echo ' AA '. $buf. ' AA ';
echo "Read $bytes bytes from Socket_recv (). Closing socket ... ";
} else {
echo "Socket_recv () failed; Reason: ". Socket_strerror (Socket_last_error ($socket)). " ";
}
*/
echo "Off socket...\n";
Socket_close ($socket);
echo "Off ok\n";
?>
------to solve the idea----------------------
That's not the case. If you have not received the Terminator, \ r, \ n will always be stopped waiting for input.
This means that the server does not have a send terminator (that is, it does not follow the Convention)
You can only connect the results of Socket_read ($socket, 1).
Actually read a piece and read a byte efficiency is the same (all read from the input buffer)