我需要從一個java服務器發送一個字符串到一個C++客戶端,但客戶端收到消息分成塊或有時只是消息的某些部分,我怎樣才能看到整個消息? 我不需要發送和接收,因爲我收到消息,但是它被跳入塊,它在不同的時間到達某些部分,但我需要這些塊在一起,而不是分開。如何查看從服務器Java到C++客戶端的整個消息?
這是我的代碼:
C++客戶:
while(1){
if(send(sock , nCartella,(unsigned)strlen(nCartella), 0) < 0){
cout <<"Invio fallito"<<endl;
return 1;
}
cout << "Messaggio inviato"<<endl;
if (recv(sock, buff, sizeof(buff)-1, 0) < 0)
{
cout << "Error receiving message " << endl;
return 1;
}
message.append(buff);
break;
}
int finale=strcspn(buff,"\n");
for(int a=0;a<finale;a++){
cout<<buff[a];
}
cout << endl;
Java服務器:
DataOutputStream os = new DataOutputStream(client.getOutputStream());
os.writeBytes(num_cartella+"\n");
什麼是錯的,請麻我解決這個問題
'while(1){/*...*/ break;}'的目的是什麼? – user463035818
這是你兩個小時前問的同一個問題的重複! – SergeyA
@SergeyA不,不一樣。在我需要發送和接收字符串之前,我現在收到併發送數據,但字符串沒有同時到達客戶端,我不知道如何接收服務器發送的全部消息。 –