2

我有 1 台在 Unix 中使用 C++ 和 c 套接字构建的服务器。客户端正在使用 QT 和它附带的套接字 api。

服务器向客户端发送 345 字节的数据。

从服务器发送消息:

void Moderator::testSynch(){
  int type = (int) SYNCRHONIZE_M;
  //Update all connected clients with info about other clients
  for(int i = 0; i<nrOfClients_; i++){
    const TcpSocket &clientSocket = clients_[i].getSocket();
    int clientID = clients_[i].getID();

    int tempType = htonl(type);
    int tempClientID = htonl(clientID);
    int tempNrOfClients = htonl(funNrOfClients);

    clientSocket.writeData((const char*) &tempType, sizeof(tempType));
    clientSocket.writeData((const char*) &tempClientID, sizeof(tempClientID));
    clientSocket.writeData((const char*) &tempNrOfClients, sizeof(tempNrOfClients));

    for(int j = 0; j<nrOfClients; j++){ //Send info about connectecd clients

        int tempLength = (int) clients_[j].getName().length();
        int tempID = clients_[j].getID();
        string tempName = clients_[j].getName();

        tempID = htonl(tempID);
        tempLength = htonl(tempLength);
        clientSocket.writeData((const char*) &tempID, sizeof(tempID));
        clientSocket.writeData((const char*) &tempLength, sizeof(tempLength));
        clientSocket.writeData(tempName.c_str(), (int)tempName.length());

    }
  }
}

bool TcpSocket::writeData(const char* buffer, int length)const{
  size_t bytesLeft = length;
  ssize_t bytesWritten = 0;

  while((bytesWritten = write(socketFD_, buffer, bytesLeft)) > 0){
    bytesLeft -= bytesWritten;
    buffer += bytesWritten;
  }
  return bytesLeft == 0;
}

在客户端读取消息:

 void ChatClient::readMessage(Message &message){

 if(socket_->readData((char*) &type, sizeof(type))){
   if(type == SYNCRHONIZE_M){
        int nrOfUsers = 0;

        socket_->readData((char*) &ID_, sizeof(ID_)); //Set the client ID that server gave us
        socket_->readData((char*) &nrOfUsers, sizeof(nrOfUsers));

        ID_ = ntohl(ID_);
        nrOfUsers = ntohl(nrOfUsers);
        qDebug("%s=%d", "nrOfUsers", nrOfUsers);
        message.setMessageType(SYNCRHONIZE_M);
        messageOK = true;
        for(int i = 0; i<nrOfUsers; i++){ //Update client with all connected users to server
            int userID = 0;
            int nameLength = 0;

            socket_->readData((char*) &userID, sizeof(userID));
            socket_->readData((char*) &nameLength, sizeof(nameLength));

            userID = ntohl(userID);
            nameLength = ntohl(nameLength);

            if(nameLength > 0){
                qDebug("%s=%d", "nameLength", nameLength);
                buffer = new char[nameLength];
                socket_->readData(buffer, nameLength);

                message.addUser(ConnectedUser(buffer, nameLength, userID));
                delete [] buffer;
            }
        }
    }
}
}

bool TcpSocket::readData(char* buffer, int length){
    int bytesLeft = length;
    int bytesRead = 0;

    while((bytesRead = qSocket_->read(buffer, bytesLeft)) > 0){
       bytesLeft -= bytesRead;
       buffer += bytesRead;

    }
    return bytesLeft == 0;
}

我遇到的问题是有时无法立即获得来自服务器的整个消息。

例如,前 45 个字节在客户端中可用。然后客户端尝试读取整个消息(345 字节),这会导致奇怪的行为。客户端读取完下一个 300 字节后立即可用。

在套接字之间发送消息的最佳方式是什么?另外,如何确定是否已收到整个消息?

4

1 回答 1

0

你有一些只存在于你脑海中的“信息”的概念。您的代码中没有任何内容反映了这一点。如果您有一个涉及发送“消息”的应用程序协议,那么您需要编写代码以发送消息和代码以根据您的协议对消息的定义来接收消息TCP 仅提供字节流,并且不会将它们粘合在一起以供应用程序使用大于一个字节的内容。

于 2012-12-29T01:04:32.130 回答