如何使用TCP / IP在c ++中同时将许多矩阵从服务器发送到客户端,反之亦然

How can I send many matrices from server to client at the same time and vice versa in c++ using TCP/IP?

本文关键字:服务器 许多矩 反之亦然 客户端 IP TCP 何使用      更新时间:2023-10-16

我的代码中有两个服务器端的两个函数sendMatrixPossendMatrixTrack的调用,以按顺序发送两个矩阵。 我把矩阵 evrey fram 发送给客户

服务器

void MyTestPlugin::sendMSGP()
{
    if(!connected)
        return;
    if ( (numbytes = send(client_skt, buf, 16*sizeof(float) ,0)) == -1){
        std::cerr<<"server, normal send error"<<std::endl;
        exit(1);
    }
}
void MyTestPlugin::sendMSGT()
{
    if(!connected)
        return;
    if ( (numbytes = send(client_skt, trackBuf, 16*sizeof(float) ,0)) == -1){
        std::cerr<<"server, normal send error"<<std::endl;
        exit(1);
    }
}
void MyTestPlugin::sendMatrixPos()
{
    if(!connected)
        return;
   Matrixd transNode = PluginHelper::getObjectMatrix();
   for (int i=0; i<4; ++i)
     for (int j=0; j<4; ++j)
        matrixP[i*4+j] = transNode(i,j);
   for(int i=0; i<16; i++)
   {
     cerr <<"matrixP:" << i << "   " << matrixP[i] << endl;
   }
    memcpy(buf,matrixP,sizeof(float)*16);
    sendMSGP();
}
void MyTestPlugin::sendMatrixTrack()
{
    if(!connected)
        return;
   Matrixd trackNode = PluginHelper::getHeadMat(1);
   for (int i=0; i<4; ++i)
     for (int j=0; j<4; ++j)
        matrixT[i*4+j] = trackNode(i,j);
   for(int i=0; i<16; i++)
   {
     cerr <<"matrixT:" << i << "   " << matrixT[i] << endl;
   }
    memcpy(trackBuf,matrixT,sizeof(float)*16);
    sendMSGT();
}
void MyTestPlugin::preFrame()
{ 
  sendMatrixPos();   
  sendMatrixTrack();
}

另一方面,我在客户端上调用了两个函数,以便函数每帧按顺序从服务器接收两个矩阵。

客户

void osgMain::draw()
{
    recvMSG();
    ...
    recvMSG();
}

void osgMain::recvMSG()
{
    numbytes = recv(client_skt, buf, bufferSize,0);
        if ( numbytes == -1 ){
            //__android_log_print(ANDROID_LOG_ERROR,"jni client","recv error");
            //exit(1);
        }
        else
        {
            memcpy (matrix, buf, numbytes);
        }
}

我使用了TCP/IP协议。我需要发送两个矩阵并同时(不是顺序)接收它们,我该怎么做这一步?或者如何同时发送两个矩阵?

客户端的 BUFFERSIZE 1024 和服务器端的 1024...

您的传输将受到"网络管道大小"的限制 - 除非您有 40Gb/s 的网卡,否则您不太可能从尝试并行发送数据中受益 - 您只会使您的代码更加复杂 - 只要您的代码被编写为异步发送/接收,这就是您所需要的。