Skip to content

Commit 779ce5b

Browse files
committed
When running serve check if port is available
Give a nice explanation if it is used or if it is a protected port. Closes rust-lang#2631
1 parent b7a27d2 commit 779ce5b

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/cmd/serve.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
6969
update_config(&mut book);
7070
book.build()?;
7171

72+
let listener = std::net::TcpListener::bind(&address);
73+
if listener.is_err() {
74+
return Err(anyhow::anyhow!("Unable to bind to address: {} (It is either a protected port or it is used already. Try to another port or use --port 0 to select a random port. ", address).into());
75+
}
76+
drop(listener);
77+
7278
let sockaddr: SocketAddr = address
7379
.to_socket_addrs()?
7480
.next()

0 commit comments

Comments
 (0)