This question already has an answer here:
这个问题在这里已有答案:
- How to send a string from java server to C++ client? 1 answer
如何从java服务器向C ++客户端发送字符串? 1个答案
I need to send a string from a java server to a c++ client but the client receive the message divided into blocks or sometimes only some parts of the message, How can I see the whole message? I don't need to send and receive because I receive the message but it is dived into blocks and it arrive some parts in different time but I need that these blocks are together and not separated.
我需要从java服务器向c ++客户端发送一个字符串,但是客户端收到的消息被分成块或者有时只是消息的某些部分,我怎样才能看到整个消息?我不需要发送和接收,因为我收到消息但是它被潜入块中并且它在不同时间到达某些部分但我需要这些块在一起而不是分开。
This is my code:
这是我的代码:
C++ client :
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 server:
DataOutputStream os = new DataOutputStream(client.getOutputStream());
os.writeBytes(num_cartella+"\n");
What is wrong?Please hemp me to resolve this problem
有什么问题?请麻烦我解决这个问题
1 个解决方案
#1
3
-
Precisely define what you mean by a message. (Perhaps it's "up to 1,023 printable characters that do not contain a newline followed by a newline". Or whatever makes sense for your application.)
准确定义消息的含义。 (也许它是“最多1,023个可打印的字符,不包含换行符后跟换行符。”或者对你的应用程序有意义的任何内容。)
-
Write a function that receives a message, as you've defined it above.
编写一个接收消息的函数,如上面所定义的那样。
-
Make sure that your "send" function sends messages, as you've defined it above.
确保您的“发送”功能发送消息,就像您在上面定义的那样。
The recv
function just receives a bunch of bytes. It has no idea what your protocol considers to be a message or how to receive one. When you use TCP and you need application-level messages, you have to actually write code to send and receive application-level messages. You didn't do that.
recv函数只接收一堆字节。它不知道您的协议认为什么是消息或如何接收消息。当您使用TCP并且需要应用程序级消息时,您必须实际编写代码以发送和接收应用程序级消息。你没有这样做。
When you use TCP to implement some application-level protocol, you have to actually define and implement that protocol. Since you need to send and receive messages, and TCP isn't a message protocol, you need to implement a message protocol on top of TCP. That means you have to both choose what the protocol will be and write send and receive functions that implement that choice.
当您使用TCP实现某些应用程序级协议时,您必须实际定义并实现该协议。由于您需要发送和接收消息,并且TCP不是消息协议,因此您需要在TCP之上实现消息协议。这意味着您必须选择协议的内容并编写实现该选择的发送和接收函数。
Also, I notice that you ignore the return value of recv
. So your code has no idea how many bytes it received. How can append
possibly know how many bytes to append?
另外,我注意到你忽略了recv的返回值。所以你的代码不知道收到了多少字节。如何追加可能知道要追加多少字节?
Similarly, you call strcspn
on the buffer. But strcspn
is only for C-style strings. If you want to return the message as a C-style string, again, you have to write code to do that. The recv
function just returns chunks of raw bytes, not C-style strings.
同样,您在缓冲区上调用strcspn。但strcspn仅适用于C风格的字符串。如果要将消息作为C样式字符串返回,则必须再编写代码来执行此操作。 recv函数只返回原始字节的块,而不是C样式的字符串。
#1
3
-
Precisely define what you mean by a message. (Perhaps it's "up to 1,023 printable characters that do not contain a newline followed by a newline". Or whatever makes sense for your application.)
准确定义消息的含义。 (也许它是“最多1,023个可打印的字符,不包含换行符后跟换行符。”或者对你的应用程序有意义的任何内容。)
-
Write a function that receives a message, as you've defined it above.
编写一个接收消息的函数,如上面所定义的那样。
-
Make sure that your "send" function sends messages, as you've defined it above.
确保您的“发送”功能发送消息,就像您在上面定义的那样。
The recv
function just receives a bunch of bytes. It has no idea what your protocol considers to be a message or how to receive one. When you use TCP and you need application-level messages, you have to actually write code to send and receive application-level messages. You didn't do that.
recv函数只接收一堆字节。它不知道您的协议认为什么是消息或如何接收消息。当您使用TCP并且需要应用程序级消息时,您必须实际编写代码以发送和接收应用程序级消息。你没有这样做。
When you use TCP to implement some application-level protocol, you have to actually define and implement that protocol. Since you need to send and receive messages, and TCP isn't a message protocol, you need to implement a message protocol on top of TCP. That means you have to both choose what the protocol will be and write send and receive functions that implement that choice.
当您使用TCP实现某些应用程序级协议时,您必须实际定义并实现该协议。由于您需要发送和接收消息,并且TCP不是消息协议,因此您需要在TCP之上实现消息协议。这意味着您必须选择协议的内容并编写实现该选择的发送和接收函数。
Also, I notice that you ignore the return value of recv
. So your code has no idea how many bytes it received. How can append
possibly know how many bytes to append?
另外,我注意到你忽略了recv的返回值。所以你的代码不知道收到了多少字节。如何追加可能知道要追加多少字节?
Similarly, you call strcspn
on the buffer. But strcspn
is only for C-style strings. If you want to return the message as a C-style string, again, you have to write code to do that. The recv
function just returns chunks of raw bytes, not C-style strings.
同样,您在缓冲区上调用strcspn。但strcspn仅适用于C风格的字符串。如果要将消息作为C样式字符串返回,则必须再编写代码来执行此操作。 recv函数只返回原始字节的块,而不是C样式的字符串。