MCPcopy Create free account
hub / github.com/bytecodealliance/rustix / server

Function server

tests/net/v4.rs:18–43  ·  view source on GitHub ↗
(ready: Arc<(Mutex<u16>, Condvar)>)

Source from the content-addressed store, hash-verified

16const BUFFER_SIZE: usize = 20;
17
18fn server(ready: Arc<(Mutex<u16>, Condvar)>) {
19 let connection_socket = socket(AddressFamily::INET, SocketType::STREAM, None).unwrap();
20
21 let name = SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 0);
22 bind(&connection_socket, &name).unwrap();
23
24 let who = getsockname(&connection_socket).unwrap();
25 let who = SocketAddrV4::try_from(who).unwrap();
26
27 listen(&connection_socket, 1).unwrap();
28
29 {
30 let (lock, cvar) = &*ready;
31 let mut port = lock.lock().unwrap();
32 *port = who.port();
33 cvar.notify_all();
34 }
35
36 let mut buffer = vec![0; BUFFER_SIZE];
37 let data_socket = accept(&connection_socket).unwrap();
38 let (nread, actual) = recv(&data_socket, &mut buffer, RecvFlags::empty()).unwrap();
39 assert_eq!(String::from_utf8_lossy(&buffer[..nread]), "hello, world");
40 assert_eq!(actual, nread);
41
42 send(&data_socket, b"goodnight, moon", SendFlags::empty()).unwrap();
43}
44
45fn client(ready: Arc<(Mutex<u16>, Condvar)>) {
46 let port = {

Callers 3

test_v4Function · 0.70
test_v4_msgFunction · 0.70
test_v4_sendmmsgFunction · 0.70

Calls 9

socketFunction · 0.50
bindFunction · 0.50
getsocknameFunction · 0.50
listenFunction · 0.50
acceptFunction · 0.50
recvFunction · 0.50
sendFunction · 0.50
recvmsgFunction · 0.50
sendmsgFunction · 0.50

Tested by 3

test_v4Function · 0.56
test_v4_msgFunction · 0.56
test_v4_sendmmsgFunction · 0.56