Home  >  Article  >  Backend Development  >  A very simple socket client PHP class

A very simple socket client PHP class

WBOY
WBOYOriginal
2016-07-25 08:42:28928browse
PHP 5, you need to open the socket extension
  1. //socke operation class
  2. class Socket {
  3. private $host;//the host connected to the socket
  4. private $port;//the port number of the socket
  5. private $error=array() ;
  6. private $socket=null;//The connection identifier of the socket
  7. private $queryStr="";//The data sent
  8. public function __construct($host,$port) {
  9. if(!extension_loaded("sockets")) {
  10. exit("Please open the socket extension");
  11. }
  12. if(empty($host)) exit("Please enter the target address");
  13. if(empty($port)) exit("Please enter a valid port No.");
  14. $this->host=$host;
  15. $this->port=$port;
  16. $this->CreateSocket();//Create connection
  17. }
  18. //Create socket
  19. private function CreateSocket(){
  20. !$this->socket&&$this->socket=socket_create(AF_INET, SOCK_STREAM, SOL_TCP);//Create socket
  21. $r=@socket_connect($this->socket,$this- >host,$this->port);
  22. if($r){
  23. return $r;
  24. }else{
  25. $this->error[]=socket_last_error($this->socket);
  26. return false;
  27. }
  28. }
  29. //Write data to the socket server and read
  30. public function wr($contents){
  31. $this->queryStr="";
  32. $this->queryStr=$contents;
  33. !$this->socket&&$this->CreateSocket();
  34. $contents=$this->fliterSendData($contents);
  35. $result=socket_write($this->socket,$contents,strlen( $contents));
  36. if(!intval($result)){
  37. $this->error[]=socket_last_error($this->socket);
  38. return false;
  39. }
  40. $response=socket_read($this ->socket,12048);
  41. if(false===$response){
  42. $this->error[]=socket_last_error($this->socket);
  43. return false;
  44. }
  45. return $response;
  46. }
  47. //Filter the sent data
  48. private function filterSendData($contents){
  49. //Process the written data
  50. return $contents;
  51. }
  52. //All error messages
  53. public function getError(){
  54. return $this->error;
  55. }
  56. //Last error message
  57. public function getLastError(){
  58. return $this->error(count($this->error));
  59. }
  60. //Get the last message sent
  61. public function getLastMsg(){
  62. return $this->queryStr;
  63. }
  64. public function getHost(){
  65. return $this->host;
  66. }
  67. public function getPort(){
  68. return $this->port;
  69. }
  70. //Close the socket connection
  71. private function close(){
  72. $this->socket&&socket_close($this->socket);//Close Connection
  73. $this->socket=null;//Connection resource initialization
  74. }
  75. public function __destruct(){
  76. $this->close();
  77. }
  78. }
Copy code

Very simple, socket, PHP


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn