Winsock 2,将变量凝结成一个字符串,将其发送出去,然后再进行回收并将其读回
Winsock 2, condensing variables into a string, sending it out, then recieving it and reading it back
我正在编写一些涉及使用惯性立方体跟踪器的代码,这些代码会积极改变其偏航螺距和滚动(以学位为单位),我需要设置一个服务器,以按顺序读取该信息网络信息。到目前为止,我已经创建了一个客户端和服务器,但是我遇到的问题是要么将信息发送到一个chunck,然后将其读回三个并打印出来,或者指定发送的匹配项。
。if( currentTrackerH > 0 )
{
int iSendResult1;
int iSendResult2;
int iSendResult3;
char EulerBuffer0[64];
char EulerBuffer1[64];
char EulerBuffer2[64];
showStationData( currentTrackerH, &TrackerInfo,
&Stations[station-1], &data.Station[station-1],
&StationsHwInfo[currentTrackerH-1][station-1],
showTemp);
//send to the server
do{
sprintf(EulerBuffer0, "%f", data.Station[station-1].Euler[0]);
iSendResult1= send(Connection, EulerBuffer0, sizeof(data.Station[station-1].Euler[0]), NULL);
sprintf(EulerBuffer1, "%f", data.Station[station-1].Euler[1]);
iSendResult2= send(Connection, EulerBuffer1, sizeof(data.Station[station-1].Euler[1]), NULL);
sprintf(EulerBuffer2, "%f", data.Station[station-1].Euler[2]);
iSendResult3= send(Connection, EulerBuffer2, sizeof(data.Station[station-1].Euler[2]), NULL);
}while ((iSendResult1 || iSendResult2 || iSendResult3)>0);
//shutdown the socket when there is no more data to send
iSendResult1 = shutdown(Connection, SD_SEND);
if (iSendResult1 == SOCKET_ERROR)
{
printf("shutdown failed with error: %dn", WSAGetLastError());
closesocket(Connection);
WSACleanup();
return 1;
}
}
}
这是我的客户端,在这里我将服务器端放置。网络连接,我的跟踪器代码工作正常,但是发送和接收是所有这些都变得奇怪的地方。
//begin recieving data
char yaw[256];
char pitch[256];
char roll[256];
int iResult1;
int iResult2;
int iResult3;
float fyaw, fpitch, froll;
do{
do {
iResult1= recv(newConnection, yaw,sizeof(yaw),NULL);
} while( iResult1 == 0 );
fyaw = atof(yaw);
do {
iResult2= recv(newConnection, pitch,sizeof(pitch),NULL);
} while( iResult1 == 0 );
fpitch = atof(pitch);
do {
iResult3= recv(newConnection, roll,sizeof(roll),NULL);
} while( iResult1 == 0 );
froll = atof(roll);
printf("(%f,%f,%f)deg n",
fyaw, fpitch, froll);
}while(1);
我对C 的了解并不是很棒,任何帮助都会很可爱。谢谢!
您的代码中存在各种错误。让我们尝试分解并纠正误解(我假设您正在使用TCP。)您正在发送一个尺寸的缓冲区,但可能会带来另一个大小的缓冲区。浮子的尺寸(偏航)与此浮子的字符串表示的大小不同。
单个项目的调用发送/RECV很慢。理想情况下,您将定义一个简单的协议。该协议中的消息将是一个字符串,其中包含您希望传输的所有值。您可以在您在数据流中阅读的接收端上使用单个send()
发送该消息,并查找何时收到完整消息的特定标记。然后,您处理该消息,将不同的组件分成偏航/俯仰/滚动变量。
字符串消息的一个示例是: "{yaw=1.34;pitch=2.45;roll=5.67}"
然后,在客户端上,您将不断阅读到缓冲数据中,直到到达"}",然后可以处理此消息并解析不同的组件。
相关文章:
- 如何在字符串中找到字符,然后在C++中提取其余的字符串
- 将子字符串字符从字符串值转换为 int,然后将其分配给 int 变量
- 字符串变成链表,然后检查括号余额
- 我正在尝试从输入文件中读取,然后使用它们的子字符串比较字符串的特定部分
- 比较字符串的 GetLine 工作一次,然后比较之后不起作用
- C++ 按数值对元组<字符串、浮点数>然后按字典顺序排序的向量
- C++读取大文件并将其保存到字符串中,然后删除特定的随机单词
- 如何创建一个C++程序来读取字符串数组中的信息,然后将其排序到类中?
- 检查字符串是否是数字,然后将该数字转换为 int?
- 如何根据长度然后字母顺序对数组字符串进行排序?
- 将 int 转换为字符串,然后连接另一个变量以创建完整扩展名,然后将其转换为 const_char*
- 如何将两个 jlong 数据类型转换为 jstring,然后将两个字符串连接在一起以便从 JNI 将字符串返回给 jav
- 当找到来自 RHS 的字符"\"时,从主字符串中获取单词或子字符串,然后擦除其余部分
- 将无符号字符转换为字符串,然后再次转换为无符号字符
- 在构造函数中创建一个字符串数组,然后在主函数中使用它
- C 从文件中读取文本,然后将字符串解析为int数组
- JNI将字符串从Java传递到C ,然后将C 字符串传递给字符串数组
- C :在字符串中找到一个char,然后在先前的char之后找到另一个字符
- 获取一个输入字符串,将其转换为其十六进制值,然后将其十六进制存储为 int
- 文字符号和字符串变量之间的串联然后返回常量字符*