Skip to content

Commit e3b1282

Browse files
committed
Fix IP example
1 parent 477cd12 commit e3b1282

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

examples/ip.rs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
//! For build and run instructions, see README.md
55
//!
6-
//! This example starts a TCP listening server that should transmit `Hello` to
7-
//! any connecting client, and then close the connection.
6+
//! This example starts a TCP listening server at the address 10.0.0.1/24, on port 80, that
7+
//! should transmit `Hello` to any connecting client, and then close the connection.
88
99
use defmt_rtt as _;
1010
use panic_probe as _;
@@ -78,33 +78,40 @@ fn main() -> ! {
7878
);
7979
let server_handle = iface.add_socket(server_socket);
8080

81-
defmt::info!("Ready, listening at {}", ip_addr);
8281
loop {
8382
let time: u64 = cortex_m::interrupt::free(|cs| *TIME.borrow(cs).borrow());
8483
cortex_m::interrupt::free(|cs| {
8584
let mut eth_pending = ETH_PENDING.borrow(cs).borrow_mut();
8685
*eth_pending = false;
8786
});
88-
match iface.poll(Instant::from_millis(time as i64)) {
89-
Ok(true) => {
90-
let socket = iface.get_socket::<TcpSocket>(server_handle);
91-
if !socket.is_open() {
92-
if let Err(e) = socket.listen(80) {
93-
defmt::error!("TCP listen error: {:?}", e)
94-
}
95-
}
9687

97-
if socket.can_send() {
98-
if let Err(e) = socket.send_slice(b"hello\n").map(|_| socket.close()) {
99-
defmt::info!("TCP send error: {:?}", e);
88+
iface.poll(Instant::from_millis(time as i64)).ok();
89+
90+
let socket = iface.get_socket::<TcpSocket>(server_handle);
91+
92+
if !socket.is_listening() && !socket.is_open() {
93+
socket.abort();
94+
if let Err(e) = socket.listen(80) {
95+
defmt::error!("TCP listen error: {:?}", e)
96+
} else {
97+
defmt::info!("Listening at {}:80...", ip_addr);
98+
}
99+
} else {
100+
match socket.send_slice(b"hello\n") {
101+
Ok(_) => {
102+
while iface.get_socket::<TcpSocket>(server_handle).send_queue() != 0 {
103+
// Poll to get the message out of the door
104+
iface.poll(Instant::from_millis(time as i64 + 1)).ok();
100105
}
106+
107+
// Abort the connection
108+
let socket = iface.get_socket::<TcpSocket>(server_handle);
109+
socket.abort();
110+
defmt::info!("Transmitted hello! Closing socket...");
111+
112+
iface.poll(Instant::from_millis(time as i64 + 1)).ok();
101113
}
102-
}
103-
Ok(false) => {}
104-
Err(e) =>
105-
// Ignore malformed packets
106-
{
107-
defmt::info!("Error: {:?}", e);
114+
Err(_) => {}
108115
}
109116
}
110117
}

0 commit comments

Comments
 (0)