TCP Telnet 传输中丢失字符
我正在使用 Winsock 通过 Telnet 发送命令;但由于某种原因,当我尝试发送字符串时,偶尔会丢失一些字符。我使用 send
:
int SendData(const string & text)
{
send(hSocket,text.c_str(),static_cast<int>(text.size()),0);
Sleep(100);
send(hSocket,"\r",1,0);
Sleep(100);
return 0;
}
有什么建议吗?
更新:
我检查过,即使发送所有字符,错误仍然发生。因此,我决定更改 Send
函数,以便它发送单个字符并检查它们是否已发送:
void SafeSend(const string &text)
{
char char_text[1];
for(size_t i = 0; i <text.size(); ++i)
{
char_text[0] = text[i];
while(send(hSocket,char_text,1,0) != 1);
}
}
此外,它以一种特殊的方式删除字符;即在句子的中间。例如
set variable [fp]exit_flag = true
发送为
ariable [fp]exit_flag = true
或
set variable [fp]app_flag = true
发送为
setrable [fp]app_flag = true
I'm using Winsock to send commands through Telnet ; but for some reason when I try to send a string, a few characters get dropped occasionally. I use send
:
int SendData(const string & text)
{
send(hSocket,text.c_str(),static_cast<int>(text.size()),0);
Sleep(100);
send(hSocket,"\r",1,0);
Sleep(100);
return 0;
}
Any suggestions?
Update:
I checked and the error still occurs even if all the characters are sent. So I decided to change the Send
function so that it sends individual characters and checks if they have been sent:
void SafeSend(const string &text)
{
char char_text[1];
for(size_t i = 0; i <text.size(); ++i)
{
char_text[0] = text[i];
while(send(hSocket,char_text,1,0) != 1);
}
}
Also, it drops characters in a peculiar way ; i.e. in the middle of the sentence. E.g.
set variable [fp]exit_flag = true
is sent as
ariable [fp]exit_flag = true
Or
set variable [fp]app_flag = true
is sent as
setrable [fp]app_flag = true
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如评论中提到的,您绝对需要检查
send
的返回值,因为它可以在仅发送缓冲区的一部分后返回。您几乎总是希望在类似于以下内容的循环中调用
send
(未测试,因为我目前没有可用的 Windows 开发环境):更新:
这与 OP 无关,但对
recv
的调用也应采用与上述相同的方式构造。要进一步调试问题,Wireshark(或同等软件)非常适合追踪问题的根源。
过滤您想要查看的数据包(它有很多选项)并检查它们是否包含您认为的内容他们包括。
另请注意,telnet 是一个具有大量 RFC 的协议。大多数时候,您可以只发送原始文本,但并不能真正保证它有效。
您提到 Windows telnet 客户端向您发送不同的字节,从两个客户端捕获最小序列并比较它们。使用 RFC 找出其他客户端的不同之处及其原因。您可以使用“查看 -> 数据包字节”来显示数据包的数据,并且可以轻松检查和复制/粘贴十六进制转储。
As mentioned in the comments you absolutely need to check the return value of
send
as it can return after sending only a part of your buffer.You nearly always want to call
send
in a loop similar to the following (not tested as I don't have a Windows development environment available at the moment):Update:
This is not relevant for the OP, but calls to
recv
should also structured in the same way as above.To debug the problem further, Wireshark (or equivalent software) is excellent in tracking down the source of the problem.
Filter the packets you want to look at (it has lots of options) and check if they include what you think they include.
Also note that telnet is a protocol with numerous RFCs. Most of the time you can get away with just sending raw text, but it's not really guaranteed to work.
You mention that the windows telnet client sends different bytes from you, capture a minimal sequence from both clients and compare them. Use the RFCs to figure out what the other client does different and why. You can use "View -> Packet Bytes" to bring up the data of the packet and can easily inspect and copy/paste the hex dump.