Add a udp program and a ping test script

This commit is contained in:
yufeng 2023-02-06 19:22:44 +08:00 committed by Yu Chen
parent 2e81c8960c
commit dd4678ff0e
2 changed files with 63 additions and 0 deletions

45
user/src/bin/udp.rs Normal file
View file

@ -0,0 +1,45 @@
#![no_std]
#![no_main]
use alloc::string::String;
use user_lib::{connect, write, read};
#[macro_use]
extern crate user_lib;
#[macro_use]
extern crate alloc;
#[no_mangle]
pub fn main() -> i32 {
println!("udp test open!");
let udp_fd = connect(10 << 24 | 0 << 16 | 2 << 8 | 2, 2001, 26099);
if udp_fd < 0 {
println!("failed to create udp connection.");
return -1;
}
let buf = "Hello rCoreOS user program!";
println!("send <{}>", buf);
write(udp_fd as usize, buf.as_bytes());
println!("udp send done, waiting for reply.");
let mut buf = vec![0u8; 1024];
let len = read(udp_fd as usize, &mut buf);
if len < 0 {
println!("can't receive udp packet");
return -1;
}
let recv_str = String::from_utf8_lossy(&buf[..len as usize]);
println!("receive reply <{}>", recv_str);
0
}