pub struct SctpStream(/* private fields */);
Expand description
One-to-one SCTP connected stream which behaves like a TCP stream.
A SctpStream
can be obtained either actively by connecting to a SCTP endpoint with the
connect
constructor, or passively from a SctpListener
which accepts new connections
Implementations§
Source§impl SctpStream
impl SctpStream
Sourcepub fn connect<A: ToSocketAddrs>(address: A) -> Result<SctpStream>
pub fn connect<A: ToSocketAddrs>(address: A) -> Result<SctpStream>
Create a new stream by connecting it to a remote endpoint
Examples found in repository?
6fn main() {
7 // Create a new one-to-one stream
8 match SctpStream::connect("127.0.0.1:3868") {
9 // match SctpStream::connectx(&["10.0.2.15:3868", "127.0.0.1:3868"]) {
10 Err(e) => println!("{:?}", e.kind()),
11 Ok(mut peer) => {
12 // Set SCTP no delay
13 println!("{}", peer.has_nodelay().unwrap());
14 peer.set_nodelay(true).unwrap();
15 println!("{}", peer.has_nodelay().unwrap());
16
17 // Set socket send buffer size
18 let oldsize = peer.get_buffer_size(SoDirection::Send).unwrap();
19 peer.set_buffer_size(SoDirection::Send, 4096).unwrap();
20 println!(
21 "Set send buffer size to {} (was : {})",
22 peer.get_buffer_size(SoDirection::Send).unwrap(),
23 oldsize
24 );
25
26 println!("Setting read timeout to 10 s");
27 peer.set_timeout(SoDirection::Receive, 10).unwrap();
28
29 // Write a message using the io::Write trait
30 peer.write_all("foo bar\n".as_bytes()).unwrap();
31 // Write a message on stream 6
32 peer.sendmsg("foo bar again\n".as_bytes(), 6).unwrap();
33 let mut data = [0u8; 1024];
34 // Read data using the io::Read trait
35 peer.read_exact(&mut data).unwrap();
36 // Read data using SCTP advanced feature, and retrieve the stream id
37 // on which data were received
38 let (size, stream) = peer.recvmsg(&mut data).unwrap();
39 println!("Received {} bytes on stream {}", size, stream);
40 }
41 }
42}
Sourcepub fn connectx<A: ToSocketAddrs>(addresses: &[A]) -> Result<SctpStream>
pub fn connectx<A: ToSocketAddrs>(addresses: &[A]) -> Result<SctpStream>
Create a new stream by connecting it to a remote endpoint having multiple addresses
Sourcepub fn sendmsg(&self, msg: &[u8], stream: u16) -> Result<usize>
pub fn sendmsg(&self, msg: &[u8], stream: u16) -> Result<usize>
Send bytes on the specified SCTP stream. On success, returns the quantity of bytes read
Examples found in repository?
4fn main() {
5 match SctpListener::bind("0.0.0.0:3868") {
6 // match SctpListener::bindx(&["10.0.2.15:3868", "127.0.0.1:3868"]) {
7 Ok(serv) => {
8 println!("bound to {:?}", serv.local_addrs().unwrap());
9 // serv.set_timeout(5).unwrap();
10 match serv.accept() {
11 Err(e) => println!("{:?}", e.kind()),
12 Ok((peer, _)) => {
13 println!(
14 "connection from {:?} on {:?}",
15 peer.peer_addrs().unwrap(),
16 peer.local_addrs().unwrap()
17 );
18 // Send message on stream 6
19 peer.sendmsg("foobar\n".as_bytes(), 6).unwrap();
20 let mut reply = [0u8; 1024];
21 let (len, stream) = peer.recvmsg(&mut reply).unwrap();
22 println!("Received {} bytes on stream {}", len, stream);
23 }
24 };
25 }
26 Err(e) => panic!("{:?}", e.kind()),
27 }
28}
More examples
6fn main() {
7 // Create a new one-to-one stream
8 match SctpStream::connect("127.0.0.1:3868") {
9 // match SctpStream::connectx(&["10.0.2.15:3868", "127.0.0.1:3868"]) {
10 Err(e) => println!("{:?}", e.kind()),
11 Ok(mut peer) => {
12 // Set SCTP no delay
13 println!("{}", peer.has_nodelay().unwrap());
14 peer.set_nodelay(true).unwrap();
15 println!("{}", peer.has_nodelay().unwrap());
16
17 // Set socket send buffer size
18 let oldsize = peer.get_buffer_size(SoDirection::Send).unwrap();
19 peer.set_buffer_size(SoDirection::Send, 4096).unwrap();
20 println!(
21 "Set send buffer size to {} (was : {})",
22 peer.get_buffer_size(SoDirection::Send).unwrap(),
23 oldsize
24 );
25
26 println!("Setting read timeout to 10 s");
27 peer.set_timeout(SoDirection::Receive, 10).unwrap();
28
29 // Write a message using the io::Write trait
30 peer.write_all("foo bar\n".as_bytes()).unwrap();
31 // Write a message on stream 6
32 peer.sendmsg("foo bar again\n".as_bytes(), 6).unwrap();
33 let mut data = [0u8; 1024];
34 // Read data using the io::Read trait
35 peer.read_exact(&mut data).unwrap();
36 // Read data using SCTP advanced feature, and retrieve the stream id
37 // on which data were received
38 let (size, stream) = peer.recvmsg(&mut data).unwrap();
39 println!("Received {} bytes on stream {}", size, stream);
40 }
41 }
42}
Sourcepub fn sendmsg_ppid(&self, msg: &[u8], ppid: u32, stream: u16) -> Result<usize>
pub fn sendmsg_ppid(&self, msg: &[u8], ppid: u32, stream: u16) -> Result<usize>
Send bytes on the specified SCTP stream. On success, returns the quantity of bytes read
Sourcepub fn recvmsg(&self, msg: &mut [u8]) -> Result<(usize, u16)>
pub fn recvmsg(&self, msg: &mut [u8]) -> Result<(usize, u16)>
Read bytes. On success, return a tuple with the quantity of bytes received and the stream they were recived on
Examples found in repository?
4fn main() {
5 match SctpListener::bind("0.0.0.0:3868") {
6 // match SctpListener::bindx(&["10.0.2.15:3868", "127.0.0.1:3868"]) {
7 Ok(serv) => {
8 println!("bound to {:?}", serv.local_addrs().unwrap());
9 // serv.set_timeout(5).unwrap();
10 match serv.accept() {
11 Err(e) => println!("{:?}", e.kind()),
12 Ok((peer, _)) => {
13 println!(
14 "connection from {:?} on {:?}",
15 peer.peer_addrs().unwrap(),
16 peer.local_addrs().unwrap()
17 );
18 // Send message on stream 6
19 peer.sendmsg("foobar\n".as_bytes(), 6).unwrap();
20 let mut reply = [0u8; 1024];
21 let (len, stream) = peer.recvmsg(&mut reply).unwrap();
22 println!("Received {} bytes on stream {}", len, stream);
23 }
24 };
25 }
26 Err(e) => panic!("{:?}", e.kind()),
27 }
28}
More examples
6fn main() {
7 // Create a new one-to-one stream
8 match SctpStream::connect("127.0.0.1:3868") {
9 // match SctpStream::connectx(&["10.0.2.15:3868", "127.0.0.1:3868"]) {
10 Err(e) => println!("{:?}", e.kind()),
11 Ok(mut peer) => {
12 // Set SCTP no delay
13 println!("{}", peer.has_nodelay().unwrap());
14 peer.set_nodelay(true).unwrap();
15 println!("{}", peer.has_nodelay().unwrap());
16
17 // Set socket send buffer size
18 let oldsize = peer.get_buffer_size(SoDirection::Send).unwrap();
19 peer.set_buffer_size(SoDirection::Send, 4096).unwrap();
20 println!(
21 "Set send buffer size to {} (was : {})",
22 peer.get_buffer_size(SoDirection::Send).unwrap(),
23 oldsize
24 );
25
26 println!("Setting read timeout to 10 s");
27 peer.set_timeout(SoDirection::Receive, 10).unwrap();
28
29 // Write a message using the io::Write trait
30 peer.write_all("foo bar\n".as_bytes()).unwrap();
31 // Write a message on stream 6
32 peer.sendmsg("foo bar again\n".as_bytes(), 6).unwrap();
33 let mut data = [0u8; 1024];
34 // Read data using the io::Read trait
35 peer.read_exact(&mut data).unwrap();
36 // Read data using SCTP advanced feature, and retrieve the stream id
37 // on which data were received
38 let (size, stream) = peer.recvmsg(&mut data).unwrap();
39 println!("Received {} bytes on stream {}", size, stream);
40 }
41 }
42}
Sourcepub fn local_addrs(&self) -> Result<Vec<SocketAddr>>
pub fn local_addrs(&self) -> Result<Vec<SocketAddr>>
Return the list of local socket addresses for this stream
Examples found in repository?
4fn main() {
5 match SctpListener::bind("0.0.0.0:3868") {
6 // match SctpListener::bindx(&["10.0.2.15:3868", "127.0.0.1:3868"]) {
7 Ok(serv) => {
8 println!("bound to {:?}", serv.local_addrs().unwrap());
9 // serv.set_timeout(5).unwrap();
10 match serv.accept() {
11 Err(e) => println!("{:?}", e.kind()),
12 Ok((peer, _)) => {
13 println!(
14 "connection from {:?} on {:?}",
15 peer.peer_addrs().unwrap(),
16 peer.local_addrs().unwrap()
17 );
18 // Send message on stream 6
19 peer.sendmsg("foobar\n".as_bytes(), 6).unwrap();
20 let mut reply = [0u8; 1024];
21 let (len, stream) = peer.recvmsg(&mut reply).unwrap();
22 println!("Received {} bytes on stream {}", len, stream);
23 }
24 };
25 }
26 Err(e) => panic!("{:?}", e.kind()),
27 }
28}
Sourcepub fn peer_addrs(&self) -> Result<Vec<SocketAddr>>
pub fn peer_addrs(&self) -> Result<Vec<SocketAddr>>
Return the list of socket addresses for the peer this stream is connected to
Examples found in repository?
4fn main() {
5 match SctpListener::bind("0.0.0.0:3868") {
6 // match SctpListener::bindx(&["10.0.2.15:3868", "127.0.0.1:3868"]) {
7 Ok(serv) => {
8 println!("bound to {:?}", serv.local_addrs().unwrap());
9 // serv.set_timeout(5).unwrap();
10 match serv.accept() {
11 Err(e) => println!("{:?}", e.kind()),
12 Ok((peer, _)) => {
13 println!(
14 "connection from {:?} on {:?}",
15 peer.peer_addrs().unwrap(),
16 peer.local_addrs().unwrap()
17 );
18 // Send message on stream 6
19 peer.sendmsg("foobar\n".as_bytes(), 6).unwrap();
20 let mut reply = [0u8; 1024];
21 let (len, stream) = peer.recvmsg(&mut reply).unwrap();
22 println!("Received {} bytes on stream {}", len, stream);
23 }
24 };
25 }
26 Err(e) => panic!("{:?}", e.kind()),
27 }
28}
Sourcepub fn shutdown(&self, how: Shutdown) -> Result<()>
pub fn shutdown(&self, how: Shutdown) -> Result<()>
Shuts down the read, write, or both halves of this connection
Sourcepub fn set_nodelay(&self, nodelay: bool) -> Result<()>
pub fn set_nodelay(&self, nodelay: bool) -> Result<()>
Set or unset SCTP_NODELAY option
Examples found in repository?
6fn main() {
7 // Create a new one-to-one stream
8 match SctpStream::connect("127.0.0.1:3868") {
9 // match SctpStream::connectx(&["10.0.2.15:3868", "127.0.0.1:3868"]) {
10 Err(e) => println!("{:?}", e.kind()),
11 Ok(mut peer) => {
12 // Set SCTP no delay
13 println!("{}", peer.has_nodelay().unwrap());
14 peer.set_nodelay(true).unwrap();
15 println!("{}", peer.has_nodelay().unwrap());
16
17 // Set socket send buffer size
18 let oldsize = peer.get_buffer_size(SoDirection::Send).unwrap();
19 peer.set_buffer_size(SoDirection::Send, 4096).unwrap();
20 println!(
21 "Set send buffer size to {} (was : {})",
22 peer.get_buffer_size(SoDirection::Send).unwrap(),
23 oldsize
24 );
25
26 println!("Setting read timeout to 10 s");
27 peer.set_timeout(SoDirection::Receive, 10).unwrap();
28
29 // Write a message using the io::Write trait
30 peer.write_all("foo bar\n".as_bytes()).unwrap();
31 // Write a message on stream 6
32 peer.sendmsg("foo bar again\n".as_bytes(), 6).unwrap();
33 let mut data = [0u8; 1024];
34 // Read data using the io::Read trait
35 peer.read_exact(&mut data).unwrap();
36 // Read data using SCTP advanced feature, and retrieve the stream id
37 // on which data were received
38 let (size, stream) = peer.recvmsg(&mut data).unwrap();
39 println!("Received {} bytes on stream {}", size, stream);
40 }
41 }
42}
Sourcepub fn has_nodelay(&self) -> Result<bool>
pub fn has_nodelay(&self) -> Result<bool>
Verify if SCTP_NODELAY option is activated for this socket
Examples found in repository?
6fn main() {
7 // Create a new one-to-one stream
8 match SctpStream::connect("127.0.0.1:3868") {
9 // match SctpStream::connectx(&["10.0.2.15:3868", "127.0.0.1:3868"]) {
10 Err(e) => println!("{:?}", e.kind()),
11 Ok(mut peer) => {
12 // Set SCTP no delay
13 println!("{}", peer.has_nodelay().unwrap());
14 peer.set_nodelay(true).unwrap();
15 println!("{}", peer.has_nodelay().unwrap());
16
17 // Set socket send buffer size
18 let oldsize = peer.get_buffer_size(SoDirection::Send).unwrap();
19 peer.set_buffer_size(SoDirection::Send, 4096).unwrap();
20 println!(
21 "Set send buffer size to {} (was : {})",
22 peer.get_buffer_size(SoDirection::Send).unwrap(),
23 oldsize
24 );
25
26 println!("Setting read timeout to 10 s");
27 peer.set_timeout(SoDirection::Receive, 10).unwrap();
28
29 // Write a message using the io::Write trait
30 peer.write_all("foo bar\n".as_bytes()).unwrap();
31 // Write a message on stream 6
32 peer.sendmsg("foo bar again\n".as_bytes(), 6).unwrap();
33 let mut data = [0u8; 1024];
34 // Read data using the io::Read trait
35 peer.read_exact(&mut data).unwrap();
36 // Read data using SCTP advanced feature, and retrieve the stream id
37 // on which data were received
38 let (size, stream) = peer.recvmsg(&mut data).unwrap();
39 println!("Received {} bytes on stream {}", size, stream);
40 }
41 }
42}
Sourcepub fn set_buffer_size(&self, dir: SoDirection, size: usize) -> Result<()>
pub fn set_buffer_size(&self, dir: SoDirection, size: usize) -> Result<()>
Set the socket buffer size for the direction specified by dir
.
Linux systems will double the provided size
Examples found in repository?
6fn main() {
7 // Create a new one-to-one stream
8 match SctpStream::connect("127.0.0.1:3868") {
9 // match SctpStream::connectx(&["10.0.2.15:3868", "127.0.0.1:3868"]) {
10 Err(e) => println!("{:?}", e.kind()),
11 Ok(mut peer) => {
12 // Set SCTP no delay
13 println!("{}", peer.has_nodelay().unwrap());
14 peer.set_nodelay(true).unwrap();
15 println!("{}", peer.has_nodelay().unwrap());
16
17 // Set socket send buffer size
18 let oldsize = peer.get_buffer_size(SoDirection::Send).unwrap();
19 peer.set_buffer_size(SoDirection::Send, 4096).unwrap();
20 println!(
21 "Set send buffer size to {} (was : {})",
22 peer.get_buffer_size(SoDirection::Send).unwrap(),
23 oldsize
24 );
25
26 println!("Setting read timeout to 10 s");
27 peer.set_timeout(SoDirection::Receive, 10).unwrap();
28
29 // Write a message using the io::Write trait
30 peer.write_all("foo bar\n".as_bytes()).unwrap();
31 // Write a message on stream 6
32 peer.sendmsg("foo bar again\n".as_bytes(), 6).unwrap();
33 let mut data = [0u8; 1024];
34 // Read data using the io::Read trait
35 peer.read_exact(&mut data).unwrap();
36 // Read data using SCTP advanced feature, and retrieve the stream id
37 // on which data were received
38 let (size, stream) = peer.recvmsg(&mut data).unwrap();
39 println!("Received {} bytes on stream {}", size, stream);
40 }
41 }
42}
Sourcepub fn get_buffer_size(&self, dir: SoDirection) -> Result<usize>
pub fn get_buffer_size(&self, dir: SoDirection) -> Result<usize>
Get the socket buffer size for the direction specified by dir
Examples found in repository?
6fn main() {
7 // Create a new one-to-one stream
8 match SctpStream::connect("127.0.0.1:3868") {
9 // match SctpStream::connectx(&["10.0.2.15:3868", "127.0.0.1:3868"]) {
10 Err(e) => println!("{:?}", e.kind()),
11 Ok(mut peer) => {
12 // Set SCTP no delay
13 println!("{}", peer.has_nodelay().unwrap());
14 peer.set_nodelay(true).unwrap();
15 println!("{}", peer.has_nodelay().unwrap());
16
17 // Set socket send buffer size
18 let oldsize = peer.get_buffer_size(SoDirection::Send).unwrap();
19 peer.set_buffer_size(SoDirection::Send, 4096).unwrap();
20 println!(
21 "Set send buffer size to {} (was : {})",
22 peer.get_buffer_size(SoDirection::Send).unwrap(),
23 oldsize
24 );
25
26 println!("Setting read timeout to 10 s");
27 peer.set_timeout(SoDirection::Receive, 10).unwrap();
28
29 // Write a message using the io::Write trait
30 peer.write_all("foo bar\n".as_bytes()).unwrap();
31 // Write a message on stream 6
32 peer.sendmsg("foo bar again\n".as_bytes(), 6).unwrap();
33 let mut data = [0u8; 1024];
34 // Read data using the io::Read trait
35 peer.read_exact(&mut data).unwrap();
36 // Read data using SCTP advanced feature, and retrieve the stream id
37 // on which data were received
38 let (size, stream) = peer.recvmsg(&mut data).unwrap();
39 println!("Received {} bytes on stream {}", size, stream);
40 }
41 }
42}
Sourcepub fn set_timeout(&self, dir: SoDirection, timeout: i32) -> Result<()>
pub fn set_timeout(&self, dir: SoDirection, timeout: i32) -> Result<()>
Set timeout
in seconds for operation dir
(either receive or send)
Examples found in repository?
6fn main() {
7 // Create a new one-to-one stream
8 match SctpStream::connect("127.0.0.1:3868") {
9 // match SctpStream::connectx(&["10.0.2.15:3868", "127.0.0.1:3868"]) {
10 Err(e) => println!("{:?}", e.kind()),
11 Ok(mut peer) => {
12 // Set SCTP no delay
13 println!("{}", peer.has_nodelay().unwrap());
14 peer.set_nodelay(true).unwrap();
15 println!("{}", peer.has_nodelay().unwrap());
16
17 // Set socket send buffer size
18 let oldsize = peer.get_buffer_size(SoDirection::Send).unwrap();
19 peer.set_buffer_size(SoDirection::Send, 4096).unwrap();
20 println!(
21 "Set send buffer size to {} (was : {})",
22 peer.get_buffer_size(SoDirection::Send).unwrap(),
23 oldsize
24 );
25
26 println!("Setting read timeout to 10 s");
27 peer.set_timeout(SoDirection::Receive, 10).unwrap();
28
29 // Write a message using the io::Write trait
30 peer.write_all("foo bar\n".as_bytes()).unwrap();
31 // Write a message on stream 6
32 peer.sendmsg("foo bar again\n".as_bytes(), 6).unwrap();
33 let mut data = [0u8; 1024];
34 // Read data using the io::Read trait
35 peer.read_exact(&mut data).unwrap();
36 // Read data using SCTP advanced feature, and retrieve the stream id
37 // on which data were received
38 let (size, stream) = peer.recvmsg(&mut data).unwrap();
39 println!("Received {} bytes on stream {}", size, stream);
40 }
41 }
42}
Sourcepub fn try_clone(&self) -> Result<SctpStream>
pub fn try_clone(&self) -> Result<SctpStream>
Try to clone the SctpStream. On success, returns a new stream wrapping a new socket handler
Trait Implementations§
Source§impl AsRawFd for SctpStream
impl AsRawFd for SctpStream
Source§impl FromRawFd for SctpStream
impl FromRawFd for SctpStream
Source§unsafe fn from_raw_fd(fd: RawFd) -> SctpStream ⓘ
unsafe fn from_raw_fd(fd: RawFd) -> SctpStream ⓘ
Self
from the given raw file
descriptor. Read moreSource§impl Read for SctpStream
impl Read for SctpStream
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read moreSource§impl Write for SctpStream
impl Write for SctpStream
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)