Basic socket-based communication for PHP

Source: Internet
Author: User

PHP Basic socket-based communication 1, preface

A socket is an intermediate software abstraction layer that the application layer communicates with the TCP/IP protocol family, which is a set of interfaces. In design mode, the socket is actually a façade mode, it is the complex TCP/IP protocol family hidden behind the socket interface, for the user, a set of simple interface is all, let the socket to organize data to meet the specified protocol.

This introduction degree Niang a lot, want to know more self Baidu, look down on the friend remember to open the socket extension first;

2. Create Socke server Side

Basic steps:

    • Initialize 86socket
    • Port bindings
    • Port for monitoring
    • Call Accept Block
    • Waiting for the customer service side connection
3, server-side code implementation
<?Phpecho"****************server*****************\r\n";//Set IP and ports (ports must be guaranteed not to be occupied and allowed to be externally accessible)$ip ="127.0.0.1"; $port=1935;//Timeout DesignSet_time_limit (0);//Create socket$socket = Socket_create (Af_inet, Sock_stream,0) or Die ("failed to create socket because: {socket_strerror ($socket)}\r\n");//bind socket to specified IP and port$ret = Socket_bind ($socket, $ip, $port) or Die ("The bind socket failed because: {socket_strerror ($ret)}\r\n");//monitor socket, maximum number of 4$ret = Socket_listen ($socket,4) or Die ("the listener socket failed because: {socket_strerror ($ret)}\r\n");//Count$count =0; Echo"wait for the connection!!! \ r \ n"; Do {    //Request Connection received//calling child socket processing information$msgsock =socket_accept ($socket); if(!$msgsock) {echo"the socket block failed because: {socket_strerror ($msgsock)}\r\n"; } $msg="<p style= ' color:red ' > Connection Success </p>"; //process client input and return dataSocket_write ($msgsock, $msg, strlen ($msg)); $buf= Socket_read ($msgsock,8192); $talkback="the message received is: {$buf}\r\n";    Echo $talkback; if(+ + $count >5) {         Break; } socket_close ($msgsock);}  while(true); Socket_close ($socket);

4. Create Slient Client

Basic steps:

    • Create a socket resource using the TCP protocol
    • Connecting the socket server
    • Socket_write Transferring Data
    • Socket_read receiving data
    • Close the socket resource
5. Client code Implementation
<?Phpecho'****************client*****************<br/>';//set IP and Port$port =1935; $ip='127.0.0.1';//Timeout DesignSet_time_limit (0);//Create a socket resource for the TCP protocol$socket = Socket_create (Af_inet, Sock_stream, sol_tcp) or Die ('socket_create Failure:'. Socket_strerror ($socket)); Echo'Create success <br/>'; $restult=Socket_connect ($socket, $ip, $port); Echo'Connection Success <br/>';$inch='Create a Sokcet customer terminal success, random number ='. Rand ( +,9999);if(Socket_write ($socket, $inch, Strlen ($inch)) {echo'sent successfully, send message to'.$inch.'<br/>';} Else{echo'send failed, original because'. $socket _strerror ($socket).'<br/>';} while($ out= Socket_read ($socket,8192) {echo'receive the message successfully, the information is'.$ out.'<br/>';} Echo'socket off <br/>'; Socket_close ($socket); Echo'Close Completion <br/>';

6, open the customer service side 7, using the Client Connection server

Service-Side status:

The basic connection to the socket is almost there.

8, related line number introduction

8.1, Socket_create

Used to create a socket resource that successfully returns a socket and fails to return false;

Detailed reference: http://www.php.net/manual/zh/function.socket-create.php

8.2, Socket_bind

Binds a name to the socket, successfully returns true, FALSE when the failure returns;

Detailed reference: http://php.net/manual/en/function.socket-bind.php

8.3, Socket_listen

Listens for a socket resource, returns true successfully, False when the failure returns;

Detailed reference: http://php.net/manual/zh/function.socket-listen.php

8.4, Socket_accept

Legal creation of a child socket, successfully returned a new socket resource, failed to return false;

Detailed reference: http://www.php.net/manual/en/function.socket-accept.php

8.5, Socket_read

Reads the information sent by the customer service, returns the read information successfully, otherwise returns an empty string

(Socket_read () returns a zero length string ("") when there was no more data to read.)

Detailed reference: http://www.php.net/manual/en/function.socket-read.php

8.6, Socket_write

Want the socket to write data to the cache

Detailed reference: http://php.net/manual/en/function.socket-write.php

8.7, Socket_close

Close the socket resource

Detailed reference: http://php.net/manual/zh/function.socket-close.php

8.8, Socket_strerror

Get error message

Detailed reference: http://php.net/manual/zh/function.socket-strerror.php

Basic socket-based communication for PHP

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.