2017-04-04 67 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      ' 
相關問題