When sorting out the new framework, I found that some cached data requires external interface access, which is more convenient It is the interface of php, so I temporarily studied how php connects to the java server.
Paste the code first:<span class="pun"><?</span><span class="pln">php</span>
<span class="pln">require_once </span><span class="str">'CRC16.php'</span><span class="pun">;</span>
<span class="com">/*-----------------------------</span>
<span class="com"> | 向服务器发送数据包</span>
<span class="com"> ------------------------------*/</span>
<span class="kwd">class</span><span class="typ">Server</span><span class="pun">{</span>
<span class="com">//发送数据包</span>
<span class="kwd">public</span><span class="kwd">static</span><span class="kwd">function</span><span class="pln"> sendPacket</span><span class="pun">(</span><span class="pln">$packet</span><span class="pun">,</span><span class="pln"> $host</span><span class="pun">,</span><span class="pln"> $port</span><span class="pun">)</span><span class="pun">{</span>
<span class="pln"> $protocol </span><span class="pun">=</span><span class="str">'tcp'</span><span class="pun">;</span>
<span class="pln"> $get_prot </span><span class="pun">=</span><span class="pln"> getprotobyname </span><span class="pun">(</span><span class="pln"> $protocol </span><span class="pun">);</span>
<span class="com">//创建socket</span>
<span class="pln"> $socket </span><span class="pun">=</span><span class="pln"> socket_create </span><span class="pun">(</span><span class="pln"> AF_INET</span><span class="pun">,</span><span class="pln"> SOCK_STREAM</span><span class="pun">,</span><span class="pln"> $get_prot </span><span class="pun">);</span>
<span class="com">//建立连接</span>
<span class="pln"> $conn </span><span class="pun">=</span><span class="pln"> socket_connect </span><span class="pun">(</span><span class="pln"> $socket</span><span class="pun">,</span><span class="pln"> $host</span><span class="pun">,</span><span class="pln"> $port </span><span class="pun">);</span>
<span class="kwd">if</span><span class="pun">(!</span><span class="pln">$conn</span><span class="pun">){</span>
<span class="pln"> socket_close</span><span class="pun">(</span><span class="pln">$socket</span><span class="pun">);</span>
<span class="kwd">exit</span><span class="pun">(</span><span class="str">"socket connect failed!"</span><span class="pun">);</span>
<span class="pun">}</span>
<span class="pln"> $buffer </span><span class="pun">=</span><span class="lit">@socket_read</span><span class="pun">(</span><span class="pln">$socket</span><span class="pun">,</span><span class="lit">9</span><span class="pun">,</span><span class="pln"> PHP_NORMAL_READ</span><span class="pun">);</span>
$buffer
<span class="pln"> $crcCode </span><span class="pun">=</span><span class="pun">(</span><span class="pln">ord</span><span class="pun">(</span><span class="pln">$buffer</span><span class="pun">[</span><span class="lit">7</span><span class="pun">])<<</span><span class="lit">8</span><span class="pun">)+</span><span class="pln">ord</span><span class="pun">(</span><span class="pln">$buffer</span><span class="pun">[</span><span class="lit">8</span><span class="pun">]);</span>
@socket_read
<span class="pln"> $len </span><span class="pun">=</span><span class="pln"> strlen</span><span class="pun">(</span><span class="pln">$packet</span><span class="pun">);</span>
$socket
<span class="pln"> $newpacket </span><span class="pun">=</span><span class="pln"> CRC16</span><span class="pun">::</span><span class="pln">encode</span><span class="pun">(</span><span class="pln">$packet</span><span class="pun">,</span><span class="pln"> $crcCode</span><span class="pun">,</span><span class="lit">4</span><span class="pun">);</span>
9
<span class="pln"> socket_send </span><span class="pun">(</span><span class="pln"> $socket</span><span class="pun">,</span><span class="pln"> $newpacket</span><span class="pun">,</span><span class="pln"> $len</span><span class="pun">,</span><span class="lit">0</span><span class="pun">);</span>
PHP_NORMAL_READ
<span class="com">//等待接受</span>
<span class="pln"> $head </span><span class="pun">=</span><span class="lit">@socket_read</span><span class="pun">(</span><span class="pln">$socket</span><span class="pun">,</span><span class="lit">4</span><span class="pun">,</span><span class="pln">PHP_NORMAL_READ</span><span class="pun">);</span>
<span class="pln"> $len </span><span class="pun">=</span><span class="pun">(</span><span class="pln">ord</span><span class="pun">(</span><span class="pln">$head</span><span class="pun">[</span><span class="lit">0</span><span class="pun">])<<</span><span class="lit">24</span><span class="pun">)+(</span><span class="pln">ord</span><span class="pun">(</span><span class="pln">$head</span><span class="pun">[</span><span class="lit">1</span><span class="pun">])<<</span><span class="lit">16</span><span class="pun">)+(</span><span class="pln">ord</span><span class="pun">(</span><span class="pln">$head</span><span class="pun">[</span><span class="lit">2</span><span class="pun">])<<</span><span class="lit">8</span><span class="pun">)+</span><span class="pln">ord</span><span class="pun">(</span><span class="pln">$head</span><span class="pun">[</span><span class="lit">3</span><span class="pun">]);</span>
=<span class="pln"> $content </span><span class="pun">=</span><span class="lit">@socket_read</span><span class="pun">(</span><span class="pln">$socket</span><span class="pun">,</span><span class="pln">$len</span><span class="pun">-</span><span class="lit">4</span><span class="pun">,</span><span class="pln">PHP_NORMAL_READ</span><span class="pun">);</span>
ord
<span class="pln"> socket_close </span><span class="pun">(</span><span class="pln"> $socket </span><span class="pun">);</span>
$buffer
<span class="kwd">return</span><span class="pln"> substr</span><span class="pun">(</span><span class="pln">$content</span><span class="pun">,</span><span class="lit">3</span><span class="pun">);</span>
7
<span class="lit">8<span class="pun">)+<span class="pln">ord(<span class="pln">$buffer[<span class="lit">8]);
<span class="pln"> $len =<span class="pln"> strlen(<span class="pln">$packet);
<span class="pln"> $newpacket =<span class="pln"> CRC16::<span class="pln">encode(<span class="pln">$packet,<span class="pln"> $crcCode,<span class="lit">4);
<span class="pln"> socket_send (<span class="pln"> $socket,<span class="pln"> $newpacket,<span class="pln"> $len,<span class="lit">0);
<span class="com">//等待接受
<span class="pln"> $head =<span class="lit">@socket_read(<span class="pln">$socket,<span class="lit">4,<span class="pln">PHP_NORMAL_READ);
<span class="pln"> $len =(<span class="pln">ord(<span class="pln">$head[<span class="lit">0])<<<span class="lit">24)+(<span class="pln">ord(<span class="pln">$head[<span class="lit">1])<<<span class="lit">16)+(<span class="pln">ord(<span class="pln">$head[<span class="lit">2])<<<span class="lit">8)+<span class="pln">ord(<span class="pln">$head[<span class="lit">3]);
<span class="pln"> $content =<span class="lit">@socket_read(<span class="pln">$socket,<span class="pln">$len-<span class="lit">4,<span class="pln">PHP_NORMAL_READ);
<span class="pln"> socket_close (<span class="pln"> $socket );
<span class="kwd">return<span class="pln"> substr(<span class="pln">$content,<span class="lit">3);
}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
<span class="kwd">public</span><span class="kwd">static</span><span class="kwd">function</span><span class="pln"> packet</span><span class="pun">(</span><span class="pln">$group</span><span class="pun">,</span><span class="pln">$cmd</span><span class="pun">,</span><span class="pln">$message</span><span class="pun">){</span>
<span class="pln"> $size </span><span class="pun">=</span><span class="pln"> strlen</span><span class="pun">(</span><span class="pln">$message</span><span class="pun">)+</span><span class="lit">8</span><span class="pun">;</span>
function<span class="pln"> $str </span><span class="pun">=</span><span class="str">''</span><span class="pun">;</span>
(<span class="pln"> $str </span><span class="pun">.=</span><span class="kwd">self</span><span class="pun">::</span><span class="pln">writeInt</span><span class="pun">(</span><span class="pln">$size</span><span class="pun">);</span>
,<span class="pln"> $str </span><span class="pun">.=</span><span class="kwd">self</span><span class="pun">::</span><span class="pln">writeByte</span><span class="pun">(</span><span class="lit">0</span><span class="pun">);</span>
,<span class="pln"> $str </span><span class="pun">.=</span><span class="kwd">self</span><span class="pun">::</span><span class="pln">writeByte</span><span class="pun">(</span><span class="pln">$group</span><span class="pun">);</span>
){<span class="pln"> $str </span><span class="pun">.=</span><span class="kwd">self</span><span class="pun">::</span><span class="pln">writeByte</span><span class="pun">(</span><span class="pln">$cmd</span><span class="pun">);</span>
<span class="pln"> $str </span><span class="pun">.=</span><span class="kwd">self</span><span class="pun">::</span><span class="pln">writeByte</span><span class="pun">(</span><span class="lit">1</span><span class="pun">);</span>
<span class="pln"> $str </span><span class="pun">.=</span><span class="pln"> $message</span><span class="pun">;</span>
<span class="kwd">return</span><span class="pln"> $str</span><span class="pun">;</span>
<span class="pun">}</span>
<span class="com">//写进2个byte的数据</span>
$str
<span class="kwd">private</span><span class="kwd">static</span><span class="kwd">function</span><span class="pln"> writeShort</span><span class="pun">(</span><span class="pln">$s</span><span class="pun">)</span><span class="pun">{</span>
''
<span class="kwd">return</span><span class="pln"> pack </span><span class="pun">(</span><span class="str">"n"</span><span class="pun">,</span><span class="pln"> $s </span><span class="pun">);</span>
<span class="pun">}</span>
<span class="com">//写进4个byte的数据</span>
.=<span class="kwd">private</span><span class="kwd">static</span><span class="kwd">function</span><span class="pln"> writeInt</span><span class="pun">(</span><span class="pln">$N</span><span class="pun">)</span><span class="pun">{</span>
::<span class="kwd">return</span><span class="pln"> pack </span><span class="pun">(</span><span class="str">"N"</span><span class="pun">,</span><span class="pln"> $N </span><span class="pun">);</span>
(<span class="pun">}</span>
);<span class="com">//写进1个byte的数据</span>
<span class="kwd">private</span><span class="kwd">static</span><span class="kwd">function</span><span class="pln"> writeByte</span><span class="pun">(</span><span class="pln">$b</span><span class="pun">)</span><span class="pun">{</span>
<span class="kwd">return</span><span class="pln"> pack </span><span class="pun">(</span><span class="str">"c"</span><span class="pun">,</span><span class="pln"> $b </span><span class="pun">);</span>
<span class="pun">(<span class="lit">0);
<span class="pln"> $str .=<span class="kwd">self::<span class="pln">writeByte(<span class="pln">$group);
<span class="pln"> $str .=<span class="kwd">self::<span class="pln">writeByte(<span class="pln">$cmd);
<span class="pln"> $str .=<span class="kwd">self::<span class="pln">writeByte(<span class="lit">1);
<span class="pln"> $str .=<span class="pln"> $message;
<span class="kwd">return<span class="pln"> $str;
}
<span class="com">//写进2个byte的数据
<span class="kwd">private<span class="kwd">static<span class="kwd">function<span class="pln"> writeShort(<span class="pln">$s){
<span class="kwd">return<span class="pln"> pack (<span class="str">"n",<span class="pln"> $s );
}
<span class="com">//写进4个byte的数据
<span class="kwd">private<span class="kwd">static<span class="kwd">function<span class="pln"> writeInt(<span class="pln">$N){
<span class="kwd">return<span class="pln"> pack (<span class="str">"N",<span class="pln"> $N );
}
<span class="com">//写进1个byte的数据
<span class="kwd">private<span class="kwd">static<span class="kwd">function<span class="pln"> writeByte(<span class="pln">$b){
<span class="kwd">return<span class="pln"> pack (<span class="str">"c",<span class="pln"> $b );
}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>
}<span class="pun">}</span>
Because after the personal server is connected, a crcCode verification code will be assigned, and the sent message needs to be encrypted by crc16 (actually very simple, keep it mysterious), so first wait for the fixed length to be accepted to obtain the crcCode, and then send the request. Wait for the packet to be accepted and return.
From Wiz Notes (Wiz)
The server provides a data buffer and provides a user identification mechanism. This is used to send corresponding information to both chat users.
What seems like user-to-user communication is actually the user communicating with the server.
My little understanding
Check the port