package cn.redis;
import redis.clients.jedis.Jedis;
public class Test {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost");
jedis.set("myname", "LQ");
System.out.println(jedis.get("myname"));
}
}
1.3 redis-cli端查看
2. 模拟redis服务端 – HackSockerServer
2.1 编码
package cn.redis.hack;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 伪装 redis 的服务端
*
* */
public class HackSockerServer {
public static void main(String[] args) throws IOException {
System.out.println("HackSockerServer已启动.....");
ServerSocket serverSocket = new ServerSocket(6379);
//接收数据
Socket accept = serverSocket.accept();
byte[] buffer = new byte[1024];
accept.getInputStream().read(buffer);
System.out.println(new String(buffer));
}
}
2.2 Test 测试类
package cn.redis;
import redis.clients.jedis.Jedis;
public class Test {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost");
jedis.set("myname", "LQ");
System.out.println(jedis.get("myname"));
}
}
分别运行HackSockerServer和Test
结果
HackSockerServer已启动.....
*3
$3
SET
$6
myname
$2
LQ
官网说明 https://redis.io/topics/protocol
For Simple Strings the first byte of the reply is "+" -->单字符
For Errors the first byte of the reply is "-" -->错误
For Integers the first byte of the reply is ":" -->数字
For Bulk Strings the first byte of the reply is "$" -->字符串
For Arrays the first byte of the reply is "*" -->数组
Simple Strings are encoded in the following way: a plus character, followed by a string that cannot contain a CR or LF character (no newlines are allowed), terminated by CRLF (that is "\r\n").