C ++ Winsock recv()缓冲区垃圾 [英] C++ Winsock recv() buffer junk

查看:216
本文介绍了C ++ Winsock recv()缓冲区垃圾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在cpp中编写一个控制台应用程序,通过TCP将控制命令从文件发送到主机,并接收响应。
所有这些信息都显示在屏幕上,并记录到一个文件,这是实际的问题。即使我尝试设置固定长度,输出字符串也似乎存储垃圾。

I'm writing a console appication in cpp that sends control commands from a file via TCP to a host machine and receives a response. All those informations are shown on screen and logged to a file and this is the actual problem. The output string seems to store junk for any reason, even if I try to set a fixed length.

EDIT:清除了代码,照顾recv()的返回值。我唯一还没有的是,我的日志文件中的 2nd recv行填充垃圾。也许你们中的一个能够发现问题。

cleaned up the code and took care of the return value of recv(). The only thing I don't get yet is that the 2nd recv line in my logfile is filled with junk. Maybe one of you guys is able to spot the problem.

        string cmd="";
        char *sendstr=(char*)cmd.c_str();
        fflush(stdin);
        int n = 1, total = 0;
        char temp[1024];
        string inStr;
        if(cmdin.is_open())
        {           
            while(!cmdin.eof())
            {
                total=0;
                cmd=fread();
                send(serverPC, sendstr, (int)strlen(sendstr),0);
                n=recv(serverPC,&temp[total],sizeof(temp)-total-1,0); // FIX THIS
                total+=n;
                temp[total]='\0';
                inStr=temp;
                fwrite(inStr,cmd);
            }
            cout << "Data successfully sent!\n";
        }
        else{
            cerr<<"can't find 'cmd.cfg' file"<<endl;
        }

我期望的输出

<11:40:00> INIT
received: VELO=0.00km/h  DOT=FORW  
----

这是我得到的:

<10:05:56> INIT
received: VELO=0.00km/h  DOT=FORW  
----
<10:05:56> VELO=50.00
received: VELO=0.00km/h  DOT=FORW  VELO=0.00km/h  DOT=FORW  VELO=0.00km/h
DOT=FORW  VELO=0.00km/h  DOT=FORW  VELO=0.00km/h  DOT=FORW  VELO=0.00km/h  DOT=FORW  
VELO=0.00km/h  DOT=FORW  
----
<10:05:56> VELO=100.00
received: VELO=50.00km/h  DOT=FORW  
----
<10:05:56> DOT=BACK
received: VELO=50.00km/h  DOT=FORW  


推荐答案

编写TCP接收器时要记住的一个关键点是TCP被认为是数据流。这意味着当您从发送N个字节数据的数据包执行接收时,您可以接收:

A key point to remember when writing a TCP receiver is that TCP is considered as a data stream. This means that when you perform a receive from something sending packets of N bytes of data, you can receive:


  • N个字节的数据

  • 少于N个字节的数据

  • 超过N个字节的数据

假设没有断开连接,缓冲区溢出或其他错误,您将始终按照发送顺序接收服务器发送的内容,但消息可能在接收端被截断或连接。这是很多需要定制TCP协议的原因之一,包括一个长度字段(所以你知道你需要接收多少数据),或者某种方式来表示结束数据。这样你的接收器可以循环,直到它收到了正确的数量,剩下的TCP缓冲区下一次接收。

Assuming there is no disconnection, buffer overflow, or other error, you will always receive what the server sends, in the order it sends it, but messages may be truncated or concatenated at the receiving end. It's one of the reasons that a lot [citation needed?] of custom TCP protocols include either a length field (so you know how much data you need to receive), or some way of denoting the end of the data. That way your receiver can loop until it has received just the right amount, leaving the rest in the TCP buffer for the next receive.

当你说你看到垃圾,它对我看起来不像垃圾,它看起来像几个数据包连接在一起成一个。

When you say you are seeing "junk", it doesn't look like junk to me, it looks like several packets of data concatenated together into one.

这篇关于C ++ Winsock recv()缓冲区垃圾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆