Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

70%
+5 −1
Q&A Read all data from TCP stream in Rust

I'd like to write a TCP client in Rust that can receive the entire message sent by a server, but I have no information about the length of a message. I'm aware that TCP doesn't preserve message bo...

1 answer  ·  posted 1y ago by Matthias Braun‭  ·  edited 1y ago by Matthias Braun‭

Question rust tcp
#4: Post edited by user avatar Matthias Braun‭ · 2023-05-02T11:32:23Z (over 1 year ago)
wording
  • I'd like to write a TCP client in Rust that can receive the entire message sent by a server, but I have no information about the length of a message.
  • I'm aware that TCP [doesn't preserve message boundaries](https://stackoverflow.com/questions/9563563/what-is-a-message-boundary#9563694).
  • **Still, what's the best I can do to read the entire message from the [`TcpStream`](https://doc.rust-lang.org/std/net/struct.TcpStream.html)?**
  • In the scenario the client should work, there's no protocol for the messages sent from server to client: The client doesn't know beforehand how many bytes are in a message and the message doesn't have a header (as in HTTP) that would contain the message length. There's no special delimiter that marks the end of a message.
  • Also, the server might keep the TCP connection open after sending a message, meaning I can not rely on every message being completed with a server [FIN](https://en.wikipedia.org/wiki/Transmission_Control_Protocol#Connection_termination).
  • I'd like to do this preferably using Rust's standard library only, no other dependencies.
  • I'd like to write a TCP client in Rust that can receive the entire message sent by a server, but I have no information about the length of a message.
  • I'm aware that TCP [doesn't preserve message boundaries](https://stackoverflow.com/questions/9563563/what-is-a-message-boundary#9563694).
  • **Still, what's the best I can do to read the entire message from the [`TcpStream`](https://doc.rust-lang.org/std/net/struct.TcpStream.html)?**
  • In the scenario the client should work, there's no protocol for the messages sent from server to client: The client doesn't know beforehand how many bytes are in a message and the message doesn't have a header (as in HTTP) that would contain the message length. There's no special delimiter that marks the end of a message.
  • Also, the server might keep the TCP connection open after sending a message, meaning I can not rely on every message being completed with a server [FIN](https://en.wikipedia.org/wiki/Transmission_Control_Protocol#Connection_termination).
  • I'd like to do this using Rust's standard library only, no other dependencies.
#3: Nominated for promotion by user avatar Alexei‭ · 2023-05-01T07:12:07Z (over 1 year ago)
#2: Post edited by user avatar Matthias Braun‭ · 2023-04-30T12:06:35Z (over 1 year ago)
make title more specific
  • Read all data from TCP stream
  • Read all data from TCP stream in Rust
#1: Initial revision by user avatar Matthias Braun‭ · 2023-04-30T11:47:50Z (over 1 year ago)
Read all data from TCP stream
I'd like to write a TCP client in Rust that can receive the entire message sent by a server, but I have no information about the length of a message.

I'm aware that TCP [doesn't preserve message boundaries](https://stackoverflow.com/questions/9563563/what-is-a-message-boundary#9563694).

**Still, what's the best I can do to read the entire message from the [`TcpStream`](https://doc.rust-lang.org/std/net/struct.TcpStream.html)?**

In the scenario the client should work, there's no protocol for the messages sent from server to client: The client doesn't know beforehand how many bytes are in a message and the message doesn't have a header (as in HTTP) that would contain the message length. There's no special delimiter that marks the end of a message.

Also, the server might keep the TCP connection open after sending a message, meaning I can not rely on every message being completed with a server [FIN](https://en.wikipedia.org/wiki/Transmission_Control_Protocol#Connection_termination).

I'd like to do this preferably using Rust's standard library only, no other dependencies.