Socket应用之模拟浏览器,自定义jedis

1 socket模拟浏览器

/**
 * tcp 模拟 浏览器
 * Created by Administrator on 2018/8/26.
 */
public class TcpClient {

    public static void main(String[] args) throws IOException {

        Socket socket = new Socket("127.0.0.1", 80);

        // 拼接请求报文
        
        // 请求行
        String request_line = "GET /servlet/helloServlet HTTP/1.1\r\n";
       
        // 请求头
        String request_head = "Host:\r\n";
        String request = request_line + request_head + "\r\n";

        // 发送请求
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(request.getBytes());

        // 获取结果
        InputStream inputStream = socket.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(inputStream);

        byte[] buf = new byte[1024];
        int len;
        while (-1 != (len = bis.read(buf))) {
            System.out.println(new String(buf, 0, len));
        }

        outputStream.close();
        inputStream.close();
        socket.close();
    }
}

2. 自实现 Jedis

/**
 * 自定义 Jedis (jedis 通过 socket 与 redis 通信)
 * Created by Administrator on 2018/8/26.
 */
public class MyJedis {

    private Socket socket;

    public MyJedis(String host, int port) {

        try {
            socket = new Socket(host, port);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // RESP redis序列化协议
    public String set(final String key, String value) throws IOException {

        StringBuilder line = new StringBuilder();
        line.append("*3").append("\r\n");
        line.append("$3").append("\r\n");
        line.append("SET").append("\r\n");
        line.append("$").append(key.length()).append("\r\n");
        line.append(key).append("\r\n");
        line.append("$").append(value.length()).append("\r\n");
        line.append(value).append("\r\n");

        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(line.toString().getBytes());

        InputStream inputStream = socket.getInputStream();
        byte[] buf = new byte[1024];
        inputStream.read(buf);

        String result = new String(buf);
        System.out.println(result);
        return result;
    }

    public String get(final String key) throws IOException {

        StringBuilder line = new StringBuilder();
        line.append("*2").append("\r\n");
        line.append("$3").append("\r\n");
        line.append("GET").append("\r\n");
        line.append("$").append(key.length()).append("\r\n");
        line.append(key).append("\r\n");

        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(line.toString().getBytes());

        InputStream inputStream = socket.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(inputStream);
        byte[] buf = new byte[1024];
        StringBuilder result = new StringBuilder();
        bis.read(buf);
        result.append(new String(buf));

        outputStream.close();
        bis.close();

        return result.toString();
    }

    public static void main(String[] args) throws IOException {

        MyJedis myJedis = new MyJedis("192.168.121.102", 6379);
        myJedis.set("myJdeis", "wjb is good");

        String value = myJedis.get("myJdeis");
        System.out.println(value);

        myJedis.socket.close();

    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值