Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot continue listen! #1758

Closed
Wuh12138 opened this issue Feb 9, 2024 · 2 comments
Closed

Cannot continue listen! #1758

Wuh12138 opened this issue Feb 9, 2024 · 2 comments

Comments

@Wuh12138
Copy link

Wuh12138 commented Feb 9, 2024

I'm new,poll not work!

First connect normally,but second is odd,
then resume,
python console log success,but actually rust code is not go into correspond code block.
If I use std::net::TcpListen with block listen, it's work as expect!

cargo 1.74.1 (ecb9851af 2023-10-18)
rustc 1.74.1 (a28077b28 2023-12-04)


[dependencies]
mio = {version="0.8",features = ["os-poll", "net"]}

win11,(22621,3007)
use mio::net::{TcpListener, TcpStream};

const SERVER: mio::Token = mio::Token(0);
fn main() {
    let mut listener = TcpListener::bind("127.0.0.1:8080".parse().unwrap()).unwrap();
    let mut poll = mio::Poll::new().unwrap();
    let mut events = mio::Events::with_capacity(1024);

    poll.registry()
        .register(&mut listener, SERVER, mio::Interest::READABLE)
        .unwrap();

    loop {
        poll.poll(&mut events, None).unwrap();
        for event in events.iter() {
            match event.token() {
                SERVER => {
                    let (mut stream, addr) = listener.accept().unwrap();
                    println!("Accepted connection from: {}", addr);
                }
                _ => unreachable!(),
            }
        }
    }
}

import socket

tcp_conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_conn.connect(('127.0.0.1', 8080))

while True:
    data=input('Enter data to send: ')
    if not data:
        break
    tcp_conn.send(data.encode())
    data = tcp_conn.recv(1024)
    if not data:
        break
    print('Received:', data.decode())
@Thomasdezeeuw
Copy link
Collaborator

You need to put a loop around accept, please read the docs: https://docs.rs/mio/latest/mio/struct.Poll.html#draining-readiness.

@Wuh12138
Copy link
Author

Wuh12138 commented Feb 9, 2024

You need to put a loop around accept, please read the docs: https://docs.rs/mio/latest/mio/struct.Poll.html#draining-readiness.

Thank you very much! It's work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants