So, I'm trying to connect to an IRC channel using TCP which seems to be working. But now I want to print the data received. I can make it print "Received {x} bytes", and I can see that I do receive data from the IRC. But I just don't know how to read it. This is what I have so far:
use std::io::{Read, Write};
use std::net::TcpStream;
fn main() {
let mut socket = TcpStream::connect("irc.chat.twitch.tv:6667").unwrap();
let result_auth = socket.write(b"PASS oauth:somelongtokenhere\r\n");
println!("{:?}", result_auth);
let result_nick = socket.write(b"NICK botname\r\n");
println!("{:?}", result_nick);
let result_chan = socket.write(b"JOIN #channel-to-join\r\n");
println!("{:?}", result_chan);
let mut line;
loop {
line = [0u8; 1024];
let result = socket.read(&mut line);
match result {
Ok(n) => println!("Received {} bytes", n),
_ => {},
}
}
}
You could also wrap your TcpStream in a BufReader, and then read each line with the lines iterator from the BufRead trait:
use std::io::{Write, BufReader, BufRead};
use std::net::TcpStream;
fn main() {
let mut socket = TcpStream::connect("irc.chat.twitch.tv:6667").unwrap();
let result_auth = socket.write(b"PASS oauth:somelongtokenhere\r\n");
println!("{:?}", result_auth);
let result_nick = socket.write(b"NICK botname\r\n");
println!("{:?}", result_nick);
let result_chan = socket.write(b"JOIN #channel-to-join\r\n");
println!("{:?}", result_chan);
let buffered = BufReader::new(socket);
for res in buffered.lines() {
match res {
Ok(line) => println!("Received line: {}", line),
_ => {},
}
}
}
If all you want is creating an IRC bot or some other kind of client, using the crate is going to be a lot easier than reimplementing everything for your project. On the other hand, if you're in for the challenge of doing everything manually, don't mind me and have fun