注意
1.socket_bind を使用する場合、IP アドレスは 127.0.0.1 などの実際のループバック アドレスにすることはできません
2.server.php がバックグラウンドで実行されている場合 nohup php server.php > /var/tmp/a.log 2>&1 &
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>?>
1: 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>?>