Listening (perhaps) to TCP Connection

Hi everyone,

I am nearing completion of TRPL (Currently on chapter 20). I understood the first section well, and proceeded to recreate the example:

use std::net::TcpListener;

fn main() {
    let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
    println!("{:?}", listener);

    for stream in listener.incoming() {
        match stream {
            Ok(stream) => {
                println!("new client");
            }
            Err(e) => {
                println!("connection failed");
            }
        }
    }
}

I believe I am connecting to the server, because my program continues to run. However, nothing is printed to the terminal! :confused:

Output:

    Finished dev [unoptimized + debuginfo] target(s) in 1.42s
     Running `target/debug/hello`
TcpListener { addr: V4(127.0.0.1:7878), fd: 3 }

I have had trouble finding a solution in the documentation. Does anyone know why new client is not printing to the terminal?

Thanks,
Connor

How are you connecting to this server?

Yes, what do you mean by that?

The code you have presented is the server.

You will need to connect to it with a client to see anything happen.

Try running that code and then connecting to it with netcat or telnet.

Or connect to it with a client you write in Rust of course!

1 Like

Sorry, I did not previously understand that I needed to run the client in my browser. :man_facepalming:

But you don't need a browser to run that example client code.

Just run them from the command line. Server in one console window, client in another.

Actually I don't see how you can run that client in a browser.

netcat 127.0.0.1 7878
Or it might be nc for short.

It might be an interesting project to write a minimal netcat in rust :slight_smile: as well as connect to a server like above it can listen on a port. Two netcats can communicate back and forth if one connects to the other that is listening (I think)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.