An IRC client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.2 KiB

extern crate tokio;
use crate::tokio::io::AsyncWriteExt;
use std::error::Error;
use std::io;
use tokio::net::TcpStream;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let mut stream = TcpStream::connect("irc.freenode.net:6667").await?;
stream.writable().await?;
stream.write(b"NICK garrit\r\n").await?;
stream.write(b"USER garrit 8 x : garrit").await?;
stream.write(b"JOIN #antimony").await?;
loop {
// Wait for the socket to be readable
stream.readable().await?;
// Creating the buffer **after** the `await` prevents it from
// being stored in the async task.
let mut buf = [0; 8];
// Try to read data, this may still fail with `WouldBlock`
// if the readiness event is a false positive.
match stream.try_read(&mut buf) {
Ok(0) => break,
Ok(n) => {
let text = std::str::from_utf8(&buf)?;
print!("{}", text);
}
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
continue;
}
Err(e) => {
return Err(e.into());
}
}
}
Ok(())
3 years ago
}