简体   繁体   中英

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). When I send messages from PC client to server, I receive one message with a single receive(). But when I send messages from android client to server, a single receive() ends up having more than one messages.

I'm using TCP and I know it's stream-based protocol. But why PC client always sends 1 message at a time. And how can I achieve it with android client too?

There is nothing whatsoever you can do that will ensure you receive an entire message in a single read() call. There is no such thing as a message in TCP. It is a byte-stream protocol. If you want messages you have to implement them yourself, with an application protocol.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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