简体   繁体   English

套接字编程:一次调用接收一条完整的消息

[英]Socket Programming: Receive one complete message with single call

I have a server in PC (written in C#), one client in PC (written in Java), and one client in android phone (same code as PC client).我有一台 PC 上的服务器(用 C# 编写),一台 PC 上的客户端(用 Java 编写),还有一个 android 手机上的客户端(与 PC 客户端的代码相同)。 When I send messages from PC client to server, I receive one message with a single receive().当我从 PC 客户端向服务器发送消息时,我通过一个 receive() 收到一条消息。 But when I send messages from android client to server, a single receive() ends up having more than one messages.但是,当我从 android 客户端向服务器发送消息时,单个 receive() 最终会收到多个消息。

I'm using TCP and I know it's stream-based protocol.我正在使用 TCP,我知道它是基于流的协议。 But why PC client always sends 1 message at a time.但是为什么PC客户端总是一次发送1条消息。 And how can I achieve it with android client too?我怎样才能用 android 客户端实现它呢?

There is nothing whatsoever you can do that will ensure you receive an entire message in a single read() call.您无法做任何事情来确保您在单个read()调用中收到完整的消息。 There is no such thing as a message in TCP. TCP 中没有消息这样的东西。 It is a byte-stream protocol.它是一种字节流协议。 If you want messages you have to implement them yourself, with an application protocol.如果你想要消息,你必须自己实现它们,使用应用程序协议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM