How to write a simple tcp client and server


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("").unwrap();

    let _ = stream.write(&[1]); // ignore the Result
    let _ = [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:

There are many other examples around online:

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.