在php中创建socket
<?php
$host="192.168.0.2";
$port=12345;
set_time_limit(0);
$socket=socket_create(AF_INET,SOCK_STREAM,0) or die("could not create socket\n");//创建socket
$result=socket_bind($socket,$host,$port) or die("could not bind to socket\n");//绑定socket到指定地址和端口
$result=socket_listen($socket,3) or die("could not set up socket listener\n");//开始监听连接
$spawn=socket_accept($socket) or die("could not accept incoming connection\n");//接收连接请求
$input=socket_read($spawn,1024) or die("could not read input\n");读取客户端输入
$input=trim($input);//clean up input string
$output=strrev($input)."\n";//反转客户端输入数据
socket_write($spawn,$output,strlen($output)) or die("could not write output\n");//返回服务端
socket_close($spawn);
socket_close($socket);关闭socket
?>