C++生成器 TtcpClient
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);
,但还是一无所获
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;
}
Why doesn't TcpClient send anything?
Server is ok. connection is ok.
Telnet sends data to the server but this code does not.
Guys! i tried to
TcpClient->SendBuf("fsd", 3);
and still got nothing
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能会导致问题:
更改
为:
如果
TcpClient->SendBuf()
的第二个参数是字节数,而不是字符数,则更改为:This may be contributing to the problem:
Change:
To:
If the second argument of
TcpClient->SendBuf()
is the number of bytes, not characters, then change to:您对
sizeof()
的使用肯定是问题所在。您发送的数据指定指向缓冲区的指针的大小,而不是缓冲区本身的大小。指针的大小在 32 位中为 4,在 64 位中为 8。您需要使用实际的缓冲区大小而不是指针大小。您应该使用 VCL 的
String
类,而不是使用new[]
运算符,例如:请注意,
String
是的别名>UnicodeString
。如果接收方不需要UTF-16
编码的数据,那么您需要在发送之前将数据转换为另一种编码,例如:或:
或:
Your use of
sizeof()
is definately the problem. You are sending your data specifying the size of the pointer that points at the buffer, not the size of the buffer itself. The size of a pointer is 4 in 32-bit and 8 in 64-bit. You need to use the actual buffer size instead of the pointer size.Rather than using the
new[]
operator, you should use the VCL'sString
class instead, eg:Note that
String
is an alias forUnicodeString
. If the receiver is not expectingUTF-16
encoded data, then you need to convert the data to another encoding before you send it, eg:Or:
Or: