Rumah  >  Artikel  >  php教程  >  php socket通信(tcp/udp)

php socket通信(tcp/udp)

WBOY
WBOYasal
2016-06-14 00:01:511085semak imbas

注意 

  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>?>

 

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:完整的PHP MYSQL数据库类Artikel seterusnya:PHP文件上传详解