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

php socket通信(tcp/udp)

WBOY
發布: 2016-06-14 00:01:51
原創
1102 人瀏覽過

注意 

  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
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板