【发布时间】:2015-05-19 10:45:23
【问题描述】:
我目前正在平板电脑(客户端)和 MAC/PC(服务器)之间开发客户端/服务器架构。我在双方都做一些实时渲染,我需要两者之间的沟通。
问题是我需要对从客户端获得的字符串(基本上是一个旋转矩阵)进行一些操作。因此,该字符串最多是 16 个浮点数,我之前将其转换为逗号分隔值字符串。 因此,我应该从我的客户那里得到的是:
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
在服务器端,我对该字符串进行了一些处理,以将我的旋转矩阵作为 16 个元素的浮点数组取回。问题是有时我一次从服务器端的客户端获得超过 16 个元素。我例如得到
1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0 1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0 1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0
所以当我尝试拆分它时,我超过了 16 个元素的限制,这对我来说根本不好。 我的问题是:有没有办法防止服务器和/或客户端一次读取/发送多个完整的矩阵?由于我使用的是平板电脑和一些实时渲染,因此我希望能够尽可能多地节省处理能力。
这是我正在使用的代码(只是 sn-ps,因为文件很大)
客户:
if (connected == true && matrixupdated == true && this.hasMatrixChanged()){
try {
this.inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
this.outToServer= new DataOutputStream(clientSocket.getOutputStream());
this.sentence = this.getStringFromMatrix();
outToServer.writeBytes(sentence + '\n');
this.hasServerProcessed = false ;
System.arraycopy(matrix, 0, previousMatrix, 0, 16); //I check whether the matrix changes enough for me to send it to the server
}catch (Exception e) {
Log.e("ClientActivity", "S: Error", e);
}
this.matrixupdated = false ;
服务器:
while( (read_size = recv(sock , client_message , 2000 , 0)) > 0 )
{
smatrix = client_message ; //smatrix is a true c++ string
pthread_mutex_lock(&mymutex);
pthread_cond_wait(&mycondition, &mymutex); // prevent real-time rendering to try and use the matrix at the same time as this function
std::stringstream ss(smatrix);
while(std::getline(ss, tok, ',')) {
matrix[i] = ::atof(tok.c_str());
i++ ;
}
i = 0 ;
pthread_mutex_unlock(&mymutex);
}
【问题讨论】:
-
如果您将它们排成一行(每行 16 个),您可以使用新行来区分 ... 行。
-
这正是我所做的,但我想知道是否有更有效和更干净的方法来做到这一点
标签: java android c sockets client-server