PHP socket connection to the server template, socket server _ PHP Tutorial

Source: Internet
Author: User
Tags socket connect
PHP socket connects to the server template, socket server. When the socket of PHP is connected to the server template, the socket server finds that some cached data needs to be accessed by external interfaces while organizing the new framework. What is more convenient is the php interface, PHP socket connection to the server template, socket server

When sorting out the new framework, I found that some cached data needs to be accessed through external interfaces, which is more convenient for php interfaces. so I temporarily studied how php connects to the java server.

First paste the code:
  1. require_once 'CRC16.php';
  2. /*-----------------------------
  3. | Send data packets to the server
  4. ------------------------------*/
  5. classServer{
  6. // Send data packets
  7. publicstaticfunction sendPacket($packet, $host, $port){
  8. $protocol ='tcp';
  9. $get_prot = getprotobyname ( $protocol );
  10. // Create a socket
  11. $socket = socket_create ( AF_INET, SOCK_STREAM, $get_prot );
  12. // Establish a connection
  13. $conn = socket_connect ( $socket, $host, $port );
  14. if(!$conn){
  15. socket_close($socket);
  16. exit("socket connect failed!");
  17. }
  18. $buffer =@socket_read($socket,9, PHP_NORMAL_READ);
  19. $crcCode =(ord($buffer[7])<<8)+ord($buffer[8]);
  20. $len = strlen($packet);
  21. $newpacket = CRC16::encode($packet, $crcCode,4);
  22. socket_send ( $socket, $newpacket, $len,0);
  23. // Wait for acceptance
  24. $head =@socket_read($socket,4,PHP_NORMAL_READ);
  25. $len =(ord($head[0])<<24)+(ord($head[1])<<16)+(ord($head[2])<<8)+ord($head[3]);
  26. $content =@socket_read($socket,$len-4,PHP_NORMAL_READ);
  27. socket_close ( $socket );
  28. return substr($content,3);
  29. }
  30. publicstaticfunction packet($group,$cmd,$message){
  31. $size = strlen($message)+8;
  32. $str ='';
  33. $str .=self::writeInt($size);
  34. $str .=self::writeByte(0);
  35. $str .=self::writeByte($group);
  36. $str .=self::writeByte($cmd);
  37. $str .=self::writeByte(1);
  38. $str .= $message;
  39. return $str;
  40. }
  41. // Write two bytes of data
  42. privatestaticfunction writeShort($s){
  43. return pack ("n", $s );
  44. }
  45. // Write 4 bytes of data
  46. privatestaticfunction writeInt($N){
  47. return pack ("N", $N );
  48. }
  49. // Write 1 byte of data
  50. privatestaticfunction writeByte($b){
  51. return pack ("c", $b );
  52. }
  53. }
After a personal server is connected, a crcCode verification code will be assigned, and the sent message needs to be encrypted with crc16 (in fact, it is very easy to keep a bit mysterious), so wait for a fixed length to get the crcCode, after sending the request, wait for receiving the data packet and return it.



From Weizhi note (Wiz)




How does PHP Socket communicate with the client?

The server provides a data buffer and a user identification mechanism. To send the corresponding information to the chat user.
It seems that the communication between the user and the user is essentially the communication between the user and the server.
My understanding

Php socket connection failure

Check the port
 

While organizing the new framework, the hacker finds that some cached data needs to be accessed by external interfaces, while the php interface is more convenient ,...

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.