I am writing a BitTorrent client using asyncio and the Streams API. I'm trying to work at the highest level possible. I have the download part working (client opens a connection to peers provided by tracker and downloads all pieces). Client can also upload pieces to connected peers, if requested.
Big Question: how to listen to incoming connections from peers that join the swarm? The client running on a peer must be able to listen for incoming connections and upload pieces. I set up a server (following the documentation in 18.5.5.1). Question 1: I don't know what address to bind the server to. I get the error message "[Errno 10049] ... the requested address is not valid in its context" Question 2: I don't know how to integrate the server with the other tasks that run concurrently in the loop. (I assume this can be done with 1 loop). # tasks that open all connections tasks that are running until complete: coros = [client.connect_to_peer(peer) for peer in client.active_peers.values()] loop.run_until_complete(asyncio.wait(coros)) then... # tasks that get a single piece by getting blocks from multiple peers (if possible) coros = [client.get_piece(index, peer) for peer in peers] loop.run_until_complete(asyncio.wait(coros)) I can easily do the following (if I could answer Question 1), but it doesn't seem right (since my program should be listening to incoming connections, even before the client becomes a seeder.) After the client is a seeder, the program can start the server and the handle_leecher coroutine handles incoming connections and handshakes. Then the server runs forever and uploads pieces to the remote peers. Thanks. Leslie
