基于C ++代码在PHP中创建字节数据并将其传递给套接字

时间:2012-11-27 16:03:30

标签: php c++ sockets unicode pack

如果这个标题没有让你感到困惑,我会看到我能在这里做些什么。我有一个C ++ DLL的源代码,它将TCP流量传递给服务器。我相信所有相关的C ++代码如下:

#define HRD_MSG_SANITY1     0x1234ABCD
#define HRD_MSG_SANITY2     0xABCD1234

typedef struct{
   unsigned int    nSize;
   unsigned int    nSanity1;
   unsigned int    nSanity2;
   unsigned int    nChecksum;
   WCHAR   szText[1];
} HRD_MSG_BLOCK;

CString strMessage = "this is a test\n";
 // I added this - the rest of the code
 // to determine this is unnecessary for
 // the context of this question.

//
//  Allocate.
//
int nMsgBytes  = sizeof(HRD_MSG_BLOCK) + sizeof(TCHAR) * (strMessage.GetLength() + 1);
HRD_MSG_BLOCK*  pMsgBlock  = (HRD_MSG_BLOCK*) new BYTE[nMsgBytes];
BYTE*           pMsgBuffer = (BYTE*)pMsgBlock;

ZeroMemory(pMsgBlock, nMsgBytes);

pMsgBlock->nSize     = nMsgBytes;
pMsgBlock->nSanity1  = HRD_MSG_SANITY1;
pMsgBlock->nSanity2  = HRD_MSG_SANITY2;
pMsgBlock->nChecksum = 0;

_tcscpy(pMsgBlock->szText, strMessage);

for (int nSendLoop = 0; (nSendLoop < MAX_SENDS) && (nToSend > 0); nSendLoop++) {
    //
    //  Send data to this port, max of 32k (0x8000).
    //
    int nSendSize  = min(nToSend, 0x8000);
    int nSentCount = (int)::send(g_socket, (char*)&pMsgBuffer[nSent], nSendSize, 0);

    //
    //  Error.
    //
    if (nSentCount == SOCKET_ERROR) {
        CSocketError error(::WSAGetLastError());

        //
        //  Report the error.
        //
        g_strLastError.Format(_T("Error sending %u bytes to socket %u - %s"),
                                      nSendSize,
                                      g_socket,
                                      error.Text());
    } else {
        nSent   += nSentCount;
        nToSend -= nSentCount;
    }
}

我想要做的是通过PHP完成同样的事情。这是PHP代码:

class HRD_MSG_BLOCK {
    public $nsize;
    public $nSanity1;
    public $nSanity2;
    public $nChecksum;
    public $szText;
}

$string = "this is a test\n";
$pMsgBlock = new HRD_MSG_BLOCK();
$nMsgBytes = 20 + (2*(strlen($string)+1));
 // I determined this by comparing to
 // actual size of TCHAR = 2 and actual
 // size of pMsgBlock = 20 in Visual Studio

$pMsgBlock->nSize = $nMsgBytes;
$pMsgBlock->nSanity1 = 0x1234ABCD;
$pMsgBlock->nSanity2 = 0xABCD1234;
$pMsgBlock->nChecksum = 0;
$pMsgBlock->szText = utf8_encode($string);
$binarydata = pack("C*", $pMsgBlock);

然后我创建套接字,并使用socket_write发送$binarydata

显然,我不能使用pack(),因为我无法将类对象($ pMsgBlock)转换为int(这是pack()所需的第二个参数)。我使用了utf8_encode(),因为DLL文档说:

  

szText是作为UNICODE(宽)字符串发送的文本。

我无法准确确定C ++ DLL发送到套接字的内容,所以我基本上猜测我需要在PHP中发送什么。

我可以帮助确定C ++代码的确切发送方式,以及如何在PHP中执行相同操作。

感谢。

1 个答案:

答案 0 :(得分:2)

你不能打包()整个对象,但你可以打包()并连接成员。所以,像

$packed = pack('I', $pMsgBlock->nSize)
.pack('I', $pMsgBlock->nSanity1)
.pack('I', $pMsgBlock->nSanity2)
.pack('I', $pMsgBlock->nChecksum)
.mb_convert_encoding($pMsgBlock->szText, 'UTF-16');