socket详解

<?php

/*

 * 

 * socket主要翻译为套接字

 * 

socket_accept — Accepts a connection on a socket

 *               接受一个socket链接

 * 

socket_bind — 给套接字绑定名字

socket_clear_error — 清除套接字或者最后的错误代码上的错误

socket_close — 关闭套接字资源

socket_cmsg_space — Calculate message buffer size

 *                   消息缓冲区大小的计算

 * 

socket_connect — 开启一个套接字连接

socket_create_listen — Opens a socket on port to accept connections

 *                      打开一个套接字端口接受连接

 * 

socket_create_pair — Creates a pair of indistinguishable sockets and stores them in an array

 *                    创建一对不可区分套接字并将其存储在一个数组

 * 

socket_create — 创建一个套接字(通讯节点)

socket_get_option — Gets socket options for the socket

 *                   获取socket套接字选项

 * 

socket_getpeername — Queries the remote side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type

 *                    查询给定的套接字的远程侧既可以导致主机/端口或Unix文件系统路径,依赖于它的类型

 * 

socket_getsockname — Queries the local side of the given socket which may either result in host/port or in a Unix filesystem path, dependent on its type

 *                    查询给定的套接字的本地端可能既导致主机/端口或Unix文件系统路径,依赖于它的类型

 * 

socket_import_stream — Import a stream

 *                      导入流

 *  

socket_last_error — Returns the last error on the socket

 *                   返回最后的socket的错误

 * 

socket_listen — Listens for a connection on a socket

 *               监听socket的一个链接

 * 

socket_read — Reads a maximum of length bytes from a socket

 *            从socket读取最大长度字节

 * 

socket_recv — Receives data from a connected socket

 *             从连接套接字接收数据

 * 

socket_recvfrom — Receives data from a socket whether or not it is connection-oriented

 *                 从socket连接接收数据无论链接是否是面向连接

 * 

socket_recvmsg — Read a message

 *                读取一个信息

 * 

socket_select — Runs the select() system call on the given arrays of sockets with a specified timeout

 *              运行的select()系统指定一个给定的数组sockets,有指定的超时时间

 * 

socket_send — Sends data to a connected socket

 *             用socket链接发送数据

 * 

socket_sendmsg — Send a message

 *                发送一个信息

 * 

socket_sendto — Sends a message to a socket, whether it is connected or not

 *               用socket发送一个信息,无论是链接还是没有链接

 * 

socket_set_block — Sets blocking mode on a socket resource

 *                  设置阻塞模式的socket资源模式

 * 

socket_set_nonblock — Sets nonblocking mode for file descriptor fd

 *                     设置无阻塞的文件描述符FD模式

 * 

socket_set_option — Sets socket options for the socket

 *                   设置socket套接字选项

 * 

socket_shutdown — Shuts down a socket for receiving, sending, or both

 *                 关闭一个socket接收,发送,或同时

 * 

socket_strerror — Return a string describing a socket error

 *                 返回一个描述一个socket错误字符串

 * 

socket_write — Write to a socket

 *              写给一个socket

 *  

 * 

 */

server.php

<?php //确保在连接客户端时不会超时 set_time_limit(0); $ip = '127.0.0.1'; $port = 1935; /* +------------------------------- * @socket通信整个过程 +------------------------------- * @socket_create * @socket_bind * @socket_listen * @socket_accept * @socket_read * @socket_write * @socket_close +-------------------------------- */ /*---------------- 以下操作都是手册上的 -------------------*/ if(($sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) < 0) { echo "socket_create() 失败的原因是:".socket_strerror($sock)."\n"; } if(($ret = socket_bind($sock,$ip,$port)) < 0) { echo "socket_bind() 失败的原因是:".socket_strerror($ret)."\n"; } if(($ret = socket_listen($sock,4)) < 0) { echo "socket_listen() 失败的原因是:".socket_strerror($ret)."\n"; } $count = 0; do { if (($msgsock = socket_accept($sock)) < 0) { echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n"; break; } else { //发到客户端 $msg ="测试成功!\n"; socket_write($msgsock, $msg, strlen($msg)); echo "测试成功了啊\n"; $buf = socket_read($msgsock,8192); $talkback = "收到的信息:$buf\n"; echo $talkback; if(++$count >= 5){ break; }; } //echo $buf; socket_close($msgsock); } while (true); socket_close($sock); ?>
<?php

error_reporting(E_ALL);

set_time_limit(0);

echo "<h2>TCP/IP Connection</h2>\n";



$port = 1935;

$ip = "127.0.0.1";



/*

 +-------------------------------

 *    @socket连接整个过程

 +-------------------------------

 *    @socket_create

 *    @socket_connect

 *    @socket_write

 *    @socket_read

 *    @socket_close

 +--------------------------------

 */



$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

if ($socket < 0) {

    echo "socket_create() failed: reason: " . socket_strerror($socket) . "\n";

}else {

    echo "OK.\n";

}



echo "试图连接 '$ip' 端口 '$port'...\n";

$result = socket_connect($socket, $ip, $port);

if ($result < 0) {

    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";

}else {

    echo "连接OK\n";

}



$in = "Ho\r\n";

$in .= "first blood\r\n";

$out = '';



if(!socket_write($socket, $in, strlen($in))) {

    echo "socket_write() failed: reason: " . socket_strerror($socket) . "\n";

}else {

    echo "发送到服务器信息成功!\n";

    echo "发送的内容为:<font color='red'>$in</font> <br>";

}



while($out = socket_read($socket, 8192)) {

    echo "接收服务器回传信息成功!\n";

    echo "接受的内容为:",$out;

}





echo "关闭SOCKET...\n";

socket_close($socket);

echo "关闭OK\n";

?>

 

 

server.php需要在cli模式下运行,后续还有更多例子添加

你可能感兴趣的:(socket)