首页 > php教程 > php手册 > php socket通信(tcp/udp)

php socket通信(tcp/udp)

WBOY
发布: 2016-06-14 00:01:51
原创
1128 人浏览过

注意 

  1.在socket_bind的时候ip地址不能真回环地址如127.0.0.1

  2.server.php后台跑起来的时候 nohup php server.php > /var/tmp/a.log 2>&1 &

一: udp 方式

1) server.php

<span style="color: #000000;">php

</span><span style="color: #008000;">//</span><span style="color: #008000;">error_reporting( E_ALL );</span>
<span style="color: #008080;">set_time_limit</span>( 0<span style="color: #000000;"> );
</span><span style="color: #008080;">ob_implicit_flush</span><span style="color: #000000;">();
</span><span style="color: #800080;">$socket</span> = socket_create( AF_INET, SOCK_DGRAM,<span style="color: #000000;"> SOL_UDP );
</span><span style="color: #0000ff;">if</span> ( <span style="color: #800080;">$socket</span> === <span style="color: #0000ff;">false</span><span style="color: #000000;"> ) {
    </span><span style="color: #0000ff;">echo</span> "socket_create() failed:reason:" . socket_strerror( socket_last_error() ) . "\n"<span style="color: #000000;">;
}
</span><span style="color: #800080;">$ok</span> = socket_bind( <span style="color: #800080;">$socket</span>, '202.85.218.133', 11109<span style="color: #000000;"> );
</span><span style="color: #0000ff;">if</span> ( <span style="color: #800080;">$ok</span> === <span style="color: #0000ff;">false</span><span style="color: #000000;"> ) {
    </span><span style="color: #0000ff;">echo</span> "socket_bind() failed:reason:" . socket_strerror( socket_last_error( <span style="color: #800080;">$socket</span><span style="color: #000000;"> ) );
}
</span><span style="color: #0000ff;">while</span> ( <span style="color: #0000ff;">true</span><span style="color: #000000;"> ) {
    </span><span style="color: #800080;">$from</span> = ""<span style="color: #000000;">;
    </span><span style="color: #800080;">$port</span> = 0<span style="color: #000000;">;
    socket_recvfrom( </span><span style="color: #800080;">$socket</span>, <span style="color: #800080;">$buf</span>,1024, 0, <span style="color: #800080;">$from</span>, <span style="color: #800080;">$port</span><span style="color: #000000;"> );
    </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$buf</span><span style="color: #000000;">;
    </span><span style="color: #008080;">usleep</span>( 1000<span style="color: #000000;"> );
}
</span>?>
登录后复制

 

2) client.php 

 

<span style="color: #000000;">php
</span><span style="color: #800080;">$sock</span> = socket_create(AF_INET, SOCK_DGRAM,<span style="color: #000000;"> SOL_UDP);
</span><span style="color: #800080;">$msg</span> = 'hello'<span style="color: #000000;">;
</span><span style="color: #800080;">$len</span> = <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$msg</span><span style="color: #000000;">);
socket_sendto(</span><span style="color: #800080;">$sock</span>, <span style="color: #800080;">$msg</span>, <span style="color: #800080;">$len</span>, 0, '202.85.218.133', 11109<span style="color: #000000;">);
socket_close(</span><span style="color: #800080;">$sock</span><span style="color: #000000;">);
</span>?>
登录后复制

 

 

一: TCP 方式

1)server.php

<span style="color: #000000;">php

</span><span style="color: #008000;">//</span><span style="color: #008000;">error_reporting( E_ALL );</span>
<span style="color: #008080;">set_time_limit</span>( 0<span style="color: #000000;"> );
</span><span style="color: #008080;">ob_implicit_flush</span><span style="color: #000000;">();
</span><span style="color: #800080;">$socket</span> = socket_create( AF_INET, SOCK_STREAM,<span style="color: #000000;"> SOL_TCP );
socket_bind( </span><span style="color: #800080;">$socket</span>, '192.168.2.143', 11109<span style="color: #000000;"> );
socket_listen(</span><span style="color: #800080;">$socket</span><span style="color: #000000;">);
</span><span style="color: #800080;">$acpt</span>=socket_accept(<span style="color: #800080;">$socket</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">echo</span> "Acpt!\n"<span style="color: #000000;">;
</span><span style="color: #0000ff;">while</span> ( <span style="color: #800080;">$acpt</span><span style="color: #000000;"> ) {
    </span><span style="color: #800080;">$words</span>=<span style="color: #008080;">fgets</span><span style="color: #000000;">(STDIN);
    socket_write(</span><span style="color: #800080;">$acpt</span>,<span style="color: #800080;">$words</span><span style="color: #000000;">);
    </span><span style="color: #800080;">$hear</span>=socket_read(<span style="color: #800080;">$acpt</span>,1024<span style="color: #000000;">);
    </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$hear</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">if</span>("bye\r\n"==<span style="color: #800080;">$hear</span><span style="color: #000000;">){
        socket_shutdown(</span><span style="color: #800080;">$acpt</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
    }
    </span><span style="color: #008080;">usleep</span>( 1000<span style="color: #000000;"> );
}
socket_close(</span><span style="color: #800080;">$socket</span><span style="color: #000000;">)
</span>?>
登录后复制

2) client.php

<span style="color: #000000;">php
</span><span style="color: #800080;">$socket</span> = socket_create(AF_INET, SOCK_STREAM,<span style="color: #000000;"> SOL_TCP);
</span><span style="color: #800080;">$con</span>=socket_connect(<span style="color: #800080;">$socket</span>,'192.168.2.143',11109<span style="color: #000000;">);
</span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$con</span>){socket_close(<span style="color: #800080;">$socket</span>);<span style="color: #0000ff;">exit</span><span style="color: #000000;">;}
</span><span style="color: #0000ff;">echo</span> "Link\n"<span style="color: #000000;">;
</span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$con</span><span style="color: #000000;">){
        </span><span style="color: #800080;">$hear</span>=socket_read(<span style="color: #800080;">$socket</span>,1024<span style="color: #000000;">);
        </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$hear</span><span style="color: #000000;">;
        </span><span style="color: #800080;">$words</span>=<span style="color: #008080;">fgets</span><span style="color: #000000;">(STDIN);
        socket_write(</span><span style="color: #800080;">$socket</span>,<span style="color: #800080;">$words</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$words</span>=="bye\r\n"){<span style="color: #0000ff;">break</span><span style="color: #000000;">;}
}
socket_shutdown(</span><span style="color: #800080;">$socket</span><span style="color: #000000;">);
socket_close(</span><span style="color: #800080;">$sock</span><span style="color: #000000;">);
</span>?>
登录后复制

 

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板