如果这个标题没有让您感到困惑,我会看看我能在这里做什么。我有一个 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 is the text being sent as a UNICODE (wide) string.
我无法准确确定 C++ DLL 发送到套接字的内容,因此我基本上猜测需要在 PHP 中发送的内容。
我可以使用帮助来确定 C++ 代码到底发送了什么,以及如何在 PHP 中执行相同的操作。
谢谢。
最佳答案
您无法打包整个对象,但可以打包并连接成员。所以,类似
$packed = pack('I', $pMsgBlock->nSize)
.pack('I', $pMsgBlock->nSanity1)
.pack('I', $pMsgBlock->nSanity2)
.pack('I', $pMsgBlock->nChecksum)
.mb_convert_encoding($pMsgBlock->szText, 'UTF-16');
关于php - 基于 C++ 代码在 PHP 中创建字节数据并将其传递到套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13588168/