After phpsocket programming is read, write blocking socket tcp php server
The server code is as follows:
Client, the code is as follows:
If you replace the server-side client read operation with a circular internal read operation, the client will remain in the wait data status.
Is there a way for the server to write data to the client after reading it cyclically?
Reply to discussion (solution)There is no error in this loop, but once the data is sent, the socket connection is closed. there is no need for a loop.
There is no error in this loop, but once the data is sent, the socket connection is closed. there is no need for a loop.
This is only a demo. in actual cases, the data sent by the client may not be long, so it can only be received cyclically.
The PHP_NORMAL_READ option should not be used in the read loop.
Removing PHP_NORMAL_READ does not solve the problem. Is there a solution?
While (true) {$ msg = socket_accept ($ socket); // accept a SOCKET if (! $ Msg) {echo "socket_accept () failed :". socket_strerror ($ msg ). "\ n"; break;} while (true) {$ command = strtoupper (trim (socket_read ($ msg, 1024); // wait for client data if (! $ Command) break; // related processing, such as directly returning socket_write ($ msg, $ command, strlen ($ command);} socket_close ($ msg ); if ($ command = "QUIT") // if the received message is break;} socket_close ($ socket); // Close the SOCKET
The PHP_NORMAL_READ option should not be used in the read loop.
Removing PHP_NORMAL_READ does not solve the problem. Is there a solution?
I use stream functions.
Ip = $ ip; $ this-> port = $ port; // self: init (); // $ this-> createServer (); $ this-> log ('listenning user... '); $ this-> listenningUser ();} protected function createServer () {$ errno; $ errstr; $ this-> webSocket = stream_socket_server ("tcp ://". $ this-> ip. ':'. $ this-> port, $ errno, $ errstr); if (! $ This-> webSocket) {self: log ("$ errstr ($ errno)"); exit () ;}$ this-> log ('server OK. ');} protected function listenningUser () {while (true) {$ this-> userIndex ++; $ user = $ this-> users [$ this-> userIndex] = stream_socket_accept ($ this-> webSocket, 9999999999 ); // if (is_resource ($ this-> users [$ this-> userIndex-1]) {$ u = $ this-> users [$ this-> userIndex-1]; $ u-> close (); $ u = NULL; unset ($ this-> users [$ th Is-> userIndex-1]);} // $ this-> log ('Connect to new user'); $ this-> listenningMessage ();}} protected function listenningMessage () {while (is_resource ($ this-> users [$ this-> userIndex]) {$ this-> message = stream_socket_recvfrom ($ this-> users [$ this-> userIndex], 10270000); if (! $ This-> message) {$ this-> closeUser (); break;} $ this-> messageOperate () ;}} function messageOperate () {$ this-> log ("received message:"); $ this-> log ($ this-> message); // mb_strstr ($ haystack, $ needle, $ before_needle, $ encoding) $ this-> sendMessage ('done');} function sendMessage ($ msg) {if ($ msg = '') {return-1 ;} return stream_socket_sendto ($ this-> users [$ this-> userIndex], $ msg);} public function closeUser () {if (! Is_resource ($ this-> users [$ this-> userIndex]) return FALSE; @ stream_socket_shutdown ($ this-> users [$ this-> userIndex], STREAM_SHUT_RDWR ); @ fclose ($ this-> users [$ this-> userIndex]); $ this-> log ("the user is disconnected. "); return TRUE;} public function shutdown () {stream_socket_shutdown ($ this-> webSocket, STREAM_SHUT_RDWR); fclose ($ this-> webSocket);} protected static function init () {error_reporting (E_ALL ^ E_NOTICE); set_time_limit (0); limit ('Asia/Shanghai'); ignore_user_abort (TRUE); mb_internal_encoding ('gbk ');} protected static function log ($ message) {echo "\ r \ n ". $ message. "\ r \ n" ;}}$ server = new SocketServer ();
Init (); $ this-> client = stream_socket_client ("tcp: // $ domain: $ port", $ errno, $ errstr, 300000); if (! $ This-> client) {$ this-> log ("$ errstr ($ errno)"); return FALSE ;}$ this-> log ('Client OK ');} protected static function init () {error_reporting (E_ALL ^ E_NOTICE); // error_reporting (0); set_time_limit (0); ob_implicit_flush (); publish ('Asia/Shanghai '); ignore_user_abort (TRUE); mb_internal_encoding ('gbk');} public function sendMessage ($ msg) {if ($ msg = '') {return-1;} try {str Eam_socket_sendto ($ this-> client, $ msg);} catch (Exception $ exc) {// $ this-> log ($ exc-> getTraceAsString ());}} public function getMessage () {$ this-> message = stream_socket_recvfrom ($ this-> client, 10270000); // $ this-> log ("received message :"); // $ this-> log ($ this-> message); fwrite (STDOUT, $ this-> message. "\ r \ n");} public function shutdown () {stream_socket_shutdown ($ this-> client, STREAM_SHUT_RDWR); fclose ($ th Is-> client);} public static function log ($ message) {echo "\ r \ n ". $ message. "\ r \ n" ;}}// $ client = new SocketClient ('2017. 0.0.1 ', 12345); while (true) {$ msg = fread (STDIN, 9999999); if (! Trim ($ msg) {continue;} $ client-> sendMessage ($ msg); $ client-> getMessage ();}
While (true) {$ msg = socket_accept ($ socket); // accept a SOCKET if (! $ Msg) {echo "socket_accept () failed :". socket_strerror ($ msg ). "\ n"; break;} while (true) {$ command = strtoupper (trim (socket_read ($ msg, 1024); // wait for client data if (! $ Command) break; // related processing, such as directly returning socket_write ($ msg, $ command, strlen ($ command);} socket_close ($ msg ); if ($ command = "QUIT") // if the received message is break;} socket_close ($ socket); // Close the SOCKET
Good moderator
Here, I need to read the data analysis from the client before writing the data back to the client. if you want the client to send the quit command to end the socket, this online environment cannot be implemented. Is there a better way? Thank you !!