我必须向UDP套接字服务器发送一些命令并获得重放,下面的代码工作正常,这是c / c ++,我从服务器使用它获得成功的响应。
typedef struct{
unsigned char type; //type
char unsignedfunctionID;
unsigned short reserved; //Reserved
int unsignediDevSn; // serial number 4-byte
unsigned char data [32]; // 32 bytes of data
unsigned int sequenceId; // serial number
unsigned char extern_data [20];
}PacketShortAccess;
发送到套接字
PacketShortAccess statusCommand;
statusCommand={0};
statusCommand.type=0x19;
statusCommand.unsignedfunctionID=0x20;
statusCommand.reserved=0; //Reserved
statusCommand.unsignediDevSn=serialNumebr; serial number 4-byte
char sendData[sizeof(statusCommand)];
memcpy(sendData, &statusCommand, sizeof(statusCommand));
sendto(sockfd,sendData,sizeof(sendData),0,(struct sockaddr *)&servaddr,sizeof(servaddr));
现在我需要在php中使用相同的命令格式,
我试过像
//this the command format I am getting while printing on c++ code
$command="19 ffb0 0 0 ff88 d 38 19 ffd0 37 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ";
socket_sendto($sock, $command, strlen($command) , 0 , $server , $port)
但是我没有从服务器得到任何响应,我可以在php上重写上面的c ++代码。
答案 0 :(得分:2)
对于结构而言,PHP并不像C ++那样优雅,只是它的类型松散。随着PHP 7& E的引入,情况有所改善。 7.1,但仍然没有C ++那么严格。下面大致相当于C ++结构。
您发送的数据似乎没有意义,因此您需要首先了解c ++中的类型是什么意思,我不会对此进行扩展,但如果您需要,可以看到此异地资源{{ 3}}
STRUCT
class PacketShortAccess {
/** @var int */
private $type;
/** @var int */
private $unsignedFunctionID;
/** @var int */
private $reserved = 0;
/** @var int */
private $unsignediDevSn;
/** @var string */
private $data;
/** @var int */
private $sequenceId;
/** @var string */
private $externData;
/**
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* @param int $type
*
* @return PacketShortAccess
* @throws Exception
*/
public function setType($type)
{
if (
false === is_int($type) ||
$type < 0 ||
$type > 255
) {
throw new \Exception('setType expected an unsigned char in the range of 0-255');
}
$this->type = $type;
return $this;
}
/**
* @return string
*/
public function getUnsignedFunctionID()
{
return $this->unsignedFunctionID;
}
/**
* @param int $unsignedFunctionID
*
* @return PacketShortAccess
* @throws Exception
*/
public function setUnsignedFunctionID($unsignedFunctionID)
{
if (
false === is_int($unsignedFunctionID) ||
$unsignedFunctionID < 0 ||
$unsignedFunctionID > 255
) {
throw new \Exception('setUnsignedFunctionID expected an unsigned char in the range of 0-255');
}
$this->unsignedFunctionID = $unsignedFunctionID;
return $this;
}
/**
* @return int
*/
public function getReserved()
{
return $this->reserved;
}
/**
* @param int $reserved
*
* @return PacketShortAccess
* @throws Exception
*/
public function setReserved($reserved)
{
if (
false === is_int($reserved) ||
$reserved < 0 ||
$reserved > 65535
) {
throw new \Exception('setReserved expected an unsigned short in the range of 0-65535');
}
$this->reserved = $reserved;
return $this;
}
/**
* @return int
*/
public function getUnsignediDevSn()
{
return $this->unsignediDevSn;
}
/**
* @param int $unsignediDevSn
*
* @return PacketShortAccess
* @throws Exception
*/
public function setUnsignediDevSn($unsignediDevSn)
{
if (
false === is_int($unsignediDevSn) ||
$unsignediDevSn < -2147483648 ||
$unsignediDevSn > 2147483647
) {
throw new \Exception('setUnsignediDevSn expected a signed int in the range of -2147483648-2147483647');
}
$this->unsignediDevSn = $unsignediDevSn;
return $this;
}
/**
* @return string
*/
public function getData()
{
return $this->data;
}
/**
* @param string $data
*
* @return PacketShortAccess
* @throws Exception
*/
public function setData($data)
{
if (
false === is_string($data) ||
strlen($data) > 32
) {
throw new \Exception('setData expected a string in the range of 0-32 characters');
}
$this->data = $data;
return $this;
}
/**
* @return int
*/
public function getSequenceId()
{
return $this->sequenceId;
}
/**
* @param int $sequenceId
*
* @return PacketShortAccess
* @throws Exception
*/
public function setSequenceId($sequenceId)
{
if (
false === is_int($sequenceId) ||
$sequenceId < 0 ||
$sequenceId > 4294967295
) {
throw new \Exception('setSequenceId expected a signed int in the range of 0-4294967295');
}
$this->sequenceId = $sequenceId;
return $this;
}
/**
* @return string
*/
public function getExternData()
{
return $this->externData;
}
/**
* @param string $externData
*
* @return PacketShortAccess
* @throws Exception
*/
public function setExternData($externData)
{
if (
false === is_string($externData) ||
strlen($externData) > 20
) {
throw new \Exception('setExternData expected a string in the range of 0-20 characters');
}
$this->externData = $externData;
return $this;
}
/**
* @return string
*/
public function __toString()
{
return sprintf(
'%d %s %d %d % 32s %d % 20s',
$this->getType(),
$this->getUnsignedFunctionID(),
$this->getReserved(),
$this->getUnsignediDevSn(),
$this->getData(),
$this->getSequenceId(),
$this->getExternData()
);
}
}
使用struct
$packetShortAccess = new PacketShortAccess;
$packetShortAccess
->setType(0x19)
->setUnsignedFunctionID(0x20)
->setReserved(0)
->setUnsignediDevSn(128495)
->setData('')
->setSequenceId(0)
->setExternData('')
;
echo "'" . (string)$packetShortAccess . "'"; // quotes added so you can see whitespace.
将输出此
'25 32 0 128495 0 '