void __fastcall TForm1::Button1Click(TObject *Sender)
{
    int size =  MemoEnter->GetTextLen() + 1;
    wchar_t *szBuff = new wchar_t[size];
    memset(szBuff, 0, sizeof(szBuff));
    MemoEnter->GetTextBuf((wchar_t *)szBuff, size);
    TcpClient->SendBuf(szBuff, sizeof(szBuff));
    LogOut->Lines->Add(szBuff);
    delete []szBuff;
}


为什么TcpClient不发送任何内容?
服务器正常。连接正常。
Telnet将数据发送到服务器,但此代码不发送。

伙计们我尝试过了

TcpClient->SendBuf("fsd", 3);


还是一无所有

最佳答案

这可能是导致问题的原因:

sizeof(szBuff); // Returns the sizeof a wchar_t*,
                // not the number of characters in szBuff


更改:

memset(szBuff, 0, sizeof(szBuff));
...
TcpClient->SendBuf(szBuff, sizeof(szBuff));


至:

memset(szBuff, 0, sizeof(wchar_t) * size);
...
TcpClient->SendBuf(szBuff, wcslen(szBuff));


如果TcpClient->SendBuf()的第二个参数是字节数,而不是字符数,则更改为:

TcpClient->SendBuf(szBuff, wcslen(szBuff) * sizeof(wchar_t));

关于c++ - C++ Builder TtcpClient,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9080066/

10-09 05:59