qydzjs.java

来自「JAVA实现的SOCKET通讯源码,里面有双方制定的报文标示!」· Java 代码 · 共 215 行

JAVA
215
字号
package testDzjs.yhServer.appInstance;

import testDzjs.yhServer.com.SendReceive;
import testDzjs.yhServer.com.YHBuildProtocol;

import java.io.BufferedOutputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.Socket;

import common.DateTime;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2005-4-16
 * Time: 19:21:00
 * To change this template use File | Settings | File Templates.
 */
public class QyDzjs implements YhInterface{

    SendReceive sendReceive = new SendReceive(); //发送数据实例

    YHBuildProtocol buildProtocol = new YHBuildProtocol(); //构造包实例

    Socket socket = null; //套接字

    BufferedInputStream in = null; //输入流

    BufferedOutputStream out = null; //输出流

    //接收包
    private String packageType = "";          //包类型
    private String swjgdm="" ;                     //纳税人所属税务机关编码
    private String jylsh="";                       //国税交易流水号
    private String nsrbm="";                       //纳税人编码
    private String kkje="";                        //扣款金额
    private String lsgx="";                        //隶属关系代码
    private String ysfp="";                        //预算分配比例代码
    private String yskm="";                        //预算科目代码
    //接收包

    //返回包
    private String sPackageType = "13";     //包类型
    private String jybz = "";           //交易标志
    private String yhJylsh = "";        //银行交易流水号
    private String fkxx = "";           //返馈信息
                 //   jylsh;            //国税交易流水号
    //返回包

    String[] receive =  new String[8];  //接收包

    public static int[] receiveLength ={2,11,16,22,15,2,4,6};   //接收包长度

    public static int[] sendLength = {2,2,16,16,40};    //返回包长度

    private long sCount = 0;
    /**
     * 外部调用开始点
     * @param socket
     * @param in
     * @param out
     */
    public void start(Socket socket,BufferedInputStream in,BufferedOutputStream out,long l){
        this.sCount = l;
        this.socket = socket;
        this.in = in;
        this.out = out;
        run();
    }


    public void setSwjgdm(String s){

    }

    public void setNsrbm(String s){

    }
    public void setNsrmc(String s){

    }
    /**
     * 内部处理流程
     */
    private void run(){
        parseReceiver(receiver());
        jycl();
        sender(buildSendBody());
    }


    public String getInfo(){
        return "";
    }
    /**
     * 发送数据包
     * @param message
     */
    private void sender(byte[] message){
        try {
            sendReceive.sendByte(message,out);
        } catch (IOException e) {
            try {
                this.socket.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
    }

    /**
     * 接收数据包,返回字串
     * @return
     */
    private String receiver(){
        String receive = "";
        try {
            receive = sendReceive.receive(in);
        } catch (IOException e) {
            try {
                this.socket.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
        System.out.println(sCount + "收到税局扣款包:" + receive);
        return receive;
    }

    /**
     * 交易处理
     */
    private void jycl(){
        if(!packageType.equals("11")){
            jybz = "99";
            yhJylsh = "99999999";
            fkxx = "扣款失败!包类型错误!" + packageType;
        }else{
            jybz = "00";
            yhJylsh = getYhJylsh();
            fkxx = "扣款成功!扣款金额为:" + kkje;
        }
    }


    /**
     * 生成唯一交易流水号后八位
     * @return
     */
    private String getYhJylsh(){
        return DateTime.getDateTime() + fillStr(String.valueOf(DbYh.getInstance().getXh()),8);
    }

    /**
     * 将字串填充成指定长度
     * @param str
     * @param length
     * @return
     */
    private String fillStr(String str,int length){
        if(str.length() > length){
            return str.substring(0,length - 1);
        }
        for(;str.length() < length;){
            str = "0" + str;
        }
        return str;
    }

    /**
     * 解析返回包内容
     * @param receive
     */
    private void parseReceiver(String receive){
        int  startx = 0;
        for(int i = 0;i < receiveLength.length;i ++){
            this.receive[i] = readString(receive,startx,receiveLength[i]);
            startx += receiveLength[i];
        }
        packageType = this.receive[0];
        swjgdm = this.receive[1];
        jylsh = this.receive[2];
        nsrbm = this.receive[3];
        kkje = this.receive[4];
        lsgx = this.receive[5];
        ysfp = this.receive[6];
        yskm = this.receive[7];
    }

    /**
     * 读取字串
     * @param str
     * @param startx
     * @param length
     * @return
     */
    private String readString(String str,int startx,int length){
        return str.substring(startx,startx+length);
    }

    /**
     * 生成发送数据包
     * @return
     */
    private byte[] buildSendBody(){
        byte[] body;
        String[] send = {sPackageType,jybz,jylsh,yhJylsh,fkxx};
        body = buildProtocol.getProtocolData(send,sendLength);
        return body;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?