PHP 套接字无法正常工作
我已经建立了一个服务器来监听通过我的 127.0.0.1 上的端口 1122 传入的请求 这是我的代码:
<?php
// set some variables
$host = "127.0.0.1";
$port = 1122;
// don't timeout!
//Since this is a server, it's also a good idea to use the set_time_limit()
//function to ensure that PHP doesn't time out and die() while waiting for
//incoming client connections.
set_time_limit(0);
// create socket
// The AF_INET parameter specifies the domain, while the SOCK_STREAM
// parameter tells the function what type of socket
// to create (in this case, TCP).
//If you wanted to create a UDP socket, you could use the
//following line of code instead:socket_create(AF_INET, SOCK_DGRAM, 0)
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create
socket\n");
// bind socket to port
//Once a socket handle has been created, the next step is to attach,
//or "bind", it to the specified address and port.
//This is accomplished via the socket_bind() function.
$result = socket_bind($socket, $host, $port) or die("Could not bind to
socket\n");
// start listening for connections
//With the socket created and bound to a port, it's time to start listening
//for incoming connections. PHP allows you to set the socket up as a listener
//via its socket_listen() function, which also allows you to specify the number
//of queued connections to allow as a second parameter (here 3)
$result = socket_listen($socket, 3) or die("Could not set up socket
listener\n");
// accept incoming connections
// spawn another socket to handle communication
//Once a client connection is received, the socket_accept() function springs
//into action, accepting the connection request and spawning another
//child socket to handle messaging between the client and the server.
//This child socket will now be used for all subsequent communication
//between the client and server.
$spawn = socket_accept($socket) or die("Could not accept incoming
connection\n");
// read client input
//With a connection established, the server now waits for the client
//to send it some input - this input is read via the socket_read() function,
//and assigned to the PHP variable $input.
//The second parameter to socket_read() specifies the number of bytes of input to read -
//you can use this to limit the size of the data stream read from the client.
//Note that the socket_read() function continues to read data from the client
//until it encounters a carriage return (\n), a tab (\t) or a \0 character.
//This character as treated as the end-of-input character, and triggers
//the next line of the PHP script.
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
//The server now must now process the data sent by the client - in this example,
//this processing merely involves reversing the input string
//and sending it back to the client.
//This is accomplished via the socket_write() function, which makes it possible
//to send a data stream back to the client via the communication socket.
//The socket_write() function needs three parameters: a reference to the socket,
//the string to be written to it, and the number of bytes to be written.
$input = trim($input);
// reverse client input and send back
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write
output\n");
// close sockets
//Once the output has been sent back to the client, both generated sockets
//are terminated via the socket_close() function.
socket_close($spawn);
socket_close($socket);
?>
这将监听客户端并返回 inout 的反转值。
该代码不会给出任何错误。事实上,当我访问 localhost/socket.php 时,它会继续加载。
现在我使用 terraterm 打开 telnet 连接并出现一个白页,如果我输入任何值或按任何键,连接将丢失。
知道为什么会发生这种情况吗?
亲切的问候
I have established a server to listen to incoming requests through port 1122 on my 127.0.0.1
Here is my code :
<?php
// set some variables
$host = "127.0.0.1";
$port = 1122;
// don't timeout!
//Since this is a server, it's also a good idea to use the set_time_limit()
//function to ensure that PHP doesn't time out and die() while waiting for
//incoming client connections.
set_time_limit(0);
// create socket
// The AF_INET parameter specifies the domain, while the SOCK_STREAM
// parameter tells the function what type of socket
// to create (in this case, TCP).
//If you wanted to create a UDP socket, you could use the
//following line of code instead:socket_create(AF_INET, SOCK_DGRAM, 0)
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create
socket\n");
// bind socket to port
//Once a socket handle has been created, the next step is to attach,
//or "bind", it to the specified address and port.
//This is accomplished via the socket_bind() function.
$result = socket_bind($socket, $host, $port) or die("Could not bind to
socket\n");
// start listening for connections
//With the socket created and bound to a port, it's time to start listening
//for incoming connections. PHP allows you to set the socket up as a listener
//via its socket_listen() function, which also allows you to specify the number
//of queued connections to allow as a second parameter (here 3)
$result = socket_listen($socket, 3) or die("Could not set up socket
listener\n");
// accept incoming connections
// spawn another socket to handle communication
//Once a client connection is received, the socket_accept() function springs
//into action, accepting the connection request and spawning another
//child socket to handle messaging between the client and the server.
//This child socket will now be used for all subsequent communication
//between the client and server.
$spawn = socket_accept($socket) or die("Could not accept incoming
connection\n");
// read client input
//With a connection established, the server now waits for the client
//to send it some input - this input is read via the socket_read() function,
//and assigned to the PHP variable $input.
//The second parameter to socket_read() specifies the number of bytes of input to read -
//you can use this to limit the size of the data stream read from the client.
//Note that the socket_read() function continues to read data from the client
//until it encounters a carriage return (\n), a tab (\t) or a \0 character.
//This character as treated as the end-of-input character, and triggers
//the next line of the PHP script.
$input = socket_read($spawn, 1024) or die("Could not read input\n");
// clean up input string
//The server now must now process the data sent by the client - in this example,
//this processing merely involves reversing the input string
//and sending it back to the client.
//This is accomplished via the socket_write() function, which makes it possible
//to send a data stream back to the client via the communication socket.
//The socket_write() function needs three parameters: a reference to the socket,
//the string to be written to it, and the number of bytes to be written.
$input = trim($input);
// reverse client input and send back
$output = strrev($input) . "\n";
socket_write($spawn, $output, strlen ($output)) or die("Could not write
output\n");
// close sockets
//Once the output has been sent back to the client, both generated sockets
//are terminated via the socket_close() function.
socket_close($spawn);
socket_close($socket);
?>
this will listen to the client and return a reversed value of the inout.
The code will not give any errors. in fact when I go to localhost/socket.php it will keep loading.
Now I open a telnet connection using terraterm and a white page appears, If I input any value or strike any key the connection will be lost.
Any idea why this is happening ?
Kind Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题可能是您的 tera 术语正在使用“字符模式”,这意味着输入的每个字符都会直接发送到服务器。
这会导致
socket_read
立即返回并仅读取一个字符。您可以在调用
socket_read
时设置PHP_NORMAL_READ
:如果没有该标志,
socket_read
就不会读取,直到\ n 被写入。
否则它会表现正常。该脚本不会将任何输出写入浏览器,因此浏览器只会等待,直到出现超时(您的 Web 服务器可能会终止 PHP 脚本,因为没有写入任何内容)。
The problem is probably that your tera term is using the "character mode" which means that every character typed is directly send to the server.
This causes the
socket_read
to immediately return with just one character read.You could set the
PHP_NORMAL_READ
when callingsocket_read
:Without that flag
socket_read
does not read until a\n
is written.Otherwise it behaves normally. The script doesn't write any output to the browser so the browser just waits until some timeout sets in (your Webserver might terminate the PHP-script because nothing is written).