php
$opts = array('socket' =
// create the context...
$context = stream_context_create($opts);
// ...and use it to fetch the data
echo file_get_contents('', false, $context);
首先是服务端的代码:
$host = 'localhost';
set_time_limit(0);
$socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
$result = socket_bind($socket, $host, $port) or die("Could not blind to port\n");
while($spawn = socket_accept($socket) or die("Could not readinput\n")){
echo $input,"\n";
//client
$output = $input."\n";
socket_write($spawn, $output, strlen($output));
}
//kill
socket_close($spawn);
socket_close($socket);
echo "close\n";
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
echo 'Write to Socket',"\n";
if(!socket_write($socket, "some data")){
echo 'write failed',"\n";
//read respose from socket
echo 'Response: ',$buffer,"\n";
产生一个Socket,你需要三个变量:一个协议、一个socket类型和一个公共协议类型.产生一个socket有三种协议供选择,继续看下面的内容来获取详细的协议内容.
定义一个公共的协议类型是进行连接一个必不可少的元素.下面的表我们看看有那些公共的协议类型.
表一:协议
名字/常量 描述
表二:Socket类型
SOCK_STREAM 这个协议是按照顺序的、可靠的、数据完整的基于字节流的连接.这是一个使用最多的socket类型,这个socket是使用TCP来进行传输.
SOCK_DGRAM 这个协议是无连接的、固定长度的传输调用.该协议是不可靠的,使用UDP来进行它的连接.
SOCK_SEQPACKET 这个协议是双线路的、可靠的连接,发送固定长度的数据包进行传输.必须把这个包完整的接受才能进行读取.
SOCK_RAW 这个socket类型提供单一的网络访问,这个socket类型使用ICMP公共协议.(ping、traceroute使用该协议)
SOCK_RDM 这个类型是很少使用的,在大部分的操作系统上没有实现,它是提供给数据链路层使用,不保证数据包的顺序
表三:公共协议
ICMP 互联网控制消息协议,主要使用在网关和主机上,用来检查网络状况和报告错误信息
UDP 用户数据报文协议,它是一个无连接,不可靠的传输协议
TCP 传输控制协议,这是一个使用最多的可靠的公共协议,它能保证数据包能够到达接受者那儿,如果在传输过程中发生错误,那么它将重新发送出错数据包.
现在你知道了产生一个socket的三个元素,那么我们就在php中使用socket_create()函数来产生一个socket.这个 socket_create()函数需要三个参数:一个协议、一个socket类型、一个公共协议.socket_create()函数运行成功返回一个包含socket的资源类型,如果没有成功则返回false.
Resourece socket_create(int protocol, int socketType, int commonProtocol);
现在你产生一个socket,然后呢?php提供了几个操纵socket的函数.你能够绑定socket到一个IP,监听一个socket的通信,接受一个socket;现在我们来看一个例子,了解函数是如何产生、接受和监听一个socket.
$commonProtocol = getprotobyname("tcp");
$socket = socket_create(AF_INET, SOCK_STREAM, $commonProtocol);
socket_listen($socket);
// More socket functionality to come
上面这个例子产生一个你自己的服务器端.例子第一行,
使用公共协议名字来获取一个协议类型.今天这一节使用的是TCP公共协议,如果你想使用UDP或者ICMP协议,那么你应该把getprotobyname() 函数的参数改为"udp"或"icmp".还有一个可选的办法是不使用getprotobyname()函数而是指定SOL_TCP或SOL_UDP在 socket_create()函数中.
例子的第二行是产生一个socket并且返回一个socket资源的实例.在你有了一个socket资源的实例以后,你就必须把socket绑定到一个IP地址和某一个端口上.
在第四行以后,你就需要了解所有的socket函数和他们的使用.
表四:Socket函数
函数名 描述
socket_accept() 接受一个Socket连接
socket_bind() 把socket绑定在一个IP地址和端口上
socket_clear_error() 清除socket的错误或者最后的错误代码
socket_close() 关闭一个socket资源
socket_connect() 开始一个socket连接
socket_create_listen() 在指定端口打开一个socket监听
socket_create_pair() 产生一对没有区别的socket到一个数组里
socket_create() 产生一个socket,相当于产生一个socket的数据结构
socket_get_option() 获取socket选项
socket_getpeername() 获取远程类似主机的ip地址
socket_getsockname() 获取本地socket的ip地址
socket_iovec_add() 添加一个新的向量到一个分散/聚合的数组
socket_iovec_alloc() 这个函数创建一个能够发送接收读写的iovec数据结构
socket_iovec_delete() 删除一个已经分配的iovec
socket_iovec_fetch() 返回指定的iovec资源的数据
socket_iovec_free() 释放一个iovec资源
socket_iovec_set() 设置iovec的数据新值
socket_last_error() 获取当前socket的最后错误代码
socket_listen() 监听由指定socket的所有连接
socket_read() 读取指定长度的数据
socket_readv() 读取从分散/聚合数组过来的数据
socket_recv() 从socket里结束数据到缓存
socket_recvfrom() 接受数据从指定的socket,如果没有指定则默认当前socket
socket_recvmsg() 从iovec里接受消息
socket_select() 多路选择
socket_send() 这个函数发送数据到已连接的socket
socket_sendmsg() 发送消息到socket
socket_sendto() 发送消息到指定地址的socket
socket_set_block() 在socket里设置为块模式
socket_set_nonblock() socket里设置为非块模式
socket_set_option() 设置socket选项
socket_shutdown() 这个函数允许你关闭读、写、或者指定的socket
socket_strerror() 返回指定错误号的详细错误
socket_write() 写数据到socket缓存
socket_writev() 写数据到分散/聚合数组
(注: 函数介绍删减了部分原文内容,函数详细使用建议参考英文原文,或者参考PHP手册)
extension=php_sockets.dll
如果你无法去掉注释,那么请使用下面的代码来加载扩展库:
if(!extension_loaded('sockets'))
{
dl('php_sockets.dll');
else
dl('sockets.so');
如果你不知道你的socket是否打开,那么你可以使用phpinfo()函数来确定socket是否打开.你通过查看phpinfo信息了解socket是否打开.如下图:
查看phpinfo()关于socket的信息
◆ 产生一个服务器
现在我们把第一个例子进行完善.你需要监听一个指定的socket并且处理用户的连接.
// Accept any incoming connections to the server
$connection = socket_accept($socket);
if($connection)
socket_write($connection, "You have connected to the socket.../n/r");
在你的命令提示符中对这个脚本进行简单测试:
Php.exe example01_server.php
在上一个代码的基础上再改进,产生下面的代码来做你的新服务器端:
// Set up our socket
// Initialize the buffer
$buffer = "NO DATA";
while(true)
// Accept any connections coming in on this socket
printf("Socket connected/r/n");
// Check to see if there is anything in the buffer
if($buffer != "")
printf("Something is in the buffer...sending data.../r/n");
socket_write($connection, $buffer . "/r/n");
printf("Wrote to socket/r/n");
printf("No Data in the buffer/r/n");
// Get the input
$buffer = $data;
socket_write($connection, "Information Received/r/n");
printf("Buffer: " . $buffer . "/r/n");
socket_close($connection);
printf("Closed the socket/r/n/r/n");
这个服务器端要做什么呢?它初始化一个socket并且打开一个缓存收发数据.它等待连接,一旦产生一个连接,它将打印"Socket connected"在服务器端的屏幕上.这个服务器检查缓冲区,如果缓冲区里有数据,它将把数据发送到连接过来的计算机.然后它发送这个数据的接受信息,一旦它接受了信息,就把信息保存到数据里,并且让连接的计算机知道这些信息,最后关闭连接.当连接关闭后,服务器又开始处理下一次连接.(翻译的烂,附上原文)
This is what the server does. It initializes the socket and the buffer that you use to receive
and send data. Then it waits for a connection. Once a connection is created it prints
"Socket connected" to the screen the server is running on. The server then checks to see if
there is anything in the buffer; if there is, it sends the data to the connected computer.
After it sends the data it waits to receive information. Once it receives information it stores
it in the data, lets the connected computer know that it has received the information, and
then closes the connection. After the connection is closed, the server starts the whole
process again.
To solve the second problem is very easy. You need to create a PHP page that connects to
a socket, receive any data that is in the buffer, and process it. After you have processed the
data in the buffer you can send your data to the server. When another client connects, it
will process the data you sent and the client will send more data back to the server.
下面的例子示范了使用socket:
// Create the socket and connect
if($buffer == "NO DATA")
echo("pNO DATA/p");
break;
// Do something with the data in the buffer
echo("pBuffer Data: " . $buffer . "/p");
echo("pWriting to Socket/p");
// Write some test data to our socket
if(!socket_write($socket, "SOME DATA/r/n"))
echo("pWrite failed/p");
// Read any response from the socket
echo("pData sent was: SOME DATAbr Response was:" . $buffer . "/p");
echo("pDone Reading from Socket/p");
以上就是土嘎嘎小编为大家整理的phpsocket例子,WebSocket相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!