Socket communication PHP As a client, C + + as a service-side communication related issues.

Source: Internet
Author: User
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)
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.