Pass php through c data

How to create a php socket client that must execute socket_sendto for a Linux socket server that has the following structure

typedef struct
{
  UI2             todo;   
  char            rz[LNG_RZ + 1]; 
  char            saId[LNG_SAT_ID + 1]; 
  char            user[LNG_USER + 1]; 
  char            lang[LANGLEN + 1]; 
  SI4             result;       
  UI4             socket;  
  char            text[LNG_ALLG + 1]; 
  char            filename[MAX_PATHLEN];
}  dmsAuf_Head;        

Obviously, PHP does not support the structure of how to pass c data to a socket using php

I know there is a trick to making a struct c object using the method below:

class dmsAuf_Head {
  public            todo;            
  public            rz    
  public            saId    
  public            user    
  public            lang    
  public            result;                         
  public            socket;        
  public            text   
  public            filename 
}

$obj = new dmsAuf_Head();

but that doesn't take care of the attribute size? Any recommendation?


How to serialize everything before sending?

As suggested by the socket server c developer, they use the following c code to send the value to socket c:

 rval = send(dms_aufHead->socket, (char *) dms_aufHead, sizeof(dmsAuf_Head), 0);

So how can I send dms_aufHead data using php?


As suggested by the socket server c developer, they use the following c code to send the value to socket c:

rval = send(dms_aufHead->socket, (char *) dms_aufHead, sizeof(dmsAuf_Head), 0);

So how can I send dms_aufHead data using php?

What if (for example) $ rz is only 3 char (for this case) instead of 13 char as the desired length? what should i output for php?

+3
3

fwrite . pack .

http://us2.php.net/pack

fwrite($sock, pack('v', 0x1234)); // send a 16 bit integer

, , C ( ..), , , ..

- , , , / ( ), fread/fwrite . PHP :

class dmsAuf_Head {
  public            $todo;            
  public            $rz;    
  public            $saId;    
  public            $user;    
  public            $lang;    
  public            $result;                         
  public            $socket;        
  public            $text;   
  public            $filename; 

  public function send($sock)
  {
    fwrite($sock, pack('v', $this->todo));
    // .. fwrite the rest
  }
}

$foo = new dmsAuf_Head();
$foo->send($sock);

( , , .)

+2

, , JSON (, , pack), . .

0

/ , "c struct", C, , C, , / struct. C, , , . PHP, "serialize" PHP.

PHP-, , , , / . , , - , , , PHP .

OTOH, , C/++ libs, PHP (. ) , .

0

Source: https://habr.com/ru/post/1770850/


All Articles