blob: 1232c9f5761b4178bba43ffb9caf6c03549ef091 [file] [log] [blame]
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use std::io::{Read as _, Write as _};
use tracing::info;
const BUS_NAME: &str = "test-bus";
const SERVER_NAME: &str = "server";
const REQUEST: &str = "hello from client";
const RESPONSE: &str = "hello from server";
async fn main() {
let listener = std::net::TcpListener::bind("").expect("bind to address");
info!("waiting for connections...");
// Let the client know that we are listening for incoming connections.
let _bus = netemul_sync::Bus::subscribe(BUS_NAME, SERVER_NAME).expect("subscribe to bus");
let (mut stream, remote) = listener.accept().expect("accept incoming connection");
info!("accepted connection from {}", remote);
let mut buffer = [0; 512];
let read = buffer).expect("read from socket");
let request = String::from_utf8_lossy(&buffer[]);
assert_eq!(request, REQUEST, "got unexpected request from client: {}", request);
info!("got request: '{}'", request);
let response = RESPONSE.as_bytes();
assert_eq!(stream.write(response).expect("write to socket"), response.len());
stream.flush().expect("flush stream");