udp_socket.rs 567 B

123456789101112131415161718
  1. use wasmedge_wasi_socket::UdpSocket;
  2. fn main() -> std::io::Result<()> {
  3. let socket = UdpSocket::bind("0.0.0.0:3000").unwrap();
  4. let port = std::env::var("PORT").unwrap_or("3000".to_string());
  5. let addr = format!("127.0.0.1:{}", port);
  6. socket.send_to(b"hello", &addr).unwrap();
  7. let mut buf = [0; 128];
  8. let (size, addr) = socket.recv_from(&mut buf).unwrap();
  9. match std::str::from_utf8(&buf[..size]) {
  10. Ok(s) => Ok(println!("{} {}", s, addr)),
  11. Err(e) => Err(std::io::Error::new(std::io::ErrorKind::InvalidData, e)),
  12. }
  13. }