你的位置:首页 > Java教程

[Java教程]Socket 例子


package com.pab.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

 

/**
* 平安银行
* @author jian.zhou
*/
public class PabUtil {
/** 不允许实例 */
private PabUtil() {
super();
}

private final static Logger log = LoggerFactory.getLogger(PabUtil.class);

private static final int[] FIELD_LENGTH_INDEX = new int[] { // 用来计算各个字段的在报文头中的长度和位置
1, 7, 9, 11, 31, 41, 47, 52, 54, 62, 68, 88, 94, 194, 195, 198, 199, 200, 212, 222, 223 };

private static int[] FIELD_LENGTH = new int[FIELD_LENGTH_INDEX.length]; // 纪录各个字段的字节表示在数组中的起始位置,最后一位是补位-为方便解析而添加

static {
for (int i = 0; i < FIELD_LENGTH_INDEX.length; i++) {
FIELD_LENGTH[i] = FIELD_LENGTH_INDEX[i] - 1;
}
}

/**
* 截取字节
* @param src
* @param begin
* @param end
* @return
*/
public static byte[] subBytes(byte[] src, int begin, int end) {
int len = end - begin;
byte[] bs = new byte[len];
for (int i = begin; i < begin + len; i++)
bs[i - begin] = src[i];
return bs;
}

/**
* 发送报文
* @param msg 报文
* @return String[] = backArray[0]状态 = succ, fail, backArray[1] = 返回的报文
*/
public static String[] sendPacket(String msg, Properties prop) {
log.info("PAB 110001 SEND PACKET IS:{}", msg);
Socket socket = null;
BufferedInputStream bi = null;
BufferedOutputStream bo = null;
InputStream in = null;
String backMsg = "";
String[] backStr = new String[2];
String ip = prop.getProperty("merCustomIp");
int port = Integer.parseInt(prop.getProperty("pfxPassword"));
try {
// 客户端socket指定服务器的地址和端口号
socket = new Socket(ip, port);
socket.setSoTimeout(30000);
log.info("{}", socket);

OutputStream ops = socket.getOutputStream();
bo = new BufferedOutputStream(ops);
bo.write(msg.getBytes(PabContants.PINGAN_CODE_UTF8));
bo.flush();

in = socket.getInputStream();
bi = new BufferedInputStream(in);
byte[] hb = new byte[PabContants.PACKET_HEAD_LENGTH];// 读取固定报文头 长度为222
bi.read(hb, 0, PabContants.PACKET_HEAD_LENGTH);

log.info("PAB 110001 BACK PACKET HEAD IS:{}", new String(hb, PabContants.PINGAN_CODE_UTF8));

// 截取报文头 长度 222 位z
String[] packetHead = new String[FIELD_LENGTH_INDEX.length - 1];// 报文数组
int j = 0;
for (int i = 1; i < FIELD_LENGTH.length; i++) {
int start = FIELD_LENGTH[i - 1];
int end = FIELD_LENGTH[i];
byte[] b = subBytes(hb, start, end);
packetHead[j] = new String(b, PabContants.PINGAN_CODE_UTF8);
j++;
}
log.info("pakcetHead {}", Arrays.toString(packetHead));

// 根据开发文档 报文头的返回码为000000,表示后续存在 if ("000000".equals(packetHead[11])) {
log.info("---start receive packet body----");

int parcketLength = Integer.parseInt(packetHead[4]);
byte[] backmsg = new byte[parcketLength];
int cnt = 0;
int iLen = 0;
while (cnt < (parcketLength)) {
iLen = bi.read(backmsg, cnt, parcketLength);
cnt += iLen;
}

backMsg = new String(backmsg, PabContants.PINGAN_CODE_UTF8);
backStr[0] = "succ";
backStr[1] = backMsg;
log.info("PAB 110001 BACK PACKET HEAD BODY IS:" + backStr[1]);
log.info("---end receive packet body----");
} else if ("YQ9996".equals(packetHead[11])) {
backStr[0] = "confirm";
backStr[1] = packetHead[11] + ":" + packetHead[12];
} else {
backStr[0] = "fail";
backStr[1] = packetHead[11] + ":" + packetHead[12];
log.info("trans error--- packetBody is not, backcode:" + packetHead[11] + ":" + packetHead[12].trim());
}

} catch (Exception e) {
log.error("{}", e);
} finally {
try {
if (bi != null) {
bi.close();
}
if (bo != null) {
bo.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
log.error("", e);
}
}
return backStr;
}

/**
* 判断是否为空
* @param str
* @return
*/
public static String isNull(String str) {
if (str == null) return "";
else return str;
}
}