Demo entry 6733601

test

   

Submitted by anonymous on Apr 17, 2018 at 23:12
Language: Rust. Code size: 1.1 kB.

extern crate encoding;
use std::io::prelude::*;
use std::io::{BufReader, BufWriter};
use std::env;
use std::net::{Shutdown, TcpStream};

use encoding::{EncoderTrap, Encoding};
use encoding::all::ASCII;

fn main() {
    let socket = TcpStream::connect("127.0.0.1:6600").unwrap();
    let mut reader = BufReader::new(&socket);
    let mut writer = BufWriter::new(&socket);
    let cmd = env::args().nth(1).unwrap();

    let mut response = String::new();
    reader.read_line(&mut response);
    println!("[connect] response='{}' ({})", response, response.len());

    let mut command_bytes = ASCII.encode(&cmd, EncoderTrap::Strict).unwrap();
    command_bytes.push('\r' as u8);
    command_bytes.push('\n' as u8);
    writer.write(&command_bytes).unwrap();
    writer.flush().unwrap();

    response = String::new();
    let mut line = String::new();
    loop {
        line = String::new();
        reader.read_line(&mut line).unwrap();
        response.push_str(&line);
        if line == "OK\n" {
            break;
        }
    }
    println!("[cmd] response='{}' ({})", response, response.len());
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).