socket example but haven't test run

This commit is contained in:
Samuka007 2025-04-21 21:40:38 +08:00
parent a34468c6c1
commit 48ab5e9b25

View File

@ -1,10 +1,9 @@
use std::sync::Arc; use std::sync::Arc;
use berkeley_socket::{ use berkeley_socket::{
driver::{irq::start_network_polling_thread, tap::TapDevice}, driver::{irq::start_network_polling_thread, tap::TapDevice}, interface::tap::TapIface, posix::SOCK, socket::{endpoint::Endpoint, inet::{common::NET_DEVICES, syscall::Inet}, Family}
interface::tap::TapIface,
socket::inet::common::NET_DEVICES,
}; };
use smoltcp::wire::{IpAddress, IpEndpoint};
use spin::Mutex; use spin::Mutex;
fn main() { fn main() {
@ -13,4 +12,15 @@ fn main() {
NET_DEVICES.write().insert(0, iface); NET_DEVICES.write().insert(0, iface);
let _ = start_network_polling_thread(); let _ = start_network_polling_thread();
// TODO: add socket tests // TODO: add socket tests
let socket = Inet::socket(SOCK::Datagram, 0).unwrap();
socket.bind(Endpoint::Ip(
IpEndpoint::new(
IpAddress::v4(192, 168, 199, 1),
1234,
)
)).unwrap();
let mut buffer = [0u8; 1024];
socket.read(&mut buffer).unwrap();
socket.write(&buffer).unwrap();
} }