diff --git a/README.md b/README.md index 122809c..d103c75 100644 --- a/README.md +++ b/README.md @@ -208,7 +208,8 @@ It responds to: * TCP packets on port 6971 (`cat /dev/urandom | socat stdio tcp4-connect:192.168.69.1:6971`), which will be ignored. -The buffers are only 64 bytes long, for convenience of testing resource exhaustion conditions. +Except for the socket on port 6971. the buffers are only 64 bytes long, for convenience +of testing resource exhaustion conditions. ### examples/client.rs diff --git a/examples/server.rs b/examples/server.rs index 5bf3c31..b8641f9 100644 --- a/examples/server.rs +++ b/examples/server.rs @@ -63,7 +63,7 @@ fn main() { // udp:6969: respond "yo dawg" { let socket: &mut UdpSocket = sockets.get_mut(udp_handle).as_socket(); - if !socket.endpoint().is_specified() { + if !socket.is_open() { socket.bind(6969).unwrap() } diff --git a/src/socket/tcp.rs b/src/socket/tcp.rs index bd738d6..128505c 100644 --- a/src/socket/tcp.rs +++ b/src/socket/tcp.rs @@ -348,8 +348,9 @@ impl<'a> TcpSocket<'a> { /// Start listening on the given endpoint. /// - /// This function returns an error if the socket was open; see [is_open](#method.is_open). - /// It also returns an error if the specified port is zero. + /// This function returns `Err(Error::Illegal)` if the socket was already open + /// (see [is_open](#method.is_open)), and `Err(Error::Unaddressable)` + /// if the port in the given endpoint is zero. pub fn listen(&mut self, local_endpoint: T) -> Result<()> where T: Into { let local_endpoint = local_endpoint.into(); diff --git a/src/socket/udp.rs b/src/socket/udp.rs index 5333e02..7ae772c 100644 --- a/src/socket/udp.rs +++ b/src/socket/udp.rs @@ -101,18 +101,25 @@ impl<'a, 'b> UdpSocket<'a, 'b> { /// Bind the socket to the given endpoint. /// - /// Returns `Err(Error::Illegal)` if the socket is already bound, - /// and `Err(Error::Unaddressable)` if the port is unspecified. + /// This function returns `Err(Error::Illegal)` if the socket was open + /// (see [is_open](#method.is_open)), and `Err(Error::Unaddressable)` + /// if the port in the given endpoint is zero. pub fn bind>(&mut self, endpoint: T) -> Result<()> { let endpoint = endpoint.into(); if endpoint.port == 0 { return Err(Error::Unaddressable) } - if self.endpoint.port != 0 { return Err(Error::Illegal) } + if self.is_open() { return Err(Error::Illegal) } self.endpoint = endpoint; Ok(()) } + /// Check whether the socket is open. + #[inline] + pub fn is_open(&self) -> bool { + self.endpoint.port != 0 + } + /// Check whether the transmit buffer is full. #[inline] pub fn can_send(&self) -> bool {