Home > Backend Development > PHP Tutorial > 转载: PHP socket

转载: PHP socket

WBOY
Release: 2016-06-23 14:33:34
Original
921 people have browsed it

  (本文转载: http://www.cnblogs.com/yimiao/archive/2011/10/28/2227603.html)

 

工作实在比较闲,就来写写博客吧。顺便整理下。环境:windows

PHP在WIN下操作socket最好用命令行运行程序,至于为什么,自己可以体会下。

首先运行php + 程序的路径,出现了这个提示表示你的php没有加入环境变量path下面。

然后找到你的php安装目录,我的是这个

然后复制上面的路径,右键点击我的电脑-属性-高级-环境变量,添加到环境变量中,注意有个;号,看下图

然后确定,保存。再次运行cmd,OK成功了。不会出现那个提示了。

接下来就是主题了,先放代码

<?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);?>
Copy after login

这是socket的服务端代码。然后运行cmd,注意是自己的程序存放路径啊。

没有反映,对现在服务端的程序已经开始运行,端口已经开始监听了。运行netstat -ano可以查看端口情况,我的是1935端口

看,端口已经处于LISTENING状态了。接下来我们只要运行客户端程序即可连接上。上代码

<?phperror_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";?>
Copy after login

至此客户端已经连接上服务端了。这只是个小例子,很多东西知道原理其他就好做了

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template