We studied PHP and C++socket communication, using C + + as the server side, PHP as the client.
Socket communication is protocol-based, so as long as the two parties agree on the line.
About the choice of protocol: I have seen most of the protocols on the Internet are in the application layer of protocol, the use of such a protocol is very convenient, basically is the string passed over, passed
The protocol for this study is a standard practice for today's internationalization. Length+flag+body (length + type + content) way,
Total_length |
Code |
Flag |
Length1 |
String1 |
Length2 |
string2 |
Total length |
Type of operation |
Sign |
String 1 length |
String 1 |
String 2 Length |
String 2 |
4 bytes |
2 bytes |
4 bytes (temporarily useless) |
2 bytes |
X bytes |
2 bytes |
X bytes |
PHP is implemented in a way that is also easy to package into binary via pack for communication. Put the code below.
The main application of local test is: Send account and password to server side
<?php class byte{//length private $length = 0;
Private $byte = ';
Operation Code Private $code;
Public Function Setbyteprev ($content) {$this->byte= $content. $this->byte;
} public Function GetByte () {return $this->byte;
} public Function GetLength () {return $this->length;
Public Function Writechar ($string) {$this->length+=strlen ($string);
$str =array_map (' Ord ', Str_split ($string));
foreach ($str as $vo) {$this->byte.=pack (' C ', $VO);
} $this->byte.=pack (' C ', ' 0 ');
$this->length++;
} Public Function Writeint ($str) {$this->length+=4;
$this->byte.=pack (' L ', $str);
} Public Function Writeshortint ($interge) {$this->length+=2;
$this->byte.=pack (' V ', $interge);
}} class gamesocket{private $socket;
Private $port = 9991;
Private $host = ' 192.168.211.231 ';
Private $byte;
Private $code;
Const code_length=2;
Const FLAG_LENGTH=4;
Public Function __set ($name, $value) {$this, $name = $value; The Public function __construct ($host =' 192.168.211.231 ', $port =9991) {$this->host= $host;
$this->port= $port;
$this->socket = socket_create (Af_inet, Sock_stream, sol_tcp);
if (! $this->socket) {exit (' Create socket failed ');
} $result = Socket_connect ($this->socket, $this->host, $this->port);
if (! $result) {exit (' Connect not on target host '. $this->host);
} $this->byte=new byte (); Public function Write ($data) {if (is_string ($data) | | | Is_int ($data) | |
Is_float ($data)) {$data []= $data;
} if (Is_array ($data)) {foreach ($data as $vo) {$this->byte->writeshortint (strlen ($VO));
$this->byte->writechar ($VO);
}} $this->setprev ();
$this->send (); }/* Set header section * table Header =length+code+flag *length is total length (4 bytes) code operation flag (2 bytes) flag temporarily useless (4 bytes) */Private Function GetHeader ()
{$length = $this->byte->getlength ();
$length =intval ($length) +self::code_length+self::flag_length;
Return pack (' L ', $length);
} Private Function GetCode () {return pack (' V ', $this->code); } Private FUnction Getflag () {return Pack (' L ', 24); } Private Function Setprev () {$this->byte->setbyteprev ($this->getheader (). $this->getcode (). $this
Getflag ());
} Private Function Send () {$result =socket_write ($this->socket, $this->byte->getbyte ());
if (! $result) {exit (' Send message failed ');
}} Public Function __desctruct () {socket_close ($this->socket);
}} $data []= ' Testzouhao ';
$data []= ' a ';
$gameSocket =new Gamesocket ();
$gameSocket->code=11; $gameSocket->write ($data);
Get the package content by grasping the packet analysis
Baotou and so on do not have to look, mainly look at the blue part.
According to protocol analysis, the first 4 bytes are the table header, which represents the length
So:
17 00 00 00 represents the length of the header, 17 is 16, is converted to decimal 23, and the remainder is all added to 23 bytes.
0b 00 represents the operation code 11, which is the login operation
18 00 00 00 It means flag.
0a 00 represents the length of the string 1, which is converted to decimal 10
The 7a 6f of the three minutes 6f were converted to decimal after the ASCII code corresponding to the character, the result is: Testzouhao,
Since the mechanism of the C + + string is the end is, after the string, 00 bytes is the
Then the second string has a length of 01 00, which is 1.
61 Similarly, decimal to ASCII code, a, followed by 00 for C + + mechanism
Perfect resolution, send packet flawless, after the C + + server also returned the corresponding package, I in accordance with the same to unpack it!