Heim > php教程 > php手册 > Php的Socket初步

Php的Socket初步

WBOY
Freigeben: 2016-06-06 19:52:54
Original
1156 Leute haben es durchsucht

工作实在比较闲,就来写写博客吧。顺便整理下。环境:windows PHP在WIN下操作socket最好用命令行运行程序,至于为什么,自己可以体会下。 首先运行php 程序的路径,出现了这个提示表示你的php没有加入环境变量path下面。 然后找到你的php安装目录,我的是这

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

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

Php的Socket初步

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

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

Php的Socket初步

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

Php的Socket初步

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

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

Php的Socket初步

<?php <span>//<span>确保在连接客户端时不会超时</span><span>set_time_limit</span>(0);

<span>$ip</span> = '127.0.0.1';
<span>$port</span> = 1935;

<span>/*</span><span>
 +-------------------------------
 *    @socket通信整个过程
 +-------------------------------
 *    @socket_create
 *    @socket_bind
 *    @socket_listen
 *    @socket_accept
 *    @socket_read
 *    @socket_write
 *    @socket_close
 +--------------------------------
 </span><span>*/</span>

<span>/*</span><span>----------------    以下操作都是手册上的    -------------------</span><span>*/</span>
<span>if</span>((<span>$sock</span> = socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) echo "socket_create() 失败的原因是:".socket_strerror(<span>$sock</span>)."\n";
}

<span>if</span>((<span>$ret</span> = socket_bind(<span>$sock</span>,<span>$ip</span>,<span>$port</span>)) echo "socket_bind() 失败的原因是:".socket_strerror(<span>$ret</span>)."\n";
}

<span>if</span>((<span>$ret</span> = socket_listen(<span>$sock</span>,4)) echo "socket_listen() 失败的原因是:".socket_strerror(<span>$ret</span>)."\n";
}

<span>$count</span> = 0;

<span>do</span> {
    <span>if</span> ((<span>$msgsock</span> = socket_accept(<span>$sock</span>)) echo "socket_accept() failed: reason: " . socket_strerror(<span>$msgsock</span>) . "\n";
        <span>break</span>;
    } <span>else</span> {
        
        <span>//</span><span>发到客户端</span>        <span>$msg</span> ="测试成功!\n";
        socket_write(<span>$msgsock</span>, <span>$msg</span>, <span>strlen</span>(<span>$msg</span>));
        
        <span>echo</span> "测试成功了啊\n";
        <span>$buf</span> = socket_read(<span>$msgsock</span>,8192);
        
        
        <span>$talkback</span> = "收到的信息:<span>$buf</span>\n";
        <span>echo</span> <span>$talkback</span>;
        
        <span>if</span>(++<span>$count</span> >= 5){
            <span>break</span>;
        };
        
    
    }
    <span>//</span><span>echo $buf;</span>    socket_close(<span>$msgsock</span>);

} <span>while</span> (<span>true</span>);

socket_close(<span>$sock</span>);
?>
Nach dem Login kopieren

Php的Socket初步

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

Php的Socket初步

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

Php的Socket初步

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

Php的Socket初步

<?php <span>error_reporting(<span>E_ALL</span>);
<span>set_time_limit</span>(0);
<span>echo</span> "<h2>TCP/IP Connection</h2>\n";

<span>$port</span> = 1935;
<span>$ip</span> = "127.0.0.1";

<span>/*</span><span>
 +-------------------------------
 *    @socket连接整个过程
 +-------------------------------
 *    @socket_create
 *    @socket_connect
 *    @socket_write
 *    @socket_read
 *    @socket_close
 +--------------------------------
 </span><span>*/</span>

<span>$socket</span> = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
<span>if</span> (<span>$socket</span> echo "socket_create() failed: reason: " . socket_strerror(<span>$socket</span>) . "\n";
}<span>else</span> {
    <span>echo</span> "OK.\n";
}

<span>echo</span> "试图连接 '<span>$ip</span>' 端口 '<span>$port</span>'...\n";
<span>$result</span> = socket_connect(<span>$socket</span>, <span>$ip</span>, <span>$port</span>);
<span>if</span> (<span>$result</span> echo "socket_connect() failed.\nReason: (<span>$result</span>) " . socket_strerror(<span>$result</span>) . "\n";
}<span>else</span> {
    <span>echo</span> "连接OK\n";
}

<span>$in</span> = "Ho\r\n";
<span>$in</span> .= "first blood\r\n";
<span>$out</span> = '';

<span>if</span>(!socket_write(<span>$socket</span>, <span>$in</span>, <span>strlen</span>(<span>$in</span>))) {
    <span>echo</span> "socket_write() failed: reason: " . socket_strerror(<span>$socket</span>) . "\n";
}<span>else</span> {
    <span>echo</span> "发送到服务器信息成功!\n";
    <span>echo</span> "发送的内容为:<font color="red"><span>$in</span></font> <br>";
}

<span>while</span>(<span>$out</span> = socket_read(<span>$socket</span>, 8192)) {
    <span>echo</span> "接收服务器回传信息成功!\n";
    <span>echo</span> "接受的内容为:",<span>$out</span>;
}


<span>echo</span> "关闭SOCKET...\n";
socket_close(<span>$socket</span>);
<span>echo</span> "关闭OK\n";
?>
Nach dem Login kopieren

Php的Socket初步

Php的Socket初步


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

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage