简体   繁体   English

C ++ Builder TtcpClient

[英]C++ Builder 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;
}  

Why doesn't TcpClient send anything? 为什么TcpClient没有发送任何东西? Server is ok. 服务器还可以。 connection is ok. 连接还可以。 Telnet sends data to the server but this code does not. Telnet将数据发送到服务器,但此代码不会。

Guys! 伙计们! i tried to 我试过了

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

and still got nothing 仍然一无所获

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()的第二个参数是字节数,而不是字符, TcpClient->SendBuf()改为:

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

Your use of sizeof() is definately the problem. 你对sizeof()使用肯定是问题所在。 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. 指针的大小是32位的4和64位的8。 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: 您应该使用VCL的String类,而不是使用new[]运算符,例如:

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 . 请注意, StringUnicodeString的别名。 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: 如果接收器不期望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); 
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM