2

如果这个标题没有让你感到困惑,我会看看我能在这里做什么。我有一个将 TCP 流量传递到服务器的 C++ DLL 的源代码。我相信所有相关的 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 中做同样的事情。

谢谢。

4

1 回答 1

2

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

$packed = pack('I', $pMsgBlock->nSize)
.pack('I', $pMsgBlock->nSanity1)
.pack('I', $pMsgBlock->nSanity2)
.pack('I', $pMsgBlock->nChecksum)
.mb_convert_encoding($pMsgBlock->szText, 'UTF-16');
于 2012-11-27T18:24:45.843 回答