wireshark grpc 客户端 发送报文

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

grpc 端口 9898

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.Charset;



/**
 * wireshark  grpc 客户端 发送报文
 * 
505249202a20485454502f322e300d0a0d0a534d0d0a0d0a000018040000000000000200000000000300000000000400100000000600002000
000004080000000000000f0001
000000040100000000
000076012400000003000000000f418ca0e41d0b4d65645905b3b96c44946272d141fc1eca245f71a9ae82b1b87eb1968a0f83865f8b1d75d0620d263d4c4d656440027465864d833505b11f7a909acac8b741f71ad51529f4c057081707408e9acac8b0c842d6958b510f21aa9b839bd9ab408a9acac8b26c190ab466ab0000000b00010000000300000000060a0474657374
 *
 */
public class TCPClient {

	public static void main(String[] args) {

		test();
	}

	public static void test() {

		//没问题
		String data = "505249202a20485454502f322e300d0a0d0a534d0d0a0d0a000018040000000000000200000000000300000000000400100000000600002000000004080000000000000f0001000000040100000000000076012400000003000000000f418ca0e41d0b4d65645905b3b96c44946272d141fc1eca245f71a9ae82b1b87eb1968a0f83865f8b1d75d0620d263d4c4d656440027465864d833505b11f7a909acac8b741f71ad51529f4c057081707408e9acac8b0c842d6958b510f21aa9b839bd9ab408a9acac8b26c190ab466ab0000000b00010000000300000000060a0474657374";
        
        //有问题
		//data = "000076012400000003000000000f418ca0e41d0b4d65645905b3b96c44946272d141fc1eca245f71a9ae82b1b87eb1968a0f83865f8b1d75d0620d263d4c4d656440027465864d833505b11f7a909acac8b741f71ad51529f4c057081707408e9acac8b0c842d6958b510f21aa9b839bd9ab408a9acac8b26c190ab466ab0000000b00010000000300000000060a0474657374"; 
		
		
		
		int serverPort = 9898;
		Socket sock;

		try {
			sock = new Socket("localhost", serverPort);

			BufferedReader in = new BufferedReader(
					new InputStreamReader(sock.getInputStream(), Charset.forName("UTF-8")));

			/*
			 * BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
			 * sock.getOutputStream(),Charset.forName("UTF-8")));
			 */

			OutputStream out = sock.getOutputStream();
			

			byte[] bytes = TypeConversion.hexString2Bytes(data);
			System.out.println("bytes===="+bytes);
			out.write(bytes);
			out.flush();

			String inputLine = in.readLine();
			System.out.println("Data : " + inputLine);

			in.close();
			out.close();
			sock.close();

		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	

	public static final byte[] hexStringToByteArray(String hexEncodedBinary) {
		if (hexEncodedBinary.length() % 2 == 0) {
			char[] sc = hexEncodedBinary.toCharArray();
			byte[] ba = new byte[sc.length / 2];

			for (int i = 0; i < ba.length; i++) {
				int nibble0 = Character.digit(sc[i * 2], 16);
				int nibble1 = Character.digit(sc[i * 2 + 1], 16);
				if (nibble0 == -1 || nibble1 == -1) {
					throw new IllegalArgumentException("Hex-encoded binary string contains an invalid hex digit in '"
							+ sc[i * 2] + sc[i * 2 + 1] + "'");
				}
				ba[i] = (byte) ((nibble0 << 4) | (nibble1));
			}

			return ba;
		} else {
			throw new IllegalArgumentException("Hex-encoded binary string contains an uneven no. of digits");
		}
	}

}
/** 
 * @Package: 
 * @ClassName:TypeConversion 
 * @Description:字节流、字符串、16进制字符串转换 
 * @author:xk 
 * @date:Jan 8, 2013 5:00:08 PM 
 */  
public class TypeConversion {  
    /** 
     * @Title:bytes2HexString 
     * @Description:字节数组转16进制字符串 
     * @param b 
     *            字节数组 
     * @return 16进制字符串 
     * @throws 
     */  
    public static String bytes2HexString(byte[] b) {  
        StringBuffer result = new StringBuffer();  
        String hex;  
        for (int i = 0; i < b.length; i++) {  
            hex = Integer.toHexString(b[i] & 0xFF);  
            if (hex.length() == 1) {  
                hex = '0' + hex;  
            }  
            result.append(hex.toUpperCase());  
        }  
        return result.toString();  
    }  
  
    /** 
     * @Title:hexString2Bytes 
     * @Description:16进制字符串转字节数组 
     * @param src 
     *            16进制字符串 
     * @return 字节数组 
     * @throws 
     */  
    public static byte[] hexString2Bytes(String src) {  
        int l = src.length() / 2;  
        byte[] ret = new byte[l];  
        for (int i = 0; i < l; i++) {  
            ret[i] = (byte) Integer  
                    .valueOf(src.substring(i * 2, i * 2 + 2), 16).byteValue();  
        }  
        return ret;  
    }  
  
    /** 
     * @Title:string2HexString 
     * @Description:字符串转16进制字符串 
     * @param strPart 
     *            字符串 
     * @return 16进制字符串 
     * @throws 
     */  
    public static String string2HexString(String strPart) {  
        StringBuffer hexString = new StringBuffer();  
        for (int i = 0; i < strPart.length(); i++) {  
            int ch = (int) strPart.charAt(i);  
            String strHex = Integer.toHexString(ch);  
            hexString.append(strHex);  
        }  
        return hexString.toString();  
    }  
  
    /** 
     * @Title:hexString2String 
     * @Description:16进制字符串转字符串 
     * @param src 
     *            16进制字符串 
     * @return 字节数组 
     * @throws 
     */  
    public static String hexString2String(String src) {  
        String temp = "";  
        for (int i = 0; i < src.length() / 2; i++) {  
            temp = temp  
                    + (char) Integer.valueOf(src.substring(i * 2, i * 2 + 2),  
                            16).byteValue();  
        }  
        return temp;  
    }  
      
    /** 
     * @Title:char2Byte 
     * @Description:字符转成字节数据char-->integer-->byte 
     * @param src 
     * @return 
     * @throws 
     */  
    public static Byte char2Byte(Character src) {  
        return Integer.valueOf((int)src).byteValue();  
    }  
      
        /** 
     * @Title:intToHexString 
     * @Description:10进制数字转成16进制 
     * @param a 转化数据 
     * @param len 占用字节数 
     * @return 
     * @throws 
     */  
    private static String intToHexString(int a,int len){  
        len<<=1;  
        String hexString = Integer.toHexString(a);  
        int b = len -hexString.length();  
        if(b>0){  
            for(int i=0;i

 

转载于:https://my.oschina.net/xiaominmin/blog/1805547

你可能感兴趣的:(wireshark grpc 客户端 发送报文)