How to send phpsocket-php Tutorial

Source: Internet
Author: User
How to send phpsocket
 
 Master = socket_create (AF_INET, SOCK_STREAM, SOL_TCP) or die ("socket_create () failed"); socket_set_option ($ this-> master, SOL_SOCKET, SO_REUSEADDR, 1) or die ("socket_option () failed"); socket_bind ($ this-> master, $ address, $ port) or die ("socket_bind () failed "); socket_listen ($ this-> master, 20) or die ("socket_listen () failed"); $ this-> sockets [] = $ this-> master; $ this-> say ("Server Started :". date ('Y-m-d H: I: s '); $ This-> say ("Listening on :". $ address. "port ". $ port); $ this-> say ("Master socket :". $ this-> master. "\ n"); while (true) {$ socketArr = $ this-> sockets; $ write = NULL; $ except = NULL; socket_select ($ socketArr, $ write, $ response T, NULL); // The socket automatically selected for the message. if the handshake is used, the host foreach ($ socketArr as $ socket) is automatically selected) {if ($ socket ==$ this-> master) {// host $ client = socket_accept ($ this-> master); if ($ client <0) {$ this-> log ("socket_ac Cept () failed "); continue;} else {$ this-> connect ($ client) ;}} else {$ this-> log ('' ^ "); $ bytes = @ socket_recv ($ socket, $ buffer, 2048,0); $ this-> log (''^"); if ($ bytes = 0) {$ this-> disConnect ($ socket);} else {if (! $ This-> handshake) {$ this-> doHandShake ($ socket, $ buffer);} else {$ buffer = $ this-> decode ($ buffer ); $ this-> send ($ socket, $ buffer) ;}}} function send ($ client, $ msg) {$ this-> log ("> ". $ msg); $ msg = $ this-> frame ($ msg); socket_write ($ client, $ msg, strlen ($ msg); $ this-> log ("! ". Strlen ($ msg);} function connect ($ socket) {array_push ($ this-> sockets, $ socket); $ this-> say ("\ n ". $ socket. "CONNECTED! "); $ This-> say (date (" Y-n-d H: I: s ");} function disConnect ($ socket) {$ index = array_search ($ socket, $ this-> sockets); socket_close ($ socket); $ this-> say ($ socket. "DISCONNECTED! "); If ($ index >=0) {array_splice ($ this-> sockets, $ index, 1) ;}} function doHandShake ($ socket, $ buffer) {$ this-> log ("\ nRequesting handshake... "); $ this-> log ($ buffer); list ($ resource, $ host, $ origin, $ key) = $ this-> getHeaders ($ buffer ); $ this-> log ("Handshaking... "); $ upgrade =" HTTP/1.1 101 Switching Protocol \ r \ n ". "Upgrade: websocket \ r \ n ". "Connection: Upgrade \ r \ n ". "Sec-WebSocket-Accept :". $ this-> calcKey ($ key ). "\ r \ n"; // it must end with two carriage returns $ this-> log ($ upgrade); $ sent = socket_write ($ socket, $ upgrade, strlen ($ upgrade); $ this-> handshake = true; $ this-> log ("Done handshaking... "); return true;} function getHeaders ($ req) {$ r = $ h = $ o = $ key = null; if (preg_match ("/GET (. *) HTTP/", $ req, $ match) {$ r = $ match [1];} if (preg_match ("/Host :(. *) \ r \ n/", $ req, $ match) {$ h = $ match [1];} if (preg_match ("/Origin :(. *) \ r \ n/", $ req, $ match) {$ o = $ match [1];} if (preg_match ("/Sec-WebSocket-Key: (. *) \ r \ n/", $ req, $ match) {$ key = $ match [1];} return array ($ r, $ h, $ o, $ key);} function calcKey ($ key) {// based on websocket version 13 $ accept = base64_encode (sha1 ($ key. '258eafa5-E914-47DA-95CA-C5AB0DC85B11 ', true); return $ accept;} function decode ($ buffer) {$ len = $ masks = $ data = $ decoded = null; $ len = ord ($ buffer [1]) & 127; if ($ len = 126) {$ masks = substr ($ buffer, 4, 4 ); $ data = substr ($ buffer, 8);} else if ($ len = 127) {$ masks = substr ($ buffer, 10, 4 ); $ data = substr ($ buffer, 14);} else {$ masks = substr ($ buffer, 2, 4); $ data = substr ($ buffer, 6 );} for ($ index = 0; $ index <strlen ($ data); $ index ++) {$ decoded. = $ data [$ index] ^ $ masks [$ index % 4];} return $ decoded;} function frame ($ s) {$ a = str_split ($ s, 125); if (count ($ a) = 1) {return "\ x81 ". chr (strlen ($ a [0]). $ a [0] ;}$ ns = ""; foreach ($ a as $ o) {$ ns. = "\ x81 ". chr (strlen ($ o )). $ o;} return $ ns;} function say ($ msg = "") {echo $ msg. "\ n";} function log ($ msg = "") {if ($ this-> debug) {echo $ msg. "\ n" ;}}$ wwss = new WS ('localhost', 4000 );

If I want to write the first parameter sent to the client, how can I write it?


Reply to discussion (solution)

$ Client = socket_accept ($ this-> master); // 27 rows

$ Bytes = @ socket_recv ($ socket, $ buffer, 2048,0) of 36 rows; receives data sent by the client.
47 when $ this-> send ($ socket, $ buffer); is to send data to the client

This is a test program that only sends data from the client as it is. Far from Practical use
You need to write a program to analyze and process the data sent by the user.
Return the corresponding data based on the processing result.
This program needs to be called between lines 46 and 47.

$ Bytes = @ socket_recv ($ socket, $ buffer, 2048,0) of 36 rows; receives data sent by the client.
47 when $ this-> send ($ socket, $ buffer); is to send data to the client

This is a test program that only sends data from the client as it is. Far from Practical use
You need to write a program to analyze and process the data sent by the user.
Return the corresponding data based on the processing result.
This program needs to be called between lines 46 and 47.


Is there any practical code? I have been searching for the code for a day. I don't see any reliable problems. as long as the client sends a and the server receives the data. if it is a, it returns a as it is.

Isn't your program like this?

The problem is that your client

Isn't your program like this?

The problem is that your client

The client I wrote in c ++. now I am writing to the server.

Then you downloadAndroid and PHP development best practices source code. The Download channel is available.
I have published a book. it should be okay.
There are Configuration instructions for http://blog.csdn.net/binyao02123202/article/details/11761931

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.