tags:

views:

65

answers:

5

Dear All, How to make a php socket client that needs to perform socket_sendto to linux C socket server which has following struct

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 is known not to support struct how can I pass the c data to socket using php

I know there is a trick to make c struct 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 this would not take care of the size of the attribute? Any recommnedation?

A: 

IT would be best if you serialized your data into a textual format like JSON (or maybe using the pack function) before you send it over the line. Upon receipt, you can deserialize it into the appropriate data structure.

Noufal Ibrahim
+2  A: 

You'll need to fwrite the data in the proper order. The pack function will be useful.

http://us2.php.net/pack

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

Of course you have to be careful regarding how the C structure is built (padding, etc), the endianness the target machine is expecting, and so on.

Because of all that, it is usually better for both systems to simply read/write the data needed sequentially and fill in the structures manually (field by field), as opposed to trying to fread/fwrite the structure itself as one big block. On the PHP side, it may look like:

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

(Note that you can build a single packed string and send it all in one go if you use multiple arguments.)

konforce
A: 

How to serialzied everything before sending???

As proposed by the developer of the c socket server, they are using the following c code to send value to the c socket server:

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

So how can I send dms_aufHead data using php instead?

tweakmy
A: 

There is no such data protocol / format as a "c struct" it's up to the C compiler how it stores this data - and up to the programmer of the C program to decide how to populate/retrieve values from the struct. Even if you were writing a client in C using the same struct you'd need to convert it to a different representation to transfer it to the server. The same is true of PHP - although 'serialize' provides a built-in mechanism for creating a portable representation of a PHP data structure.

You need to write PHP code to create a respresentation of the data which the server can process - which has very little to do with how the server stores/manipulates the data internally. For the server to exist, then it must have capability to parse data in some format - go find out what that format is and write your PHP accordingly.

OTOH if the server does not yet exist, then there are C/C++ libs available which will parse a PHP serialized data structure (see this SO topic) or if there is a requirement to support a more formal protocol, then have a look at soap.

symcbean
A: 

As proposed by the developer of the c socket server, they are using the following c code to send value to the c socket server:

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

So how can I send dms_aufHead data using php instead?

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

tweakmy
This is a terrible way to send data. It is dependent on both the compiler and architecture. If you must send data that way, you can still use the code in my example. You'll just have to use the pack format that matches exactly how their compiler is building the structure. You'll need to get the precise structure format from them, including padding between structure elements, etc.
konforce