C++生成器 TtcpClient

发布于 2025-01-01 04:47:27 字数 541 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心的憧憬 2025-01-08 04:47:27

这可能会导致问题:

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));

This may be contributing to the problem:

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

Change:

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

To:

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

If the second argument of TcpClient->SendBuf() is the number of bytes, not characters, then change to:

TcpClient->SendBuf(szBuff, wcslen(szBuff) * sizeof(wchar_t));
平定天下 2025-01-08 04:47:27

您对 sizeof() 的使用肯定是问题所在。您发送的数据指定指向缓冲区的指针的大小,而不是缓冲区本身的大小。指针的大小在 32 位中为 4,在 64 位中为 8。您需要使用实际的缓冲区大小而不是指针大小。

您应该使用 VCL 的 String 类,而不是使用 new[] 运算符,例如:

void __fastcall TForm1::Button1Click(TObject *Sender) 
{ 
    String s = MemoEnter->Text; 
    TcpClient->SendBuf(s.c_str(), ByteLength(s)); 
    LogOut->Lines->Add(s); 
}

请注意,String的别名>UnicodeString。如果接收方不需要 UTF-16 编码的数据,那么您需要在发送之前将数据转换为另一种编码,例如:

void __fastcall TForm1::Button1Click(TObject *Sender) 
{ 
    String s = MemoEnter->Text; 
    UTF8String utf8 = s;
    TcpClient->SendBuf(utf8.c_str(), utf8.Length()); 
    LogOut->Lines->Add(s); 
}

或:

void __fastcall TForm1::Button1Click(TObject *Sender) 
{ 
    String s = MemoEnter->Text; 
    AnsiString ansi = s; // <-- potential data loss for non-ASCII characters!
    TcpClient->SendBuf(ansi.c_str(), ansi.Length()); 
    LogOut->Lines->Add(s); 
}

或:

void __fastcall TForm1::Button1Click(TObject *Sender) 
{ 
    String s = MemoEnter->Text; 
    AnsiStringT<SomeCodePage> ansi = s; // <-- use a suitable codepage to avoid data loss!
    TcpClient->SendBuf(ansi.c_str(), ansi.Length()); 
    LogOut->Lines->Add(s); 
}

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's String class instead, eg:

void __fastcall TForm1::Button1Click(TObject *Sender) 
{ 
    String s = MemoEnter->Text; 
    TcpClient->SendBuf(s.c_str(), ByteLength(s)); 
    LogOut->Lines->Add(s); 
}

Note that String is an alias for UnicodeString. If the receiver is not expecting UTF-16 encoded data, then you need to convert the data to another encoding before you send it, eg:

void __fastcall TForm1::Button1Click(TObject *Sender) 
{ 
    String s = MemoEnter->Text; 
    UTF8String utf8 = s;
    TcpClient->SendBuf(utf8.c_str(), utf8.Length()); 
    LogOut->Lines->Add(s); 
}

Or:

void __fastcall TForm1::Button1Click(TObject *Sender) 
{ 
    String s = MemoEnter->Text; 
    AnsiString ansi = s; // <-- potential data loss for non-ASCII characters!
    TcpClient->SendBuf(ansi.c_str(), ansi.Length()); 
    LogOut->Lines->Add(s); 
}

Or:

void __fastcall TForm1::Button1Click(TObject *Sender) 
{ 
    String s = MemoEnter->Text; 
    AnsiStringT<SomeCodePage> ansi = s; // <-- use a suitable codepage to avoid data loss!
    TcpClient->SendBuf(ansi.c_str(), ansi.Length()); 
    LogOut->Lines->Add(s); 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文