java netty 服务端向客户端发送16进制数据,客户端无法接收-已解决

项目中需要通过netty与C语言建立连接,双方是通过16进制进行数据传输

 

本案例主要介绍如何在JavaNetty与C Sokcet进行字符串通信,Java服务端,C客户端。

重点提示:网络通信中都是byte字节,两边通信一定要统一编码,尽量避免乱码与接收不到的问题。

1.设置ChildChannelInitializer.java中initChannel(SocketChannel ch)

 

public class ChildChannelInitializer extends ChannelInitializer {
	@Override
	protected void initChannel(SocketChannel ch) throws Exception {
		System.out.println("==================netty报告==================");
		System.out.println("信息:有一客户端链接到本服务端");
		System.out.println("IP:" + ch.localAddress().getHostName());
		System.out.println("Port:" + ch.localAddress().getPort());
		System.out.println("==================netty报告完毕==================");
		
		ChannelPipeline  pipeline  =  ch.pipeline();
		// 在管道中添加我们自己的接收数据实现方法
		/*pipeline.addLast(new MsgEncoder());
		pipeline.addLast(new MsgDecoder());*/
		//注意 我这里并没有设置管道的编码/解码
		
		pipeline.addLast("handler", serverHandler);
	}
	...
}

 

 

 

 

 

2.在ServerHandler.java中接收C语言客户端发来的16进制字符串

 

public class ServerHandler extends ChannelHandlerAdapter{

	public void channelRead(ChannelHandlerContext channel, Object msg) throws Exception {
		try {
			ByteBuf buf = (ByteBuf)msg;
			byte [] bytes = new byte[buf.readableBytes()];
			buf.readBytes(bytes);//复制内容到字节数组bytes 
			String receiveStr = ConvertCode.receiveHexToString(bytes);//将接收到的数据转为字符串,此字符串就是客户端发送的字符串
			//返回16进制到客户端
			writeToClient(receiveStr,channel,"测试");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error("异常:",e);
		}
	}
	/**
	 * 公用回写数据到客户端的方法
	 * @param 需要回写的字符串
	 * @param channel
	 * @param mark 用于打印/log的输出
	 * 
//channel.writeAndFlush(msg);//不行 *
//channel.writeAndFlush(receiveStr.getBytes());//不行 *
在netty里,进出的都是ByteBuf,楼主应确定服务端是否有对应的编码器,将字符串转化为ByteBuf */ private void writeToClient(final String receiveStr, ChannelHandlerContext channel, final String mark) { try { ByteBuf bufff = Unpooled.buffer();//netty需要用ByteBuf传输 bufff.writeBytes(ConvertCode.hexString2Bytes(receiveStr));//对接需要16进制 channel.writeAndFlush(bufff).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { StringBuilder sb = new StringBuilder(""); if(!StringUtils.isEmpty(mark)){ sb.append("【").append(mark).append("】"); } if (future.isSuccess()) { System.out.println(sb.toString()+"回写成功"+receiveStr); log.info(sb.toString()+"回写成功"+receiveStr); } else { System.out.println(sb.toString()+"回写失败"+receiveStr); log.error(sb.toString()+"回写失败"+receiveStr); } } }); } catch (Exception e) { e.printStackTrace(); System.out.println("调用通用writeToClient()异常"+e.getMessage()); log.error("调用通用writeToClient()异常:",e); } } ... }


3.编写ConvertCode.java:在第2里面用到了ConvertCode.receiveHexToString(bytes)和ConvertCode.hexString2Bytes(receiveStr)

 

 

package com.vk.updoc.netty2;

public class ConvertCode {
	 /** 
     * @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 字节数组 
     */  
    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进制字符串 
     */  
    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++) {
        	//System.out.println(Integer.valueOf(src.substring(i * 2, i * 2 + 2),16).byteValue());
            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 
     */  
    public 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;i1
		1 xor f7-->f6
		f6 xor 0f-->f9
		....
		62 xor 84-->e6
		即,得到的一字节校验码为:e6
     * @return
     */
    public static String xor(String strHex_X,String strHex_Y){   
        //将x、y转成二进制形式   
        String anotherBinary=Integer.toBinaryString(Integer.valueOf(strHex_X,16));   
        String thisBinary=Integer.toBinaryString(Integer.valueOf(strHex_Y,16));   
        String result = "";   
        //判断是否为8位二进制,否则左补零   
        if(anotherBinary.length() != 8){   
        	for (int i = anotherBinary.length(); i <8; i++) {   
                anotherBinary = "0"+anotherBinary;   
            }   
        }   
        if(thisBinary.length() != 8){   
        	for (int i = thisBinary.length(); i <8; i++) {   
                thisBinary = "0"+thisBinary;   
            }   
        }   
        //异或运算   
        for(int i=0;i"07dd"
 	 * @param input 需要补位的字符串
 	 * @param size 补位后的最终长度
 	 * @param symbol 按symol补充 如'0'
 	 * @return
 	 * N_TimeCheck中用到了
 	 */
 	public static String fill(String input, int size, char symbol) {
		while (input.length() < size) {
			input = symbol + input;
		}
		return input;
	}
    public static void main(String args[]) {  
    	String productNo = "3030303032383838";
    	System.out.println(hexString2String(productNo));
    	productNo = "04050103000001070302050304";
    	System.out.println(hexString2String(productNo));
    }  
    //用Java语言实现对十六进制字符串异或运算http://blog.csdn.net/acrambler/article/details/45743157	
}

 

 

客户端测试工具我使用的:TCPUDP Socket调试工具 V2.3 绿色免费版

 

 

你可能感兴趣的:(netty,java,springMVC)