PHP传递C结构数据throught socket编程 [英] php passing c struct data throught socket programming

查看:125
本文介绍了PHP传递C结构数据throught socket编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让一个PHP套接字客户端需要执行socket_sendto到Linux下C插槽服务器,具有以下结构

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;        

显然,PHP被称为不支持结构如何传递C数据使用PHP插座

Obviously, PHP is known not to support struct how can I pass the c data to socket using php

我知道有一招用,使C结构对象下方的方法:

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

,但这不会采取属性的尺寸的关心?任何recommnedation?

but this would not take care of the size of the attribute? Any recommnedation?

如何在发送前serialzied一切???

How to serialzied everything before sending???

建议,由交流电插座服务器的开发人员,他们正在使用下面的C code发送值到c socket服务器:

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

所以,我怎么能发送使用PHP,而不是dms_aufHead数据?

So how can I send dms_aufHead data using php instead?

建议,由交流电插座服务器的开发人员,他们正在使用下面的C code发送值到c socket服务器:

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

所以,我怎么能发送使用PHP,而不是dms_aufHead数据?

So how can I send dms_aufHead data using php instead?

如果(例如)$ Rz为什么只有3炭(对于这种情况下),而不是13 char用作期望的长度?我应该怎么输出PHP?

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?

推荐答案

您需要 FWRITE 中的数据以正确的顺序。在功能将非常有用。

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

当然,你有关于如何的C结构是建立(填充等),目标机器期待字节顺序,依此类推要小心。

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.

由于所有的,它通常是两个系统更好地简单地读/写所需的数据顺序,并在手动结构(由字段字段)填写,而不是试图的fread / fwrite的结构本身作为一个大块。在PHP端,它可能看起来像:

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

这篇关于PHP传递C结构数据throught socket编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆