otm
otm

Reputation: 745

Why there is no `shutdown` method for TcpListener and UdpSocket in Rust

According to the documentation, TcpListener and UdpSocket will be automatically closed when the value is dropped/out of scrope. But why is there no shutdown method to let me manually close them?

https://doc.rust-lang.org/stable/std/net/struct.TcpListener.html

Upvotes: 3

Views: 2964

Answers (3)

Ibraheem Ahmed
Ibraheem Ahmed

Reputation: 13538

You can manually close a listener simply by explicitly dropping:

drop(listener);

Upvotes: 0

iggy
iggy

Reputation: 1328

You confuse TcpListener and TcpStream.

'shutdown' in TCP sockets has a technical meaning. Shutdown on the send side transmits a FIN to the remote. Shutdown on the receive side means that any arriving data segments will get a RST response. These definitions are only applicable to a TCP connection in data transfer state, not to listening sockets.

Upvotes: 7

loops
loops

Reputation: 5635

You can use std::mem::drop to drop a value early:

let listener = TcpListener::bind("127.0.0.1:80")?;
// ...
drop(listener);
// ...

There is no shutdown method because it isn't needed. The existing ownership system is already good enough for keeping track of if a socket is usable, and there is nothing you can do with a closed socket anyways.

Upvotes: 8

Related Questions