鍦ㄦ煇娆¢」鐩?腑闇?瑕佷笌c++鐨勬帴鍙h繘琛宻ocket閫氫俊锛屾暟鎹?殑鍙戦?佷笌鎺ユ敹閮芥槸鏁版嵁娴侊紝闇?瑕佸皢鍙戦?佺殑鏁版嵁杞?寲涓烘暟鎹?祦锛屽啀閫氳繃socket鍙戦?佸埌瀵瑰簲鐨勭?鍙c??/p>
瑕佺偣锛?/p>
1瀵瑰簲鐨勬暟鎹?寘缁撴瀯锛圕++涓嶱HP涓?鑷达級
2 pack鍑芥暟浣跨敤
3 pack鍑芥暟format鍙傛暟锛堢?涓?涓?弬鏁扮殑姝g‘浣跨敤锛夛紝瑕佽窡c++鍙戦?佺殑鏁版嵁鍙奀 type 涓?鑷?/p>
4 format 涓?湪姣忎釜format 瀵瑰簲鍚庨潰 姣忎竴涓?弬鏁帮紝瑕佸湪鍚庨潰璺熼暱搴︿緥锛?data = pack('A5C','melon',1)
5 unpack 鍑芥暟涓?殑format 瀵瑰簲澶氫釜鍑芥暟锛岀敤鈥?鈥?span style="line-height: 1.5;">鍒嗛殧 渚嬶細鐢ㄤ笂闈㈢殑$data 锛寀npack("A5mystr/Cmyint",$data),杈撳嚭array('mystr'=>'melon','myint'=>1)
聽
渚?鏁版嵁鍖呯粨鏋?涓猴細鏁版嵁鍖呭ご(2Byte) 鍖呭惈鏁版嵁澶у皬, 鏁版嵁鍐呭?锛屸?橽0鈥欑粨灏剧殑瀛楃?涓?/p>
聽
<?php $host='127.0.0.1; $port= 8080; $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); if(!$socket){ exit('鍒涘缓socket澶辫触'); } $result = socket_connect($socket,$host,$port); $message = "recharge"; $mlength = strlen($message); $length = strlen($message) + 3; $fmt_data = pack("sA".$mlength."C", $length, $message, 0); var_dump(strval($fmt_data)); $result=socket_write($socket,$fmt_data); if(!$result){ exit('鍙戦?佷俊鎭?け璐?); } $out = socket_read($socket,128); $recvmessage = strlen($out)-3; $ufmt_data = unpack("slenth/A".$recvmessage."meg/Cend",$out); var_dump($ufmt_data); socket_close($socket);
聽
聽
聽闄勶細
format鍙傛暟涓?枃璇存槑锛?/p>
pack/unpack 鐨勬懜鏉垮瓧绗﹀瓧绗?鍚?箟
a 涓?涓?~鍏呯┖鐨勫瓧鑺備覆
A 涓?涓?~鍏呯┖鏍肩殑瀛楄妭涓?/p>
b 涓?涓?綅涓诧紝鍦ㄦ瘡涓?瓧鑺傞噷浣嶇殑椤哄簭閮芥槸鍗囧簭
B 涓?涓?綅涓诧紝鍦ㄦ瘡涓?瓧鑺傞噷浣嶇殑椤哄簭閮芥槸闄嶅簭
c 涓?涓?湁绗﹀彿 char锛?浣嶆暣鏁帮級鍊?/p>
C 涓?涓?棤绗﹀彿 char锛?浣嶆暣鏁帮級鍊硷紱鍏充簬 Unicode 鍙傞槄 U
d 鏈?満鏍煎紡鐨勫弻绮惧害娴?偣鏁?/p>
f 鏈?満鏍煎紡鐨勫崟绮惧害娴?偣鏁?/p>
h 涓?涓?崄鍏?繘鍒朵覆锛屼綆鍥涗綅鍦ㄥ墠
H 涓?涓?崄鍏?繘鍒朵覆锛岄珮鍥涗綅鍦ㄥ墠
i 涓?涓?湁绗﹀彿鏁存暟鍊硷紝鏈?満鏍煎紡
I 涓?涓?棤绗﹀彿鏁存暟鍊硷紝鏈?満鏍煎紡
l 涓?涓?湁绗﹀彿闀挎暣褰?紝鎬绘槸 32 浣?/p>
L 涓?涓?棤绗﹀彿闀挎暣褰?紝鎬绘槸 32 浣?/p>
n 涓?涓?16浣嶇煭鏁村舰锛屸?滅綉缁溾?濆瓧鑺傚簭锛堝ぇ澶村湪鍓嶏級
N 涓?涓?32 浣嶇煭鏁村舰锛屸?滅綉缁溾?濆瓧鑺傚簭锛堝ぇ澶村湪鍓嶏級
p 涓?涓?寚鍚戠┖缁撳熬鐨勫瓧涓茬殑鎸囬拡
P 涓?涓?寚鍚戝畾闀垮瓧涓茬殑鎸囬拡
q 涓?涓?湁绗﹀彿鍥涘?嶏紙64浣嶆暣鏁帮級鍊?/p>
Q 涓?涓?棤绗﹀彿鍥涘?嶏紙64浣嶆暣鏁帮級鍊?/p>
s 涓?涓?湁绗﹀彿鐭?暣鏁板?硷紝鎬绘槸 16 浣?/p>
S 涓?涓?棤绗﹀彿鐭?暣鏁板?硷紝鎬绘槸 16 浣嶏紝瀛楄妭搴忚窡鏈哄櫒鑺?墖鏈夊叧
u 涓?涓?棤缂栫爜鐨勫瓧涓?/p>
U 涓?涓?Unicode 瀛楃?鏁板瓧
v 涓?涓??淰AX鈥濆瓧鑺傚簭锛堝皬澶村湪鍓嶏級鐨?16 浣嶇煭鏁存暟
V 涓?涓??淰AX鈥濆瓧鑺傚簭锛堝皬澶村湪鍓嶏級鐨?32 浣嶇煭鏁存暟
w 涓?涓?BER 鍘嬬缉鐨勬暣鏁?/p>
x 涓?涓?┖瀛楄妭锛堝悜鍓嶅拷鐣ヤ竴涓?瓧鑺傦級
X 澶囦唤涓?涓?瓧鑺?/p>
Z 涓?涓?┖缁撴潫鐨勶紙鍜岀┖濉?厖鐨勶級瀛楄妭涓?/p>
@ 鐢ㄧ┖瀛楄妭濉?厖缁濆?浣嶇疆