您好,欢迎访问一九零五行业门户网

Php socket数据编码

bytes.php  字节编码类
/** * byte数组与字符串转化类 * @author * created on 2011-7-15 */class bytes { /** * 转换一个string字符串为byte数组 * @param $str 需要转换的字符串 * @param $bytes 目标byte数组 * @author zikie */ public static function getbytes($str) { $len = strlen($str); $bytes = array(); for($i=0;$i= 128){ $byte = ord($str[$i]) - 256; }else{ $byte = ord($str[$i]); } $bytes[] = $byte ; } return $bytes; } /** * 将字节数组转化为string类型的数据 * @param $bytes 字节数组 * @param $str 目标字符串 * @return 一个string类型的数据 */ public static function tostr($bytes) { $str = ''; foreach($bytes as $ch) { $str .= chr($ch); } return $str; } /** * 转换一个int为byte数组 * @param $byt 目标byte数组 * @param $val 需要转换的字符串 * @author zikie */ public static function integertobytes($val) { $byt = array(); $byt[0] = ($val & 0xff); $byt[1] = ($val >> 8 & 0xff); $byt[2] = ($val >> 16 & 0xff); $byt[3] = ($val >> 24 & 0xff); return $byt; } /** * 从字节数组中指定的位置读取一个integer类型的数据 * @param $bytes 字节数组 * @param $position 指定的开始位置 * @return 一个integer类型的数据 */ public static function bytestointeger($bytes, $position) { $val = 0; $val = $bytes[$position + 3] & 0xff; $val > 8 & 0xff); return $byt; } /** * 从字节数组中指定的位置读取一个short类型的数据。 * @param $bytes 字节数组 * @param $position 指定的开始位置 * @return 一个short类型的数据 */ public static function bytestoshort($bytes, $position) { $val = 0; $val = $bytes[$position + 1] & 0xff; $val = $val
socket.class.php  socket赋值类
defaulthost; } if($serverport == false) { $serverport = $this->defaultport; } $this->defaulthost = $serverhost; $this->defaultport = $serverport; if($timeout == false) { $timeout = $this->defaulttimeout; } $this->connection = socket_create(af_inet,sock_stream,sol_tcp); if(socket_connect($this->connection,$serverhost,$serverport) == false) { $errorstring = socket_strerror(socket_last_error($this->connection)); $this->_throwerror(connecting to {$serverhost}:{$serverport} failed.
reason: {$errorstring}); }else{ $this->_throwmsg(socket connected!); } $this->connectionstate = connected; } /** * disconnects from the server * * @return true on succes, false if the connection was already closed */ public function disconnect() { if($this->validateconnection()) { socket_close($this->connection); $this->connectionstate = disconnected; $this->_throwmsg(socket disconnected!); return true; } return false; } /** * sends a command to the server * * @return string server response */ public function sendrequest($command) { if($this->validateconnection()) { $result = socket_write($this->connection,$command,strlen($command)); return $result; } $this->_throwerror(sending command \{$command}\ failed.
reason: not connected); } public function isconn() { return $this->connectionstate; } public function getunreadbytes() { $info = socket_get_status($this->connection); return $info['unread_bytes']; } public function getconnname(&$addr, &$port) { if ($this->validateconnection()) { socket_getsockname($this->connection,$addr,$port); } } /** * gets the server response (not multilined) * * @return string server response */ public function getresponse() { $read_set = array($this->connection); while (($events = socket_select($read_set, $write_set = null, $exception_set = null, 0)) !== false) { if ($events > 0) { foreach ($read_set as $so) { if (!is_resource($so)) { $this->_throwerror(receiving response from server failed.
reason: not connected); return false; }elseif ( ( $ret = @socket_read($so,4096,php_binary_read) ) == false){ $this->_throwerror(receiving response from server failed.
reason: not bytes to read); return false; } return $ret; } } } return false; } public function waitforresponse() { if($this->validateconnection()) { return socket_read($this->connection, 2048); } $this->_throwerror(receiving response from server failed.
reason: not connected); return false; } /** * validates the connection state * * @return bool */ private function validateconnection() { return (is_resource($this->connection) && ($this->connectionstate != disconnected)); } /** * throws an error * * @return void */ private function _throwerror($errormessage) { echo socket error: . $errormessage; } /** * throws an message * * @return void */ private function _throwmsg($msg) { if ($this->debug) { echo socket message: . $msg . \n\n; } } /** * if there still was a connection alive, disconnect it */ public function __destruct() { $this->disconnect(); }}?>
packetbase.class.php  打包类
__call('__construct1', $args); break; case 2: $this->__call('__construct2', $args); break; default: throw new exception(); } } //无参数 public function __construct1($opcode) { $this->opcode = $opcode; $this->params = 0; } //有参数 public function __construct2($opcode, $params) { $this->opcode = $opcode; $this->params = $params; } //析构 function __destruct() { unset($this->head); unset($this->buf); } //打包 public function pack() { $head = $this->makehead($this->opcode,$this->params); return $head.$this->buf; } //解包 public function unpack($packet,$nohead = false) { $this->buf = $packet; if (!$nohead){ $recvhead = unpack(s2hd/i2pa,$packet); $sd = $recvhead[hd1];//sd $this->contentlen = $recvhead[hd2];//content len $this->opcode = $recvhead[pa1];//opcode $this->params = $recvhead[pa2];//params $this->pos = 12;//去除包头长度 if ($sd != 21316) { return false; } }else { $this->pos = 0; } return true; } public function getop() { if ($this->buf) { return $this->opcode; } return 0; } /************************private***************************/ //构造包头 private function makehead($opcode,$param) { return pack(ssii,sd,$this->tellput(),$opcode,$param); } //用以模拟函数重载 private function __call($name, $arg) { return call_user_func_array(array($this, $name), $arg); } /***********************uitl***************************/ //将16进制的op转成10进制 static function makeopcode($major_op, $minor_op) { return ((($major_op & 0xffff) buf = ; $this->contentlen = 0; $this->pos = 0; } function __destruct() { unset($this->buf); } public function putint($int) { $this->buf .= pack(i,(int)$int); } public function pututf($str) { $l = strlen($str); $this->buf .= pack(s,$l); $this->buf .= $str; } public function putstr($str) { return $this->pututf($str); } public function tellput() { return strlen($this->buf); } /*******************************************/ public function getint() { //$cont = substr($out,$l,4); $get = unpack(@.$this->pos./i,$this->buf); if (is_int($get[1])){ $this->pos += 4; return $get[1]; } return 0; } public function getshort() { $get = unpack(@.$this->pos./s,$this->buf); if (is_int($get[1])){ $this->pos += 2; return $get[1]; } return 0; } public function getutf() { $getstrlen = $this->getshort(); if ($getstrlen > 0) { $end = substr($this->buf,$this->pos,$getstrlen); $this->pos += $getstrlen; return $end; } return ''; } /***************************/ public function getbuf() { return $this->buf; } public function setbuf($strbuf) { $this->buf = $strbuf; } public function resetbuf(){ $this->buf = ; $this->contentlen = 0; $this->pos = 0; }}?>
格式
struct header{int type; // 消息类型int length; // 消息长度}struct msg_q2r2db_payresult{int serialno; int openid; char payitem[512];int billno; int zoneid;int providetype; int coins; }调用的方法,另外需require两个php文件,一个是字节编码类,另外一个socket封装类,其实主要看字节编码类就可以了!
调用测试
public function index() { $socketaddr = 127.0.0.1; $socketport = 10000; try { $selfpath = dirname ( __file__ ); require ($selfpath . /../tool/bytes.php); $bytes = new bytes (); $payitem = sdfasdfasdfsdfsdfsdfsdfsdfsdf; $serialno = 1; $zoneid = 22; $openid = cff47c448d4aa2069361567b6f8299c2; $billno = 1; $providetype = 1; $coins = 1; $headtype = 10001; $headlength = 56 + intval(strlen($payitem )); $headtype = $bytes->integertobytes ( intval ( $headtype ) ); $headlength = $bytes->integertobytes ( intval ( $headlength ) ); $serialno = $bytes->integertobytes ( intval ( $serialno ) ); $zoneid = $bytes->integertobytes ( intval ( $zoneid ) ); $openid = $bytes->getbytes( $openid ); $payitem_len = $bytes->integertobytes ( intval ( strlen($payitem) ) ); $payitem = $bytes->getbytes($payitem); $billno = $bytes->integertobytes ( intval ( $billno ) ); $providetype = $bytes->integertobytes ( intval ( $providetype ) ); $coins = $bytes->integertobytes ( intval ( $coins ) ); $return_betys = array_merge ($headtype , $headlength , $serialno , $zoneid , $openid,$payitem_len ,$payitem,$billno,$providetype,$coins); $msg = $bytes->tostr ($return_betys); $strlen = strlen($msg); $packet = pack(a{$strlen}, $msg); $pcklen = strlen($packet); $socket = socket::singleton (); $socket->connect ( $socketaddr, $socketport ); //连服务器 $sockresult = $socket->sendrequest ( $packet); // 将包发送给服务器 sleep ( 3 ); $socket->disconnect (); //关闭链接 } catch ( exception $e ) { var_dump($e); $this->log_error(pay order send to server.$e->getmessage()); } }
其它类似信息

推荐信息