pub struct Socket<Address> { /* private fields */ }Expand description
A POSIX socket.
Implementations§
Source§impl<Address: AsSocketAddress> Socket<Address>
impl<Address: AsSocketAddress> Socket<Address>
Sourcepub fn new(kind: c_int, protocol: c_int) -> Result<Self>where
Address: SpecificSocketAddress,
pub fn new(kind: c_int, protocol: c_int) -> Result<Self>where
Address: SpecificSocketAddress,
Create a new socket with the specified type and protocol.
The domain is taken from the Address type.
The created socket has the close-on-exec flag set.
The flag will be set atomically when the socket is created if the platform supports it.
See man socket for more information.
Sourcepub fn new_generic(domain: c_int, kind: c_int, protocol: c_int) -> Result<Self>
pub fn new_generic(domain: c_int, kind: c_int, protocol: c_int) -> Result<Self>
Create a new socket with the specified domain, type and protocol.
Unless you are working with generic socket addresses,
you should normally prefer Self::new.
The created socket has the close-on-exec flag set.
The flag will be set atomically when the socket is created if the platform supports it.
See man socket for more information.
Sourcepub fn pair(kind: c_int, protocol: c_int) -> Result<(Self, Self)>where
Address: SpecificSocketAddress,
pub fn pair(kind: c_int, protocol: c_int) -> Result<(Self, Self)>where
Address: SpecificSocketAddress,
Create a connected pair of socket with the specified type and protocol.
The domain is taken from the Address type.
The created sockets have the close-on-exec flag set.
The flag will be set atomically when the sockets are created if the platform supports it.
See man socketpair and man socket for more information.
Sourcepub fn pair_generic(
domain: c_int,
kind: c_int,
protocol: c_int,
) -> Result<(Self, Self)>
pub fn pair_generic( domain: c_int, kind: c_int, protocol: c_int, ) -> Result<(Self, Self)>
Create a connected pair of socket with the specified domain, type and protocol.
Unless you are working with generic socket addresses,
you should normally prefer Self::pair.
The created sockets have the close-on-exec flag set.
The flag will be set atomically when the sockets are created if the platform supports it.
See man socketpair and man socket for more information.
Sourcepub fn try_clone(&self) -> Result<Self>
pub fn try_clone(&self) -> Result<Self>
Try to clone the socket.
This is implemented by duplicating the file descriptor.
The returned Socket refers to the same kernel object.
The underlying file descriptor of the new socket will have the close-on-exec flag set.
If the platform supports it, the flag will be set atomically when the file descriptor is duplicated.
Sourcepub unsafe fn from_raw_fd(fd: RawFd) -> Self
pub unsafe fn from_raw_fd(fd: RawFd) -> Self
Wrap a raw file descriptor in a Socket.
This function sets no flags or options on the file descriptor or socket.
It is your own responsibility to make sure the close-on-exec flag is already set,
and that the SO_NOSIGPIPE option is set on Apple platforms.
Sourcepub fn as_raw_fd(&self) -> RawFd
pub fn as_raw_fd(&self) -> RawFd
Get the raw file descriptor.
This function does not release ownership of the underlying file descriptor.
The file descriptor will still be closed when the FileDesc is dropped.
Sourcepub fn into_raw_fd(self) -> RawFd
pub fn into_raw_fd(self) -> RawFd
Release and get the raw file descriptor.
This function releases ownership of the underlying file descriptor. The file descriptor will not be closed.
Sourcepub fn set_nonblocking(&self, non_blocking: bool) -> Result<()>
pub fn set_nonblocking(&self, non_blocking: bool) -> Result<()>
Put the socket in blocking or non-blocking mode.
Sourcepub fn get_nonblocking(&self) -> Result<bool>
pub fn get_nonblocking(&self) -> Result<bool>
Check if the socket in blocking or non-blocking mode.
Sourcepub fn take_error(&self) -> Result<Option<Error>>
pub fn take_error(&self) -> Result<Option<Error>>
Gets the value of the SO_ERROR option on this socket.
This will retrieve the stored error in the underlying socket, clearing the field in the process. This can be useful for checking errors between calls.
Sourcepub fn local_addr(&self) -> Result<Address>
pub fn local_addr(&self) -> Result<Address>
Get the local address the socket is bound to.
Sourcepub fn connect(&self, address: &Address) -> Result<()>
pub fn connect(&self, address: &Address) -> Result<()>
Connect the socket to a remote address.
It depends on the exact socket type what it means to connect the socket.
See man connect for more information.
Sourcepub fn bind(&self, address: &Address) -> Result<()>
pub fn bind(&self, address: &Address) -> Result<()>
Bind the socket to a local address.
It depends on the exact socket type what it means to bind the socket.
See man bind for more information.
Sourcepub fn listen(&self, backlog: c_int) -> Result<()>
pub fn listen(&self, backlog: c_int) -> Result<()>
Put the socket in listening mode, ready to accept connections.
Once the socket is in listening mode,
new connections can be accepted with accept().
Not all socket types can be put into listening mode.
See man listen for more information.
Sourcepub fn accept(&self) -> Result<(Self, Address)>
pub fn accept(&self) -> Result<(Self, Address)>
Accept a new connection on the socket.
The socket must have been put in listening mode
with a call to listen().
Not all socket types can be put into listening mode or accept connections.
See man listen for more information.
Sourcepub fn send(&self, data: &[u8], flags: c_int) -> Result<usize>
pub fn send(&self, data: &[u8], flags: c_int) -> Result<usize>
Send data over the socket to the connected peer.
Returns the number of transferred bytes, or an error.
See man send for more information.
Sourcepub fn send_to(
&self,
data: &[u8],
address: &Address,
flags: c_int,
) -> Result<usize>
pub fn send_to( &self, data: &[u8], address: &Address, flags: c_int, ) -> Result<usize>
Send data over the socket to the specified address.
This function is only valid for connectionless protocols such as UDP or unix datagram sockets.
Returns the number of transferred bytes, or an error.
See man sendto for more information.
Sourcepub fn send_msg(
&self,
data: &[IoSlice<'_>],
cdata: Option<&[u8]>,
flags: c_int,
) -> Result<usize>
pub fn send_msg( &self, data: &[IoSlice<'_>], cdata: Option<&[u8]>, flags: c_int, ) -> Result<usize>
Send a message over the socket to the connected peer.
Returns the number of transferred bytes, or an error.
See man sendmsg for more information.
Sourcepub fn send_msg_to(
&self,
address: &Address,
data: &[IoSlice<'_>],
cdata: Option<&[u8]>,
flags: c_int,
) -> Result<usize>
pub fn send_msg_to( &self, address: &Address, data: &[IoSlice<'_>], cdata: Option<&[u8]>, flags: c_int, ) -> Result<usize>
Send a message over the socket to the specified address.
This function is only valid for connectionless protocols such as UDP or unix datagram sockets.
Returns the number of transferred bytes, or an error.
See man sendmsg for more information.
Sourcepub fn recv(&self, buffer: &mut [u8], flags: c_int) -> Result<usize>
pub fn recv(&self, buffer: &mut [u8], flags: c_int) -> Result<usize>
Receive a data on the socket from the connected peer.
Returns the number of transferred bytes, or an error.
See man recv for more information.
Sourcepub fn recv_from(
&self,
buffer: &mut [u8],
flags: c_int,
) -> Result<(Address, usize)>
pub fn recv_from( &self, buffer: &mut [u8], flags: c_int, ) -> Result<(Address, usize)>
Receive a data on the socket.
Returns the address of the sender and the number of transferred bytes, or an error.
See man recvfrom for more information.
Sourcepub fn recv_msg(
&self,
data: &[IoSliceMut<'_>],
cdata: &mut SocketAncillary<'_>,
flags: c_int,
) -> Result<(usize, c_int)>
pub fn recv_msg( &self, data: &[IoSliceMut<'_>], cdata: &mut SocketAncillary<'_>, flags: c_int, ) -> Result<(usize, c_int)>
Receive a message on the socket from the connected peer.
If the call succeeds, the function returns a tuple with:
- the number of transferred bytes
- the number of transferred control message bytes
- the reception flags
See man recvmsg for more information.
Sourcepub fn recv_msg_from(
&self,
data: &[IoSliceMut<'_>],
cdata: &mut SocketAncillary<'_>,
flags: c_int,
) -> Result<(Address, usize, c_int)>
pub fn recv_msg_from( &self, data: &[IoSliceMut<'_>], cdata: &mut SocketAncillary<'_>, flags: c_int, ) -> Result<(Address, usize, c_int)>
Receive a message on the socket from any address.
If the call succeeds, the function returns a tuple with:
- the address of the sender
- the number of transferred bytes
- the number of transferred control message bytes
- the reception flags
See man recvmsg for more information.
Trait Implementations§
Source§impl<Address: AsSocketAddress> AsRawFd for &Socket<Address>
impl<Address: AsSocketAddress> AsRawFd for &Socket<Address>
Source§impl<Address: AsSocketAddress> AsRawFd for Socket<Address>
impl<Address: AsSocketAddress> AsRawFd for Socket<Address>
Source§impl<Address: AsSocketAddress> FromRawFd for Socket<Address>
impl<Address: AsSocketAddress> FromRawFd for Socket<Address>
Source§unsafe fn from_raw_fd(fd: RawFd) -> Self
unsafe fn from_raw_fd(fd: RawFd) -> Self
Self from the given raw file
descriptor. Read more