Java 套接字:为什么来自服务器的消息总是被分成完全相同的两条消息?

Java 套接字:为什么来自服务器的消息总是被分成完全相同的两条消息?,java,php,sockets,websocket,phpwebsocket,Java,Php,Sockets,Websocket,Phpwebsocket,此php代码段位于服务器端: if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n"; //bind the socket to the ip address and port if (socket_bind($sock, $ad

此php代码段位于服务器端:

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) 
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";


//bind the socket to the ip address and port
if (socket_bind($sock, $address, $port) === false) 
    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";  

//make the socket listen for connections, SOMAXCONN is the max limit of queued sockets waiting to 
//connect
if (socket_listen($sock, SOMAXCONN) === false) 
    echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";

if (($client= socket_accept($sock)) === false) 
    echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";

if ( false === ($id = socket_read($client, 10, PHP_NORMAL_READ)) )
    socket_close( $client ); //close the socket connection

$talkback = "PHP: Your id is '$id'.\n";
socket_write($client, $talkback, strlen($talkback));
while ((inputLine = in.readLine()) != null) 
    Log.i( "MY_TAG", "Message received: " + inputLine);
此java代码段位于客户端:

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) 
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";


//bind the socket to the ip address and port
if (socket_bind($sock, $address, $port) === false) 
    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";  

//make the socket listen for connections, SOMAXCONN is the max limit of queued sockets waiting to 
//connect
if (socket_listen($sock, SOMAXCONN) === false) 
    echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";

if (($client= socket_accept($sock)) === false) 
    echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";

if ( false === ($id = socket_read($client, 10, PHP_NORMAL_READ)) )
    socket_close( $client ); //close the socket connection

$talkback = "PHP: Your id is '$id'.\n";
socket_write($client, $talkback, strlen($talkback));
while ((inputLine = in.readLine()) != null) 
    Log.i( "MY_TAG", "Message received: " + inputLine);
其中,
inputLine
是一个字符串,
in
是客户端套接字的输入流

输出总是:

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) 
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";


//bind the socket to the ip address and port
if (socket_bind($sock, $address, $port) === false) 
    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";  

//make the socket listen for connections, SOMAXCONN is the max limit of queued sockets waiting to 
//connect
if (socket_listen($sock, SOMAXCONN) === false) 
    echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";

if (($client= socket_accept($sock)) === false) 
    echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";

if ( false === ($id = socket_read($client, 10, PHP_NORMAL_READ)) )
    socket_close( $client ); //close the socket connection

$talkback = "PHP: Your id is '$id'.\n";
socket_write($client, $talkback, strlen($talkback));
while ((inputLine = in.readLine()) != null) 
    Log.i( "MY_TAG", "Message received: " + inputLine);
收到消息:您的id是'1


收到消息:'.

您正在从套接字读取
$id
,在
PHP\u NORMAL\u READ
中,读取被换行符终止,因此
$id==“1\n”
。只需
trim()
it:

$id = trim($id);
//or
$talkback = "PHP: Your id is '" . trim($id) . "'.\n";

显示您是如何打开套接字的?
$id=“1\n”
可能吗?@MarcB我已经添加了它。我只包含了我认为你们需要看到的必要代码,但如果你们需要看到更多,请告诉我。我一按照你们的建议完成var_转储就意识到了这一点。非常感谢。