-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from arceos-os/std_support_socket2
add support for socket2
- Loading branch information
Showing
5 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "socket2-echoserver" | ||
version = "0.1.0" | ||
edition = "2021" | ||
authors = ["scpointer <[email protected]>"] | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
env_logger = { version = "0.9.3", default-features = false } | ||
socket2 = { git = "https://github.com/arceos-os/socket2.git", branch = "arceos", feature = "all" } | ||
|
||
# axstd = { path = "../../../ulib/axstd", features = ["net"], optional = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//! Configuration needed to run this test (in the root Makefile): | ||
//! A=apps/std/socket2-echoserver STD=y NET=y | ||
use socket2::{Domain, Socket, Type}; | ||
use std::io::Write; | ||
use std::mem::MaybeUninit; | ||
use std::net::SocketAddr; | ||
use std::str::from_utf8; | ||
|
||
const DATA: &[u8] = b"hello world"; | ||
const BUF_SIZE: usize = 4096; | ||
const ADDR: &str = "0.0.0.0:5555"; | ||
|
||
fn main() { | ||
env_logger::init(); | ||
test() | ||
} | ||
|
||
fn test() { | ||
let mut recv_buffer = [MaybeUninit::<u8>::new(0); BUF_SIZE]; | ||
let mut send_buffer = [0u8; BUF_SIZE]; | ||
|
||
let socket = Socket::new(Domain::IPV4, Type::STREAM, None).unwrap(); | ||
let addr: SocketAddr = ADDR.parse::<SocketAddr>().unwrap(); | ||
println!("addr {:#?}", addr); | ||
#[cfg(not(target_os = "arceos"))] | ||
let addr: socket2::SockAddr = addr.into(); | ||
socket.bind(&addr).unwrap(); | ||
socket.listen(128).unwrap(); | ||
|
||
println!("---------- socket2 echoserver ----------"); | ||
println!("type `nc {}` at another terminal:", ADDR); | ||
|
||
loop { | ||
let (mut connection, sockaddr) = loop { | ||
if let Ok(result) = socket.accept() { | ||
break result; | ||
} else { | ||
println!("user got a Err from accept, try again"); | ||
} | ||
}; | ||
#[cfg(target_os = "arceos")] | ||
println!("Accepted connection from: {}", sockaddr); | ||
#[cfg(not(target_os = "arceos"))] | ||
println!( | ||
"Accepted connection from: {}", | ||
sockaddr.as_socket().unwrap() | ||
); | ||
connection.write_all(DATA).unwrap(); | ||
|
||
loop { | ||
let n = connection.recv(&mut recv_buffer).unwrap(); | ||
if n == 0 { | ||
break; | ||
} | ||
for i in 0..n { | ||
send_buffer[i] = unsafe { recv_buffer[i].assume_init() }; | ||
} | ||
let received_data = &send_buffer[..n]; | ||
if let Ok(str_buf) = from_utf8(received_data) { | ||
println!("Received data({}B): {}", n, str_buf.trim_end()); | ||
connection.write_all(received_data).unwrap(); | ||
} else { | ||
println!("Received (none UTF-8) data: {:?}", received_data); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters