2

UnixでC++およびcソケットを使用して構築されたサーバーが1つあります。クライアントは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 はバイト ストリームのみを提供し、アプリケーション用にそれらを 1 バイトより大きなものに結合しません。

于 2012-12-29T01:04:32.130 に答える