PHP使用Socket发送字节流
2013-01-04 10:27
351 查看
例如,需要发送以下数据
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封装类,其实主要看字节编码类就可以了!
Bytes.php 字节编码类
View Code
可能你看的有点迷糊,因为我也不知道该怎么解释,有空梳理一下~
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()); } }
Bytes.php 字节编码类
View Code
1 <?php 2 /** 3 * PacketBase class 4 * 5 * 用以处理与c++服务端交互的sockets 包 6 * 7 * 注意:不支持宽字符 8 * 9 * @author Seven <seven@qoolu.com> 10 * 11 */ 12 class PacketBase extends ContentHandler 13 { 14 private $head; 15 private $params; 16 private $opcode; 17 /**************************construct***************************/ 18 function __construct() 19 { 20 $num = func_num_args(); 21 $args = func_get_args(); 22 switch($num){ 23 case 0: 24 //do nothing 用来生成对象的 25 break; 26 case 1: 27 $this->__call('__construct1', $args); 28 break; 29 case 2: 30 $this->__call('__construct2', $args); 31 break; 32 default: 33 throw new Exception(); 34 } 35 } 36 //无参数 37 public function __construct1($OPCODE) 38 { 39 $this->opcode = $OPCODE; 40 $this->params = 0; 41 } 42 //有参数 43 public function __construct2($OPCODE, $PARAMS) 44 { 45 $this->opcode = $OPCODE; 46 $this->params = $PARAMS; 47 } 48 //析构 49 function __destruct() 50 { 51 unset($this->head); 52 unset($this->buf); 53 } 54 55 //打包 56 public function pack() 57 { 58 $head = $this->MakeHead($this->opcode,$this->params); 59 return $head.$this->buf; 60 } 61 //解包 62 public function unpack($packet,$noHead = false) 63 { 64 65 $this->buf = $packet; 66 if (!$noHead){ 67 $recvHead = unpack("S2hd/I2pa",$packet); 68 $SD = $recvHead[hd1];//SD 69 $this->contentlen = $recvHead[hd2];//content len 70 $this->opcode = $recvHead[pa1];//opcode 71 $this->params = $recvHead[pa2];//params 72 73 $this->pos = 12;//去除包头长度 74 75 if ($SD != 21316) 76 { 77 return false; 78 } 79 }else 80 { 81 $this->pos = 0; 82 } 83 return true; 84 } 85 public function GetOP() 86 { 87 if ($this->buf) 88 { 89 return $this->opcode; 90 } 91 return 0; 92 } 93 /************************private***************************/ 94 //构造包头 95 private function MakeHead($opcode,$param) 96 { 97 return pack("SSII","SD",$this->TellPut(),$opcode,$param); 98 } 99 100 //用以模拟函数重载 101 private function __call($name, $arg) 102 { 103 return call_user_func_array(array($this, $name), $arg); 104 } 105 106 107 /***********************Uitl***************************/ 108 //将16进制的op转成10进制 109 static function MakeOpcode($MAJOR_OP, $MINOR_OP) 110 { 111 return ((($MAJOR_OP & 0xffff) << 16) | ($MINOR_OP & 0xffff)); 112 } 113 } 114 /** 115 * 包体类 116 * 包含了对包体的操作 117 */ 118 class ContentHandler 119 { 120 public $buf; 121 public $pos; 122 public $contentlen;//use for unpack 123 124 function __construct() 125 { 126 $this->buf = ""; 127 $this->contentlen = 0; 128 $this->pos = 0; 129 } 130 function __destruct() 131 { 132 unset($this->buf); 133 } 134 135 public function PutInt($int) 136 { 137 $this->buf .= pack("i",(int)$int); 138 } 139 public function PutUTF($str) 140 { 141 $l = strlen($str); 142 $this->buf .= pack("s",$l); 143 $this->buf .= $str; 144 } 145 public function PutStr($str) 146 { 147 return $this->PutUTF($str); 148 } 149 150 151 public function TellPut() 152 { 153 return strlen($this->buf); 154 } 155 156 157 /*******************************************/ 158 159 public function GetInt() 160 { 161 //$cont = substr($out,$l,4); 162 $get = unpack("@".$this->pos."/i",$this->buf); 163 if (is_int($get[1])){ 164 $this->pos += 4; 165 return $get[1]; 166 } 167 return 0; 168 } 169 public function GetShort() 170 { 171 $get = unpack("@".$this->pos."/S",$this->buf); 172 if (is_int($get[1])){ 173 $this->pos += 2; 174 return $get[1]; 175 } 176 return 0; 177 } 178 public function GetUTF() 179 { 180 $getStrLen = $this->GetShort(); 181 182 if ($getStrLen > 0) 183 { 184 $end = substr($this->buf,$this->pos,$getStrLen); 185 $this->pos += $getStrLen; 186 return $end; 187 } 188 return ''; 189 } 190 /***************************/ 191 192 public function GetBuf() 193 { 194 return $this->buf; 195 } 196 197 public function SetBuf($strBuf) 198 { 199 $this->buf = $strBuf; 200 } 201 202 public function ResetBuf(){ 203 $this->buf = ""; 204 $this->contentlen = 0; 205 $this->pos = 0; 206 } 207 208 } 209 210 ?>
可能你看的有点迷糊,因为我也不知道该怎么解释,有空梳理一下~
相关文章推荐
- PHP使用Socket发送字节流
- PHP使用Socket发送字节流
- socket编程之使用socket发送电子邮件(PHP)
- PHP使用Socket发送字节流
- PHP使用socket方式发送邮件,无需SMTP服务
- 使用php的socket发送tcp请求
- PHP使用socket发送HTTP请求的方法
- PHP中以SOCKET方式发送HTTP请求的函数及使用方法
- 使用Pear和php socket来实现通过smtp验证发送邮件
- PHP使用socket发送邮件
- PHP使用socket发送HTTP请求的方法
- 使用SOCKET发送HTTP请求
- 使用PHP的Socket写的POP3类(一)
- 使用php通过Socket进行发信源码,支持发信认证
- 使用socket方式连接Nginx优化php-fpm性能
- php使用pdo连接mysql数据库如何设置发送的字符集?
- PHP使用PHPMailer发送邮件
- C#使用Socket发送和接收TCP数据实例
- nginx 和 php-fpm 通信使用unix socket还是TCP,及其配置
- 在PHP中使用Pear 的NetMail发送smtp邮件