PHP socket server answer eof char to linux telnet
I want to talk with php server with socket by telnet.
I wrote 'echo' server (i send string to server, server send it to me)i use chr(0) on end of output string to send information that string is sent
socket_write($client, $output.chr(0));but telnet haven't see it and i cant send new string
TELNET
telnet 127.0.0.1 9000
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
hello
hello_
PHP
<?php
set_time_limit (0);
$address = '127.0.0.1';
$port = 9000;
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($sock, $address, $port) or die('Could not bind'开发者_运维百科);
socket_listen($sock);
while(true) {
$client = socket_accept($sock);
$input = trim(socket_read($client, 1024));
if ($input == 'off') break;
$output = $input.chr(0);
socket_write($client, $output);
}
socket_close($client);
socket_close($sock);
?>
what i'm doing wrong?
You should end the lines with carriage return or "\r\n"
, instead of just the \0
character. The telnet client watches out for them too I think.
The real problem however is your use of $client = socket_accept(..)
within the loop. You must only establish the connection once, before the while
. Otherwise you will reset the connected stream.
精彩评论