File tree Expand file tree Collapse file tree 1 file changed +4
-6
lines changed Expand file tree Collapse file tree 1 file changed +4
-6
lines changed Original file line number Diff line number Diff line change 33[ ![ std-badge]] [ std ] [ ![ cat-net-badge]] [ cat-net ]
44
55In this example, the port is displayed on the console, and the program will
6- listen until a request is made. ` SocketAddrV4 ` assigns a random port when
7- setting port to 0.
6+ listen until a request is made. ` TcpListener::bind ` uses a random port
7+ allocated by the OS when requested to bind to port 0.
88
99``` rust,edition2018,no_run
10- use std::net::{SocketAddrV4, Ipv4Addr, TcpListener} ;
10+ use std::net::TcpListener;
1111use std::io::{Read, Error};
1212
1313fn main() -> Result<(), Error> {
14- let loopback = Ipv4Addr::new(127, 0, 0, 1);
15- let socket = SocketAddrV4::new(loopback, 0);
16- let listener = TcpListener::bind(socket)?;
14+ let listener = TcpListener::bind("localhost:0")?;
1715 let port = listener.local_addr()?;
1816 println!("Listening on {}, access this port to end the program", port);
1917 let (mut tcp_stream, addr) = listener.accept()?; //block until requested
You can’t perform that action at this time.
0 commit comments