这是客户端MyClient.java
package com.jadyer.client;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
public class MyClient {
public static void main(String[] args) {
StringBuilder reqData = new StringBuilder();
for(int i=0; i<22; i++){
reqData.append("0003961000510110199201209222240000020120922000069347814303000700000813``中国联通交费充值`为号码18655228826交费充值100.00元`UDP1209222238312219411`10000```20120922`chinaunicom-payFeeOnline`UTF-8`20120922223831`MD5`20120922020103806276`1`02`10000`20120922223954`20120922`BOCO_B2C```http://192.168.20.2:5545/ecpay/pay/elecChnlFrontPayRspBackAction.action`1`立即支付,交易成功`");
}
//UTF-8编码的,上面的reqData循环后的字符串长度就是8712,再加上报文协议中表示完整报文长度的前六个字节就是8718
reqData.insert(0, "008718");
//socket = SSLSocketFactory.getDefault().createSocket("127.0.0.1", 8060);
Socket socket = new Socket();
try {
socket.setSoTimeout(30000);
socket.connect(new InetSocketAddress("127.0.0.1", 8060), 1000);
/**
* 发送TCP请求
*/
OutputStream out = socket.getOutputStream();
out.write(reqData.toString().getBytes("UTF-8"));
/**
* 接收TCP响应
*/
InputStream in = socket.getInputStream();
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
byte[] buffer = new byte[512];
int len = -1;
while((len=in.read(buffer)) != -1){
bytesOut.write(buffer, 0, len);
}
System.out.println("收到服务器应答=[" + bytesOut.toString("UTF-8") + "]");
} catch (Exception e) {
System.out.println("请求通信[127.0.0.1:8060]时偶遇异常,堆栈轨迹如下");
e.printStackTrace();
} finally {
if(null != socket){
try {
socket.close();
} catch (IOException e) {
//ignore the exception
}
}
}
}
}
