PHP Socket Programming Detailed

Source: Internet
Author: User

Recent projects have a function that requires time-consuming tasks to run in the background, although PHP is not very suitable for a resident daemon, but because the main code is based on the PHP implementation, if the daemon running in the background to change other languages will be very inconvenient. So inevitably involves the web side and the daemon part of the communication, socket is a good way.

What is a socket?

The English literal of the socket is "hole" or " socket". As the BSD UNIX process communication mechanism, take the latter one meaning. Often also referred to as a "socket," which describes the IP address and port, is a handle to a communication chain. Hosts on the Internet typically run multiple service software, while providing several services. Each service opens a socket and binds to a port, and the different ports correspond to different services.

The above content from Baidu Encyclopedia

In short, sockets can help different services communicate on different ports.

Implementing the service side in PHP
1234567891011121314151617181920212223242526272829303132333435363738394041424344 <?<span id="6_nwp" style="width: auto; height: auto; float: none;"><a id="6_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=6a8c95905023ea45&k=php&k0=php&kdi0=0&luki=6&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=45ea235090958c6a&ssp2=1&stid=0&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F6057%2Ehtml&urlid=0" target="_blank" mpid="6" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">php</span></a></span> set_time_limit(0);// 设置<span id="7_nwp" style="width: auto; height: auto; float: none;"><a id="7_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=6a8c95905023ea45&k=%D6%F7%BB%FA&k0=%D6%F7%BB%FA&kdi0=0&luki=3&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=45ea235090958c6a&ssp2=1&stid=0&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F6057%2Ehtml&urlid=0" target="_blank" mpid="7" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">主机</span></a></span>和端口$host = "127.0.0.1";$port = 12387;// 创建一个tcp流$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)     or die("socket_create() failed:" . socket_strerror(socket_last_error()));// 设置阻塞模式socket_set_block($socket)     or die("socket_set_block() failed:" . socket_strerror(socket_last_error()));  // 绑定到端口socket_bind($socket, $host, $port)     or die("socket_bind() failed:" . socket_strerror(socket_last_error()));// 开始监听socket_listen($socket, 4)     or die("socket_listen() failed:" . socket_strerror(socket_last_error()));echo "Binding the socket on $host:$port ... \n"; while (true) {    // 接收连接请求并调用一个子连接Socket来处理客户端和<span id="8_nwp" style="width: auto; height: auto; float: none;"><a id="8_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=6a8c95905023ea45&k=%B7%FE%CE%F1%C6%F7&k0=%B7%FE%CE%F1%C6%F7&kdi0=0&luki=1&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=45ea235090958c6a&ssp2=1&stid=0&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F6057%2Ehtml&urlid=0" target="_blank" mpid="8" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">服务器</span></a></span>间的信息    if (($msgsock = socket_accept($socket)) < 0) {        echo "socket_accept() failed:" . socket_strerror(socket_last_error());    }else{        // 读数据        $out = ‘‘;        while($buf = socket_read($msgsock,8192)){            $out .= $buf;        }        // 写数据        $in = "数据是 $out";        socket_write($msgsock, $in, strlen($in));    }    // 结束通信    socket_close($msgsock);}socket_close($socket);?>

  The following steps are mainly performed on the server:

    • Create a listener for the socket and wait for the connection
    • When linked to, open a child connection processing IO
    • Receive transfer data from the client
    • Write the results back to the client
Client
123456789101112131415161718192021222324252627 <?<span id="4_nwp" style="width: auto; height: auto; float: none;"><a id="4_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=6a8c95905023ea45&k=php&k0=php&kdi0=0&luki=6&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=45ea235090958c6a&ssp2=1&stid=0&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F6057%2Ehtml&urlid=0" target="_blank" mpid="4" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">php</span></a></span> set_time_limit(0);$host = "127.0.0.1"$port = 12387;// 创建一个tcp流$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)     or die("socket_create() failed:" . socket_strerror(socket_last_error()));echo "try to connect to $host:$port...\n";$result = <span id="5_nwp" style="width: auto; height: auto; float: none;"><a id="5_nwl" href="http://cpro.baidu.com/cpro/ui/uijs.php?adclass=0&app_id=0&c=news&cf=1001&ch=0&di=128&fv=19&is_app=0&jk=6a8c95905023ea45&k=socket&k0=socket&kdi0=0&luki=7&mcpm=0&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=45ea235090958c6a&ssp2=1&stid=0&t=tpclicked3_hc&td=1922429&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F6057%2Ehtml&urlid=0" target="_blank" mpid="5" style="text-decoration: none;"><span style="color:#0000ff;font-size:14px;width:auto;height:auto;float:none;">socket</span></a></span>_connect($socket, $host, $port)    or die("socket_connect() failed:" . socket_strerror(socket_last_error()));$in = "hello \n";if(!socket_write($socket, $in, strlen($in))) {    echo "socket_write() failed:" . socket_strerror($socket);}else {    echo "发送成功!\n";}$out = ‘‘;while($buf = socket_read($socket, 8192)) {    $out .= $buf;}echo "接受内容为:$out \n";socket_close($socket);?>

  The client has the following main steps:

    • Connect to the service-side socket
    • Write data to the server
    • Receive data from the server

PHP Socket Programming Detailed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.