2017-04-04 68 views
0

我必须发送一些命令到UDP套接字服务器并获得重播,下面的代码工作正常,这是在C/C + +,我得到成功的响应从服务器使用它。PHP发送十六进制数据到套接字

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

“*命令格式,而对C印花++代码*我得到” 你怎么打印此? – alk

+0

我正在打印这样的数据:'for(int i = 0; i <64; i ++) \t \t cout << hex << static_cast (sendData [i])<<“”; – CodeDezk

回答

2

对于结构来说,PHP并不像C++那么优雅,只是简单地将它打成松散的类型。随着PHP 7 & 7.1的推出,情况有所改善,但仍不像C++那样严格。以下大致相当于C++结构。

您发送的数据似乎没有意义,所以您需要先了解C++中输入的内容是什么意思,我不会详述,但如果您想要,您可以看到此异地资源C++ Data Types

结构

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() 
     ); 
    } 

} 

使用结构

$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      ' 
相关问题