java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE, JavaME, JavaSE)的总称。本站提供基于Java框架struts,spring,hibernate等的桌面应用、web交互及移动终端的开发技巧与资料

保持永久学习的心态,将成就一个优秀的你,来 继续搞起java知识。

昨天我们谈了怎么建立socket通信的服务端和客户端,今天我们就来谈一谈怎么封装报文。
什么是报文这里我就不在阐述了,不清楚的朋友可以自己去查资料。我们今天要谈的报文主要友以下几个部分组成:
3位同步校验位+8位报文长度+报文头+报文体+32位MD5校验位
基本格式如下:
0X110X120X130000023202042014122314122301000815217010001张三B251AB76B11114DB176023A0AA27A524
说明:
  前面的0X110X120X13是3位16进制的同部位,这里为了大家理解,所以就以字符的形式谢出来了。00000232是报文长度。02042014122314122301000815217010001是报文头。即每个报文都包含的信息。张三是报文体。B251AB76B11114DB176023A0AA27A524是加密数据。
关于如何将对象转换为xml格式的报文我将在下一篇写,这里主要是给大家如何将如上的这些字符串转化为字节以及如何发送和接收报文。
1.建立报文的对象

public class SocketPacket {

    private String bodyLen;
    private String body;
    private String syncStr;
    private String md5;
    public String getBodyLen() {
        return bodyLen;
    }
    public String getBody() {
        return body;
    }
    public String getSyncStr() {
        return syncStr;
    }
    public String getMd5() {
        return md5;
    }
    public void setBodyLen(String bodyLen) {
        this.bodyLen = bodyLen;
    }
    public void setBody(String body) {
        this.body = body;
    }
    public void setSyncStr(String syncStr) {
        this.syncStr = syncStr;
    }
    public void setMd5(String md5) {
        this.md5 = md5;
    }
    
    
    public byte[] getByteStream() throws UnsupportedEncodingException{
        byte[] bodyBytes = this.body.getBytes("gbk");//获得body的字节数组
        int bodyLength = bodyBytes.length;
        int socketLength = 3+bodyLength+8+32;
        byte [] soc = new byte[socketLength];
        //添加校验数据
        int index = 0;
        soc[0]=0x11;
        soc[1]=0x12;
        soc[2]=0x13;
        index+=3;
        //添加8位报文长度(我的博文中也有NumberFormat的用法介绍)
        NumberFormat numberFormat = NumberFormat.getNumberInstance();
        numberFormat.setMinimumIntegerDigits(8);
        numberFormat.setGroupingUsed(false);
        byte [] num = numberFormat.format(socketLength).getBytes();
        for(int i = 0;i<8;i++){
            soc[index++]= num[i];
        }
        //添加body内容
        for(int i = 0;i<bodyLength;i++){
            soc[index++] = bodyBytes[i];
        }
        //添加md5校验码
        byte [] md5Bytes = this.md5.getBytes();
        for (int i = 0; i < num.length; i++) {
            soc[index++] = md5Bytes[i];
        }
        return soc;
    }
    
    //字节装转报文string
    public String getString(byte [] socketBytes){
        String syncStr = this.bytesToString(socketBytes, 0, 3);
        String socketLength = this.bytesToString(socketBytes, 3, 3+8);
        String body = this.bytesToString(socketBytes, 3+8, socketBytes.length-32);
        String md5 = this.bytesToString(socketBytes,socketBytes.length-32,socketBytes.length);
        return syncStr+socketLength+body+md5;
    }
    
    //将字节数组转化为string
    public String bytesToString(byte [] bytes,int start,int end){
        String str = "";
        if(bytes.length<end-start){
            return str;
        }
        byte [] bs = new byte[end-start];
        for(int i = 0;i<end-start;i++){
            bs[i] = bytes[start++];
        }
        str = new String(bs);
        return str;
    }
    
    public String toString(){
        return this.syncStr+this.bodyLen+this.body+this.md5;
    }
    
}

2.封装发送和接收报文的工具类

/**
 * 报文发送
 */
public class SockeUtil {
    Socket socket = null;
    public SockeUtil(String ip,int port) throws UnknownHostException, IOException{
        socket = new Socket(ip, port);
    }
    //
    public SocketPacket sentSocket(SocketPacket socketPacket) throws UnsupportedEncodingException, IOException{
        SocketPacket sPacket = new SocketPacket();
        OutputStream output=null;
        InputStream input =null;
        // 同步字符串(3byte)
        byte[] sync = null; //
        byte[] bodyLen = null; // 8位长度
        byte[] body = null; // 内容
        byte[] md5 = null;  // MD5
        output = socket.getOutputStream();
        //写数据发送报文
        output.write(socketPacket.getByteStream());
        //获得服务端返回的数据
        input = socket.getInputStream();
        sync = this.streamToBytes(input,3);
        bodyLen = this.streamToBytes(input, 8);
        String lenString = new String(bodyLen);
        int len = Integer.valueOf(lenString);
        body = this.streamToBytes(input, len);
        md5 = this.streamToBytes(input, 32);
        sPacket.setSyncStr(new String(sync,Charset.forName("gbk")));
        socketPacket.setBodyLen(new String(bodyLen,Charset.forName("gbk")));
        socketPacket.setBody(new String(body,Charset.forName("gbk")));
        socketPacket.setMd5(new String(md5,Charset.forName("gbk")));
        return sPacket;
    }
    
    public byte[] streamToBytes(InputStream inputStream,int len){
        /**
         * inputStream.read(要复制到得字节数组,起始位置下标,要复制的长度)
         * 该方法读取后input的下标会自动的后移,下次读取的时候还是从上次读取后移动到的下标开始读取
         * 所以每次读取后就不需要在制定起始的下标了
         */
        byte [] bytes= new byte[len];
        try {
            inputStream.read(bytes, 0, len);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return bytes;
    }
}

3.在封装一个调用报文发送的类:

public String socket(SocketPacket socketPacket) throws UnsupportedEncodingException{

    	SockeUtil socketUtil = null;;
        try {
        	socketUtil = new SockeUtil("192.168.13.15", 8080);
        } catch (UnknownHostException e) {
            log.error("socket链接异常,链接信息:");
            e.printStackTrace();
        } catch (IOException e) {
            log.error("socket IO异常");
            e.printStackTrace();
        }
        SocketPacket s = null;
        try {
            s = socketUtil.sentSocket(socketPacket);
        } catch (Exception e) {
            try {
                log.error("socket发送消息异常,发送信息:" + new String(socketPacket.getByteStream(),"GBK"));
            } catch (UnsupportedEncodingException e1) {
                log.error("socket将socketPackage转为字符串异常,socketPackage信息:" + socketPacket.getByteStream());
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
        String result = "";
        try {
             result = new String(s.getByteStream(), "GBK");
        } catch (UnsupportedEncodingException e) {
            log.error("socket将socketPackage转为字符串异常,socketPackage信息:" + socketPacket.getByteStream());
            e.printStackTrace();
        }
        return result ;
  }

稍做了些修改,原创封装的调用类,有点问题........
转自:http://www.cnblogs.com/mouseIT/p/4189386.html
javascoket

因为水平有限,难免有疏忽或者不准确的地方,希望大家能够直接指出来,我会及时改正。一切为了知识的分享。

后续会有更多的精彩的内容分享给大家。