can anyone please explain tcp client. does anyone have a good documentation regarding tcp client and server ? please help me.
Hi Harry -
Hopefully I can provide some pointers to useful material elsewhere on the Internet. Make sure that . If the syntax looks confusing, I recommend rereading the Book (I must have read it ~10 times by now).
How to write a simple TCP client
The simplest example I've been able to find comes from the standard library documentation for the std::net::TcpStream
struct:
use std::io::prelude::*;
use std::net::TcpStream;
{
let mut stream = TcpStream::connect("127.0.0.1:34254").unwrap();
let _ = stream.write(&[1]); // ignore the Result
let _ = stream.read(&mut [0; 128]); // ignore this too
} // the stream is closed here
We create a stream with connect
, then we can either read
from or write
to that stream as we would expect.
How to write a simple TCP server
In general terms, a server will bind
to a socket, rather than connect
to it.. then when others connect
, the server will accept
the connection, often in another thread. For more detail, I recommend looking at the Stack Overflow question of the same name:
http://stackoverflow.com/q/17445485
There are many other examples around online:
- A multi-threaded server/client chat system
- Here is a very good article describing websockets from Rust
- A simple TCP server that accepts HTTP requests
Any good documentation?
There are several tutorials and other material online. It's hard to know what the best sources for you would be, as "good" documentation for you is the documentation that matches your experience level.
When you are searching online - make sure that you are reading a blog post for Rust 1.0 or later!
Thank you so much for this information.
Hi all,
Thank you timClicks for your reply. If it's possible I have a few questions...
Is it possible that this rust tcp server received the data when listening on localhost and sends everything to a remote host? how do you do that?
Thank you for any pointers you might have (or anyone lese).