PHP+socket游戏数据统计平台发包接包类库
<?php /** * @title: PHP+socket游戏数据统计平台发包接包类库 * @version: 1.0 * @author: perry <perry@1kyou.com> * @published: 2013-05-2 */ class TPkgxmk{ var $socket; var $connection; var $IP; var $Port; var $buffer; var $buffer_len; function __construct($IP,$Port){ $this->IP = $IP; $this->Port = $Port; $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)or die("Could not create socket\n"); //创建一个socket $this->connection = socket_connect($this->socket, $this->IP, $this->Port) or die("Could not connet server\n"); //连接服务器 } function SendData(){ socket_write($this->socket, $this->buffer) or die("Write failed\n"); //把数据写到缓冲区 //循环读取回包 while ($this->buffer = socket_read($this->socket,65535,PHP_BINARY_READ)){ if(empty($this->buffer)) $i++; if(empty($this->buffer) && $i==5){ exit('Could not read backdata'); } $this->buffdata.=$this->buffer; } } //组合包 function Endata($data){ $head="POST /PostBulletin HTTP/1.1\r\n"; $head.="Connection:Close\r\nContent-Length:10\r\n\r\n"; $pre = $data."malimalihong"; //数据附加字符串 $sign = md5($pre); $data.="&sign=".$sign; $this->buffer = $head.$data; return $this->buffer; } //获取回包 function getdata($code="DECODE"){ if(!empty($this->buffdata)){ if($code=='DECODE'){ $result = str_replace("\n","<br />", htmlspecialchars($this->buffdata)); //$res = explode("<br /><br />", $result); return $result; }else{ // echo $this->buffdata; return $this->buffdata; } } } } //使用例子 //$strs = "cuttle=3021&parm0=perry&parm1=1&parm2=3&parm3=1&sign"; //$strs.="malimalihong"; //$sing = md5($strs); //$strs.=$sing; //$tp = new TPkgxmk("58.254.217.74", "8065"); //$tp->Endata($strs); //$tp->SendData(); //$arr = $tp->getdata(); //$aa = str_replace("\n","<br />", htmlspecialchars($arr)); // //echo $aa;