Php 套接字服务器无法使用命令行
这些代码构建tcp服务器,您可以使用telnet或netcat访问服务器。 您还可以使用浏览器访问服务器,如“”。现在您将看到命令行输出。 socket_accept会阻止进程,直到到达请求。有关详细信息,请访问Php 套接字服务器无法使用命令行,php,web-services,sockets,Php,Web Services,Sockets,这些代码构建tcp服务器,您可以使用telnet或netcat访问服务器。 您还可以使用浏览器访问服务器,如“”。现在您将看到命令行输出。 socket_accept会阻止进程,直到到达请求。有关详细信息,请访问 <?php if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0))) { $errorcode = socket_last_error(); $errormsg = socket_strerror($errorco
<?php
if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0)))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
echo "Socket created \n";
// Bind the source address
if( !socket_bind($sock, "127.0.0.1" , 5000) )
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [$errorcode] $errormsg \n");
}
echo "Socket bind OK \n";
if(!socket_listen ($sock , 10))
{
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not listen on socket : [$errorcode] $errormsg \n");
}
echo "Socket listen OK \n";
echo "Waiting for incoming connections... \n";
//Accept incoming connection - This is a blocking call
$client = socket_accept($sock);
//display information about the client who is connected
if(socket_getpeername($client , $address , $port))
{
echo "Client $address : $port is now connected to us.";
}
socket_close($client);
socket_close($sock);