유닉스에는 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 바이트가 사용 가능하게됩니다.
소켓간에 메시지를 보내는 가장 좋은 방법은 무엇입니까? 또한 전체 메시지를 받았는지 어떻게 확인할 수 있습니까?
TCP는 어떤 종류의 메시지 경계도 제공하지 않으며 단지 바이트 스트림입니다. 모든 예상 데이터를 얻을 때까지 루프에서'read()'를 호출해야합니다. 'readXXX' 함수의 구현을 게시하면 누군가가 좀 더 구체적인 조언을 제공 할 수 있습니다. – Barmar
업데이트 된 게시물 확인. – Carlj901
나에게 맞는 것 같습니다. "이상한 행동"이란 무엇입니까? 소켓이 블로킹 모드에 있다고 가정하면,'readString'은 전체 메시지가 수신되기를 기다려야합니다. 비 차단 모드 인 경우'read()'를 호출 할 때'EWOULDBLOCK' 오류를 검사해야합니다. – Barmar