From dd4678ff0e6c9b9a51a71a1942cbe6c83ec9c6b6 Mon Sep 17 00:00:00 2001 From: yufeng <321353225@qq.com> Date: Mon, 6 Feb 2023 19:22:44 +0800 Subject: [PATCH] Add a udp program and a ping test script --- ping.py | 18 ++++++++++++++++++ user/src/bin/udp.rs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 ping.py create mode 100644 user/src/bin/udp.rs diff --git a/ping.py b/ping.py new file mode 100644 index 0000000..c68fc71 --- /dev/null +++ b/ping.py @@ -0,0 +1,18 @@ +import socket +import sys +import time + +sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) +addr = ('localhost', 26099) +sock.bind(addr) + + +print("pinging...", file=sys.stderr) +while True: + buf, raddr = sock.recvfrom(4096) + print("receive: " + buf.decode("utf-8")) + buf = "this is a ping to port 6200!".encode('utf-8') + sock.sendto(buf, ("127.0.0.1", 6200)) + buf = "this is a ping to reply!".encode('utf-8') + sock.sendto(buf, raddr) + time.sleep(1) diff --git a/user/src/bin/udp.rs b/user/src/bin/udp.rs new file mode 100644 index 0000000..dda6005 --- /dev/null +++ b/user/src/bin/udp.rs @@ -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 +} \ No newline at end of file