Winsock 2,将变量凝结成一个字符串,将其发送出去,然后再进行回收并将其读回

Winsock 2, condensing variables into a string, sending it out, then recieving it and reading it back

本文关键字:然后 字符串 变量 一个 Winsock      更新时间:2023-10-16

我正在编写一些涉及使用惯性立方体跟踪器的代码,这些代码会积极改变其偏航螺距和滚动(以学位为单位),我需要设置一个服务器,以按顺序读取该信息网络信息。到目前为止,我已经创建了一个客户端和服务器,但是我遇到的问题是要么将信息发送到一个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}"

然后,在客户端上,您将不断阅读到缓冲数据中,直到到达"}",然后可以处理此消息并解析不同的组件。

相关文章: