PHP socket connection to service-side template, Socket server _php Tutorial

Source: Internet
Author: User
Tags socket connect

The socket of PHP is connected to the service-side template, the socket server


In the new framework, the discovery of some cached data, external interface access, and more convenient is the PHP interface, so the temporary study of the next PHP how to connect to the Java server.

First put the code:
  1. php
  2. require_once 'CRC16.php';
  3. /*-----------------------------
  4. | 向服务器发送数据包
  5. ------------------------------*/
  6. classServer{
  7. //发送数据包
  8. publicstaticfunction sendPacket($packet, $host, $port){
  9. $protocol ='tcp';
  10. $get_prot = getprotobyname ( $protocol );
  11. //创建socket
  12. $socket = socket_create ( AF_INET, SOCK_STREAM, $get_prot );
  13. //建立连接
  14. $conn = socket_connect ( $socket, $host, $port );
  15. if(!$conn){
  16. socket_close($socket);
  17. exit("socket connect failed!");
  18. }
  19. $buffer =@socket_read($socket,9, PHP_NORMAL_READ);
  20. $crcCode =(ord($buffer[7])<<8)+ord($buffer[8]);
  21. $len = strlen($packet);
  22. $newpacket = CRC16::encode($packet, $crcCode,4);
  23. socket_send ( $socket, $newpacket, $len,0);
  24. //等待接受
  25. $head =@socket_read($socket,4,PHP_NORMAL_READ);
  26. $len = ( ord ( $head [ 0 ]) << 24 ) + ( ord ( $head [ 1 ]) << 16 ) + ( ord ( $head [ 2 ]) << 8 ) + ord ( $head [ 3 ]);
  27. $content =@socket_read($socket,$len-4,PHP_NORMAL_READ);
  28. socket_close ( $socket );
  29. return substr($content,3);
  30. }
  31. publicstaticfunction packet($group,$cmd,$message){
  32. $size = strlen($message)+8;
  33. $str ='';
  34. $str .=self::writeInt($size);
  35. $str .=self::writeByte(0);
  36. $str .=self::writeByte($group);
  37. $str .=self::writeByte($cmd);
  38. $str .=self::writeByte(1);
  39. $str .= $message;
  40. return $str;
  41. }
  42. //写进2个byte的数据
  43. privatestaticfunction writeShort($s){
  44. return pack ("n", $s );
  45. }
  46. //写进4个byte的数据
  47. privatestaticfunction writeInt($N){
  48. return pack ("N", $N );
  49. }
  50. //写进1个byte的数据
  51. privatestaticfunction writeByte($b){
  52. return pack ("c", $b );
  53. }
  54. }
Since the personal server is connected, a Crccode checksum is assigned, and the message sent needs to be CRC16 encrypted (in fact very simple, keeping the dots secret), so wait for a fixed length to get Crccode, then send the request, wait for the packet to be received, and return.



From for notes (Wiz)




How does a PHP socket communicate with the client on the server?

The server provides a data buffer and provides a mechanism for user identification. This is to send the corresponding information to both users of the chat.
It appears that the communication between the user and the user is essentially the user communicating with the server.
My point of understanding

The PHP socket is not connected

Check the port.

http://www.bkjia.com/PHPjc/880092.html www.bkjia.com true http://www.bkjia.com/PHPjc/880092.html techarticle PHP Socket connection to the service-side template, the socket server in the new framework, the discovery of some cached data, the need for external interface access, and more convenient is the PHP interface, ...

  • 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.