为什么我在接收的包装结束时会获得Gibberish文本?
我正在为大学做一个激光雷达项目,但我遇到了一点困难。我正在发送一个包裹,其中包括电机的确切位置以及通过 WiFi 从距离传感器读取的距离,包裹应该看起来像这样的“位置/距离”。我在 Visual Studio 中制作了一个客户端程序,但是当我收到包时,消息末尾有 iiiiii ,并且包未按正确的顺序通过。我将把代码放在这里,如果您知道我缺少什么,我将非常感激。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include "winsock2.h"
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#pragma warning(disable:4996)
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
FILE* file;
file = fopen("package.txt", "w");
if (file == NULL) {
printf("Error openning the file\n");
exit(1);
}
int iResult;
WSADATA wsaData;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR)
printf("Error WSAStartup\n");
SOCKET ClientSocket;
ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ClientSocket == INVALID_SOCKET)
{
printf("Error initializing socket with the next error code: %ld\n",
WSAGetLastError());
WSACleanup();
return 0;
}
int Port = 13000;
char IP[14] = "192.168.0.105";
sockaddr_in ServerAddr;
int AddrLen = sizeof(ServerAddr);
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_addr.s_addr = inet_addr(IP);
ServerAddr.sin_port = htons(Port);
if (connect(ClientSocket, (SOCKADDR*)&ServerAddr, AddrLen) == SOCKET_ERROR)
{
printf("Error connecting with the next error code: %ld\n",
WSAGetLastError());
WSACleanup();
return 0;
}
else {
printf("Sikeres kapcsolodas\n");
}
char message[15];
int receiving = 0;
while (receiving != SOCKET_ERROR) {
receiving = recv(ClientSocket, message, sizeof(message), 0);
fprintf(file, "%s\n", message);
printf("%i, %i\n", receiving, sizeof(message));
}
if (receiving == SOCKET_ERROR) {
printf("Error at receiving the message\n");
closesocket(ClientSocket);
WSACleanup();
return 0;
}
fclose(file);
return 0;
}
我收到的消息如下所示:
0.00/176.00ÌÌÌÌÌÌÌÌÌÌÌÌ 0.75/180.003.75ÌÌÌÌÌÌÌÌ /179.008.63/179ÌÌÌÌÌÌÌÌ .009.008.63/179ÌÌÌÌÌÌÌÌ 14.06/179.0019.ÌÌÌÌÌÌÌÌ 13/178.00.0019.ÌÌÌÌÌÌÌÌ 24.19/178.0019.ÌÌÌÌÌÌÌÌ 29.44/179.0019.ÌÌÌÌÌÌÌÌ 35.06/178.0019.ÌÌÌÌÌÌÌÌ 39.94/180.0045.ÌÌÌÌÌÌÌÌ 19/178.00.0045.ÌÌÌÌÌÌÌÌ 50.44/180.0045.ÌÌÌÌÌÌÌÌ 55.69/179.0061.ÌÌÌÌÌÌÌÌ 50/179.00.0061.ÌÌÌÌÌÌÌÌ 66.19/180.0072.ÌÌÌÌÌÌÌÌ 38/178.00.0072.ÌÌÌÌÌÌÌÌ 77.63/178.0083.ÌÌÌÌÌÌÌÌ 25/178.00.0083.ÌÌÌÌÌÌÌÌ 88.13/178.0093.ÌÌÌÌÌÌÌÌ 56/176.00.0093.ÌÌÌÌÌÌÌÌ 99.19/177.0093.ÌÌÌÌÌÌÌÌ 104.44/177.0011 ÌÌÌÌÌÌÌÌ 0.25/176.000011 ÌÌÌÌÌÌÌÌ 115.31/177.0011 ÌÌÌÌÌÌÌÌ 121.13/177.0011 ÌÌÌÌÌÌÌÌ 126.75/173.0013ÌÌÌÌÌÌÌÌ 2.19/176.00136.ÌÌÌÌÌÌÌÌ 88/176.0000136.ÌÌÌÌÌÌÌÌ 142.69/176.0014ÌÌÌÌÌÌÌÌ 8.31/177.000014ÌÌÌÌÌÌÌÌ 153.75/177.0014ÌÌÌÌÌÌÌÌ 159.38/174.0016ÌÌÌÌÌÌÌÌ 5.38/173.000016ÌÌÌÌÌÌÌÌ 170.63/174.0017ÌÌÌÌÌÌÌÌ 5.88/169.000017ÌÌÌÌÌÌÌÌ 181.13/169.0018ÌÌÌÌÌÌÌÌ 7.13/169.000018ÌÌÌÌÌÌÌÌ 192.56/167.0019ÌÌÌÌÌÌÌÌ 8.38/170.000019ÌÌÌÌÌÌÌÌ 203.81/168.0019ÌÌÌÌÌÌÌÌ 209.25/166.0021 ÌÌÌÌÌÌÌÌ 4.88/165.000021 ÌÌÌÌÌÌÌÌ 220.69/168.0021 ÌÌÌÌÌÌÌÌ 226.13/167.0023ÌÌÌÌÌÌÌÌ 1.94/167.000023ÌÌÌÌÌÌÌÌ 237.56/167.0024ÌÌÌÌÌÌÌÌ 3.38/166.000024ÌÌÌÌÌÌÌÌ 248.81/169.0025ÌÌÌÌÌÌÌÌ 4.25/169.00259.ÌÌÌÌÌÌÌÌ 88/170.00265.69ÌÌÌÌÌÌÌÌ /170.00271.31/1ÌÌÌÌÌÌÌÌ 69.0000271.31/1ÌÌÌÌÌÌÌÌ 277.50/170.0028ÌÌÌÌÌÌÌÌÌ
注意:它应该看起来像前。 241.00/32.00,位置以度数表示,距离以毫米表示。
I'm making a LiDAR project for university but I got stuck a little bit. I am sending a package which includes the exact position of the motor and the distance read from the distance sensor through WiFi and the package should look like this "position/distance". I made a client program in Visual Studio but when I receive the package I have iiiiii at the end of the message and the packages are not coming through in the right order. I am gonna put the code here and if you have any idea of what I am missing I would appreciate it a lot.
#include <iostream>
#include <stdio.h>
#include <string.h>
#include "winsock2.h"
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#pragma warning(disable:4996)
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main()
{
FILE* file;
file = fopen("package.txt", "w");
if (file == NULL) {
printf("Error openning the file\n");
exit(1);
}
int iResult;
WSADATA wsaData;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR)
printf("Error WSAStartup\n");
SOCKET ClientSocket;
ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ClientSocket == INVALID_SOCKET)
{
printf("Error initializing socket with the next error code: %ld\n",
WSAGetLastError());
WSACleanup();
return 0;
}
int Port = 13000;
char IP[14] = "192.168.0.105";
sockaddr_in ServerAddr;
int AddrLen = sizeof(ServerAddr);
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_addr.s_addr = inet_addr(IP);
ServerAddr.sin_port = htons(Port);
if (connect(ClientSocket, (SOCKADDR*)&ServerAddr, AddrLen) == SOCKET_ERROR)
{
printf("Error connecting with the next error code: %ld\n",
WSAGetLastError());
WSACleanup();
return 0;
}
else {
printf("Sikeres kapcsolodas\n");
}
char message[15];
int receiving = 0;
while (receiving != SOCKET_ERROR) {
receiving = recv(ClientSocket, message, sizeof(message), 0);
fprintf(file, "%s\n", message);
printf("%i, %i\n", receiving, sizeof(message));
}
if (receiving == SOCKET_ERROR) {
printf("Error at receiving the message\n");
closesocket(ClientSocket);
WSACleanup();
return 0;
}
fclose(file);
return 0;
}
The message that I get looks like this:
0.00/176.00ÌÌÌÌÌÌÌÌÌÌÌÌÌ
0.75/180.003.75ÌÌÌÌÌÌÌÌÌ
/179.008.63/179ÌÌÌÌÌÌÌÌÌ
.009.008.63/179ÌÌÌÌÌÌÌÌÌ
14.06/179.0019.ÌÌÌÌÌÌÌÌÌ
13/178.00.0019.ÌÌÌÌÌÌÌÌÌ
24.19/178.0019.ÌÌÌÌÌÌÌÌÌ
29.44/179.0019.ÌÌÌÌÌÌÌÌÌ
35.06/178.0019.ÌÌÌÌÌÌÌÌÌ
39.94/180.0045.ÌÌÌÌÌÌÌÌÌ
19/178.00.0045.ÌÌÌÌÌÌÌÌÌ
50.44/180.0045.ÌÌÌÌÌÌÌÌÌ
55.69/179.0061.ÌÌÌÌÌÌÌÌÌ
50/179.00.0061.ÌÌÌÌÌÌÌÌÌ
66.19/180.0072.ÌÌÌÌÌÌÌÌÌ
38/178.00.0072.ÌÌÌÌÌÌÌÌÌ
77.63/178.0083.ÌÌÌÌÌÌÌÌÌ
25/178.00.0083.ÌÌÌÌÌÌÌÌÌ
88.13/178.0093.ÌÌÌÌÌÌÌÌÌ
56/176.00.0093.ÌÌÌÌÌÌÌÌÌ
99.19/177.0093.ÌÌÌÌÌÌÌÌÌ
104.44/177.0011ÌÌÌÌÌÌÌÌÌ
0.25/176.000011ÌÌÌÌÌÌÌÌÌ
115.31/177.0011ÌÌÌÌÌÌÌÌÌ
121.13/177.0011ÌÌÌÌÌÌÌÌÌ
126.75/173.0013ÌÌÌÌÌÌÌÌÌ
2.19/176.00136.ÌÌÌÌÌÌÌÌÌ
88/176.0000136.ÌÌÌÌÌÌÌÌÌ
142.69/176.0014ÌÌÌÌÌÌÌÌÌ
8.31/177.000014ÌÌÌÌÌÌÌÌÌ
153.75/177.0014ÌÌÌÌÌÌÌÌÌ
159.38/174.0016ÌÌÌÌÌÌÌÌÌ
5.38/173.000016ÌÌÌÌÌÌÌÌÌ
170.63/174.0017ÌÌÌÌÌÌÌÌÌ
5.88/169.000017ÌÌÌÌÌÌÌÌÌ
181.13/169.0018ÌÌÌÌÌÌÌÌÌ
7.13/169.000018ÌÌÌÌÌÌÌÌÌ
192.56/167.0019ÌÌÌÌÌÌÌÌÌ
8.38/170.000019ÌÌÌÌÌÌÌÌÌ
203.81/168.0019ÌÌÌÌÌÌÌÌÌ
209.25/166.0021ÌÌÌÌÌÌÌÌÌ
4.88/165.000021ÌÌÌÌÌÌÌÌÌ
220.69/168.0021ÌÌÌÌÌÌÌÌÌ
226.13/167.0023ÌÌÌÌÌÌÌÌÌ
1.94/167.000023ÌÌÌÌÌÌÌÌÌ
237.56/167.0024ÌÌÌÌÌÌÌÌÌ
3.38/166.000024ÌÌÌÌÌÌÌÌÌ
248.81/169.0025ÌÌÌÌÌÌÌÌÌ
4.25/169.00259.ÌÌÌÌÌÌÌÌÌ
88/170.00265.69ÌÌÌÌÌÌÌÌÌ
/170.00271.31/1ÌÌÌÌÌÌÌÌÌ
69.0000271.31/1ÌÌÌÌÌÌÌÌÌ
277.50/170.0028ÌÌÌÌÌÌÌÌÌ
Note: it should look like ex. 241.00/32.00, position expressed in degree and distance in mm.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在此行中:
接收
设置为实际收到的字节数。它不一定是您传递的缓冲区的大小(在您的情况下,少了几个字节)。
但是,当您将文件转储到此行中:
您忽略接收到的字节数。
使用
fprintf
与“%s”使用零终止字符数组,resv
不会添加零终止。您可以通过在
fprintf
之前添加此行来解决此问题:另一个问题是允许填充字节的最大数量
recv
。由于我们要保留一个字节以零终止,因此呼叫
recv
应该是:IE允许接收填充
sizeof(message)-1
bytes,并保留一个零终止。In this line:
receiving
is set to the number of bytes actually received.It is not necessarily the size of the buffer you pass in (in your case it is a few bytes less).
But when you dump it th the file in this line:
You ignore the number of bytes received.
Using with
fprintf
with "%s" requires a zero terminated array of characters, andresv
does not add the zero termination.You can solve this issue by adding this line before the
fprintf
:Another issue is the maximum number of bytes
recv
is allowed to fill.Since we want to keep one byte for the zero termination, the call to
recv
should be:I.e. allow receive fill only
sizeof(message)-1
bytes and keep one for the zero termination.