package com.learn.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.Reader; import java.net.InetSocketAddress; import java.net.Socket; /** * Created by manager on 2017/12/30. */ public class RedisClient { public static final byte DOLLAR_BYTE = '$'; public static final byte ASTERISK_BYTE = '*'; public static final byte PLUS_BYTE = '+'; public static final byte MINUS_BYTE = '-'; public static final byte COLON_BYTE = ':'; public static final byte CARRIAGE_RETURN = '\r'; public static final byte LINE_FEED = '\n'; public RedisClient() throws IOException { } public static void log(Object tsr){ System.out.println(tsr); } public static void main(String args[]) throws IOException { System.out.print((byte)'1'); Socket socket = new Socket(); socket.setReuseAddress(true); // Will monitor the TCP connection is valid socket.setKeepAlive(true); // close socket buffer to ensure timely delivery of data socket.setTcpNoDelay(true); // Control calls close() method to no block. socket.setSoLinger(true, 0); socket.connect(new InetSocketAddress("127.0.0.1", 6379), 2000); socket.setSoTimeout(2000); OutputStream out = socket.getOutputStream(); byte[] bytes=new byte[100]; bytes[0]=ASTERISK_BYTE; bytes[1]=49; bytes[2]=CARRIAGE_RETURN; bytes[3]=LINE_FEED; bytes[4]=DOLLAR_BYTE; bytes[5]=52; bytes[6]=CARRIAGE_RETURN; bytes[7]=LINE_FEED; bytes[8]=80; bytes[9]=73; bytes[10]=78; bytes[11]=71; bytes[12]=CARRIAGE_RETURN; bytes[13]=LINE_FEED; out.write(bytes); out.flush(); // outputStream.close(); InputStream inputStream = socket.getInputStream(); InputStreamReader inputStreamReader=new InputStreamReader(inputStream); BufferedReader bufr = new BufferedReader(inputStreamReader);//缓冲 String info=null; log(bufr.readLine()); } }
转载于:https://my.oschina.net/QAAQ/blog/1599687