PHP's Simple socket programming

Source: Internet
Author: User

I. What is a socket

The combination of the source IP address and destination IP address and the source port number and destination port number is called a socket. The servers and services that are used to identify client requests.

Second, PHP socket instance

Service-side code: socket.php

<?PHPerror_reporting(E_all);Set_time_limit(0);$CHR _13_10= "\ r \ n";//Carriage return Line//ob_implicit_flush ();$address= ' 127.0.0.1 ';$port= 10005;//Creating Socketsif( ($sock= Socket_create (Af_inet, Sock_stream, sol_tcp)) = = =false) {    Echo"Socket_create () failed:", Socket_strerror (Socket_last_error ()),$CHR _13_10;}//bind a socket to a portif(Socket_bind ($sock,$address,$port) ===false) {    Echo"Socket_bind () failed:", Socket_strerror (Socket_last_error ($sock)),$CHR _13_10;}//allow other sockets to connectif(Socket_listen ($sock, 5) = = =false) {    Echo"Socket_bind () failed:", Socket_strerror (Socket_last_error ($sock)),$CHR _13_10;} Do {    //Waiting for connection    if(($msgsock= Socket_accept ($sock)) ===false) {        Echo"Socket_accepty () failed:", Socket_strerror (Socket_last_error ($sock)),$CHR _13_10;  Break; }    //Read client-side information    Echo' Client message: ',$CHR _13_10; $buffer= Socket_read ($msgsock, 8192); Echo $buffer,$CHR _13_10; //The information to be read back to the client    if(false= = = Socket_write ($msgsock,$buffer,strlen($buffer))) {        Echo"Socket_write () failed:", Socket_strerror (Socket_last_error ($sock)),$CHR _13_10; }    //Close this connectionSocket_close ($msgsock);}  while(true);//Close SocketSocket_close ($sock);

Client code: client.php

<?PHP$CHR _13_10= "\ r \ n";//carriage return line break$service _port= 10005;$address= ' 127.0.0.1 ';//Creating Sockets$socket= Socket_create (Af_inet, Sock_stream,sol_tcp);if($socket===false) {    Echo"Socket_create () failed:", Socket_strerror (Socket_last_error ()),$CHR _13_10;} Else {    Echo"Socket create OK",$CHR _13_10;}//Connect to other sockets$result= Socket_connect ($socket,$address,$service _port);if($result===false) {    Echo"Socket_connect () failed:", Socket_strerror (Socket_last_error ($socket)),$CHR _13_10;} Else {    Echo"Socket Connect OK",$CHR _13_10;}$in= "";if(isset($argv[1]))$in.=$argv[1];//write information to a connected socketSocket_write ($socket,$in,strlen($in));Echo"Socket write OK",$CHR _13_10;Echo"Reading response-------------------------->start",$CHR _13_10;$out= "";//read the information sent from a connected socket while($out= Socket_read ($socket, 8192)) {    Echo $out;}Echo $CHR _13_10, "Reading response-------------------------->end",$CHR _13_10; Socket_close ($socket);Echo"Socket close OK",$CHR _13_10;

Third, the code demonstration results

1. Open a CMD to start the server

2. Open a CMD again to start the client

3, check the service side of the CMD

Iv. two images captured in the HTTP authoritative guide

1. The process of TCP client and server communication through the TCP socket interface

2. Common socket interface functions required for programming TCP connections

PHP's Simple socket programming

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.