PHP Create socket Service

Source: Internet
Author: User

PHP can create a socket service.

First familiar with a few PHP network functions, operating manual address http://php.net/manual/zh/ref.sockets.php

A simple introduction to the socket, which represents the socket, is used for the TCP/IP protocol. A little understanding of network communications know that the establishment of TCP/IP requires two sides of their respective network address ip+ ports port, this ip+port is the socket, communication needs a pair so socket. For example, a common browser request server, the browser side of the ip+ randomly find an idle port number, connect the ip+ service port of the other server (usually the HTTP service is 80).

A code example of the browser requesting the server's socket on the top of the tutorial, deepens understanding:

<?PHP$remote _ip= ' 61.135.169.125 '; $remote _port= ' 80 '; $socket= Localsocket ($remote _ip,$remote _port); EchoThe local socket is$socket\ n "; EchoThe remote socket is$remote _ip:$remote _port\ n "; functionLocalsocket ($dest= ' 61.135.169.125 ',$port=80)  {    $socket= Socket_create (Af_inet, Sock_dgram,sol_udp); Socket_connect ($socket,$dest,$port);//Dest+port is the remote serverSocket_getsockname ($socket,$addr,$port);//get the local socket, addr + port is localSocket_close ($socket); return $addr.":".$port; }

By outputting The local ip+port it is not difficult to find that the local port is given at random.

  

PHP Create socket Service

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.