Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
redis/src/example/main.rs /
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
41 lines (33 sloc)
994 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extern mod redis; | |
| use std::rt::io::*; | |
| use std::rt::io::net::ip::*; | |
| use std::io::println; | |
| use std::str; | |
| fn send_command_to_redis(mut redis: net::tcp::TcpStream, command: &[u8]) -> ~str{ | |
| redis.write(command); | |
| let buf: &mut [u8] = [0]; | |
| let mut response : ~[u8] = ~[]; | |
| // 13 is \r | |
| while(buf[0] != 13) { | |
| match redis.read(buf) { | |
| Some(_i) => { | |
| response.push(buf[0]); | |
| } | |
| None => println("Couldn't read off of the socket.") | |
| } | |
| } | |
| // read off the \n | |
| redis.read(buf); | |
| str::from_utf8(response) | |
| } | |
| fn main() { | |
| let ip = Ipv4Addr(127, 0, 0, 1); | |
| let socket = net::tcp::TcpStream::connect(SocketAddr { ip: ip, port: 6379 }); | |
| match socket { | |
| Some(redis) => { | |
| let response = send_command_to_redis(redis, bytes!("*3\r\n$3\r\nSET\r\n$5\r\nmykey\r\n$7\r\nmyvalue\r\n")); | |
| println(response); | |
| }, | |
| None => println("Couldn't connect to a socket") | |
| } | |
| } |