try_sock_opt.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. use wasmedge_wasi_socket::{
  2. socket::{AddressFamily, Socket, SocketType},
  3. ToSocketAddrs,
  4. };
  5. fn main() {
  6. let s = Socket::new(AddressFamily::Inet4, SocketType::Stream).unwrap();
  7. let device = s.device().unwrap();
  8. assert!(device.is_none());
  9. s.bind_device(Some(b"lo")).unwrap();
  10. let device = s.device().unwrap();
  11. assert!(device.is_some());
  12. assert_eq!(device.unwrap(), b"lo");
  13. let addr = "8.8.8.8:53".to_socket_addrs().unwrap().next().unwrap();
  14. let recv_timeout = s.get_recv_timeout().unwrap();
  15. println!("recv_timeout {:?}", recv_timeout);
  16. let send_timeout = s.get_send_timeout().unwrap();
  17. println!("send_timeout {:?}", send_timeout);
  18. let snd_timeout = std::time::Duration::from_secs(1);
  19. let rcv_timeout = std::time::Duration::from_secs(1);
  20. s.set_recv_timeout(Some(snd_timeout)).unwrap();
  21. s.set_send_timeout(Some(rcv_timeout)).unwrap();
  22. let recv_timeout = s.get_recv_timeout().unwrap();
  23. println!("recv_timeout {:?}", recv_timeout);
  24. assert_eq!(recv_timeout, Some(rcv_timeout));
  25. let send_timeout = s.get_send_timeout().unwrap();
  26. println!("send_timeout {:?}", send_timeout);
  27. assert_eq!(send_timeout, Some(snd_timeout));
  28. println!("start connect {addr}");
  29. assert!(s.connect(&addr).is_err());
  30. }