$server_host = "127.0.0.1";
$server_port = 8891;
//socket 创建
$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
//设置最大超时时间为6秒,usec为微秒
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array("sec" => 6, "usec" => 0));
//连接套接字
if((socket_connect($socket,$server_host,$server_port) == false)){
echo '连接失败'.socket_strerror(socket_last_error());
}else{
//把数据写入套接字
if(socket_write($socket,$message,strlen($message)) == false){
echo '连接失败'.socket_strerror(socket_last_error());
}else{
//读取套接字返回流
while($callback = socket_read($socket,1024)){
var_dump($callback);die();
}
}
}
//工作完毕,关闭套接流
socket_close($socket);
网友评论