dhdzjs.java
来自「JAVA实现的SOCKET通讯源码,里面有双方制定的报文标示!」· Java 代码 · 共 241 行
JAVA
241 行
package testDzjs.yhServer.appInstance;
import testDzjs.yhServer.com.SendReceive;
import testDzjs.yhServer.com.YHBuildProtocol;
import java.net.Socket;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 2005-4-16
* Time: 22:48:48
* To change this template use File | Settings | File Templates.
*/
/**
* 电话电子缴税服务器
*/
public class DhDzjs implements YhInterface{
SendReceive sendReceive = new SendReceive(); //收发数据实例
YHBuildProtocol buildProtocol = new YHBuildProtocol(); //构造协议实例
Socket socket = null; //套接字
BufferedInputStream in = null; //输入流
BufferedOutputStream out = null; //输出流
private String packageType = "11";
private String swjg_dm = "";
private String jylsh = "";
private String nsrbm = "";
private String kkje = "";
private String wtjyrq = "";
private String dfzh = "";
private String sPackageType = "12";
private String bz = "";
private String snsrbm = "";
private String sswjg_dm = "";
private String kkrq = "";
//接收包长度
public static int[] receiveLength ={2,11,15,20,15,8,50};
public static int rLength = 121;
//返回包长度
public static int[] sendLength = {2,2,20,11,8};
private static int[] cxLength = {2,2,20};
private static String cxPackageType = "42";
private String cxBz = "00";
private String cxJe = "99999000";
//接收包
String[] receive = new String[7];
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 setNsrbm(String s){
}
public void setSwjgdm(String s){
}
public void setNsrmc(String s){
}
/**
* 处理流程
*/
private void run(){
parseReceiver(receiver());
jycl();
if(packageType.equals("41")){
sender(buildSendBody("cx"));
}else{
sender(buildSendBody(""));
}
}
public String getInfo(){
return "";
}
/**
* 读取扣款包
* @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;
}
/**
* 解析扣款包
* @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];
swjg_dm = this.receive[1];
jylsh =this.receive[2];
nsrbm = this.receive[3];
kkje = this.receive[4];
wtjyrq = this.receive[5];
dfzh = this.receive[6];
}
/**
* 解析扣款包
* @param receive
*/
private void parseReceiverCX(String receive){
int startx = 0;
for(int i = 0;i < receiveLength.length - receiveLength.length + 1;i ++){
this.receive[i] = readString(receive,startx,receiveLength[i]);
startx += receiveLength[i];
}
packageType = this.receive[0];
// swjg_dm = this.receive[1];
// jylsh =this.receive[2];
// nsrbm = this.receive[3];
// kkje = this.receive[4];
// wtjyrq = this.receive[5];
// dfzh = this.receive[6];
}
/**、
* 字串读取
* @param str
* @param startx
* @param length
* @return
*/
private String readString(String str,int startx,int length){
return str.substring(startx,startx+length);
}
/**
* 缴税扣款处理
*/
private void jycl(){
bz="00";
snsrbm=nsrbm;
sswjg_dm=swjg_dm;
kkrq="20050421";
}
/**
* 生成返回包
* @return
*/
private byte[] buildSendBody(String cx){
byte[] body;
if(cx.equals("cx")){
String[] send = {cxPackageType,cxBz,cxJe};
body = buildProtocol.getProtocolDataDH(send,cxLength);
}else{
String[] send = {sPackageType,bz,snsrbm,sswjg_dm,kkrq};
body = buildProtocol.getProtocolDataDH(send,sendLength);
}
return body;
}
/**
* 发送数据
* @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();
}
}
/**
* 字串填充
* @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;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?