📄 testclient.java
字号:
package sms.PHS;
import java.io.*;
import java.util.Properties;
import java.io.InputStream;
import java.util.*;
import java.text.*;
public class TestClient
extends Thread
implements PHSClientListener {
public PrintWriter log = null;
private static int mt = 0;
private static int mo = 0;
private static int result = 0;
public static java.util.Vector ht = new java.util.Vector();
private PHSClient client;
public String content;
private String mobile;
private String name;
private String password;
private String serverHost;
private int serverPort;
String spNumber;
String ServiceId;
String feecode;
String feetype;
String squenceid;
//public int counter = 0;
//public int maxCount = 4;
private boolean isConnected = false;
public TestClient() throws IOException, PHSException, CSocksException {
loadProperties();
makeConnection();
}
/*public void run() {
loadProperties();
makeConnection();
try {
this.submit("02150441612");
}
catch (Exception ex) {
}
}*/
public void active() throws Exception {
PHSActiveTest test = new PHSActiveTest();
PHSActiveTestRep rep = client.active(test, 1000L);
if (rep == null) {
System.out.println("No ActiveResponse packet !");
return;
}
if (rep.getCommandId() != 0x80000004) {
System.out.println("Invalid command id !" + rep.getCommandId());
}
if (rep.getSequenceId() != test.getSequenceId()) {
System.out.println("invalid sequence id !" + rep.getSequenceId());
}
System.out.println("==================this is a active===================");
System.out.println("lenMessageBody = " + rep.lenMessageBody);
}
/**
* 连接登陆
*
*/
public void connect() throws Exception {
String strSource_Addr = name;
String strAuthenticatorSP = password;
byte version = 0x13;
byte loginMode = 2;
PHSLogin login = new PHSLogin(strSource_Addr, strAuthenticatorSP, loginMode,
version);
PHSLoginRep rep = client.login(login, 500000L);
if (rep == null) {
System.out.println(
"No LoginResponse packet ");
return;
}
if (rep.getCommandId() != 0x80000001) {
System.out.println(
"Invalid command id !====================TestClient.java 59" +
rep.getCommandId());
}
if (rep.getSequenceId() != login.getSequenceId()) {
System.out.println(
"invalid sequence id !====================TestClient.java 62" +
rep.getSequenceId());
}
System.out.println(
"==================this is a connection===================");
System.out.println("Status = " + rep.getStatus());
System.out.println("AuthenticatorISMG = " + rep.getAuthenticatorISMG());
System.out.println("Version = " + rep.getVersion());
}
/**
* 退出
*
*/
public void exit() throws Exception {
PHSExit exit = new PHSExit();
PHSExitRep rep = client.exit(exit, 5000L);
if (rep == null) {
System.out.println("No ExitResponse packet !");
return;
}
if (rep.getCommandId() != 0x80000006) {
System.out.println("Invalid command id !");
}
if (rep.getSequenceId() != exit.getSequenceId()) {
System.out.println("invalid sequence id !");
}
}
/**
* 读取配置信息
*/
private void loadProperties() {
Properties props = new Properties();
try {
//FileInputStream fileinputstream = new FileInputStream("smg.properties");
InputStream fileinputstream = getClass().getResourceAsStream(
"smg.properties");
props = new Properties();
props.load(fileinputstream);
fileinputstream.close();
}
catch (Exception e) {
System.out.println(
"Unable to read configuration file,Please set smg.properties in work dir!");
System.exit(0);
}
serverHost = props.getProperty("serverHost");
serverPort = Integer.parseInt(props.getProperty("serverPort"));
name = props.getProperty("name");
password = props.getProperty("password");
spNumber = props.getProperty("spNumber");
mobile = props.getProperty("mobile");
//content = props.getProperty("content");
System.out.println("the serverHost is" + serverHost);
System.out.println("the serverPort is" + serverPort);
System.out.println("the name is" + name);
System.out.println("the password is" + password);
System.out.println("the spNumber is" + spNumber);
System.out.println("the mobile is" + mobile);
System.out.println("小灵通网关连接测试......");
return;
}
/**
* 建立连接
*/
private synchronized void makeConnection() {
//boolean isConnected;
if (client != null) {
System.out.println("已经和网关建立连接!^-^" + client.isConnected());
if (client.isConnected()) {
return;
}
client.close();
client = null;
}
isConnected = false;
while (!isConnected) {
try {
System.out.println("connecting to server ...");
client = new PHSClient(serverHost, serverPort);
isConnected = true;
}
catch (Exception ex) {
ex.printStackTrace();
}
}
if (isConnected) {
try {
//client.startActiveTest(60000);
client.addListener(this);
connect();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
return;
}
/**
* 连接监听
*/
public void onClientDisconnected() {
makeConnection();
System.out.println("Client Disconnected!!!Reconnect ... ");
}
public void onDeliverPacketReceive(PHSDeliver deliver) {
//System.out.println(
// "receive deliver message:===============================>TestClient.java 167");
// System.out.println("getDestTermId=" + deliver.getDestTermId().trim());
// System.out.println("getSrcTermId=" + deliver.getSrcTermId().trim());
// System.out.println("getIsReport=" + deliver.getIsReport());
// System.out.println("getMsgFormat=" + deliver.getMsgFormat());
try {
if (deliver.getMsgFormat() == 15 && deliver.getIsReport()==0) {
System.out.println("getMsgContent=" +
new String(deliver.getMsgContent(), "GBK"));
}
System.out.println("mt============================" + mt);
System.out.println("mo============================" + mo);
if (deliver.getMsgFormat() == 15 && new String(deliver.getMsgContent(),
"GBK").trim().equalsIgnoreCase("A")) {
/*try {
log = new PrintWriter(new FileWriter("aa.log", true));
}
catch (IOException ex1) {
}
log.println("MsgID=" + bytesToBCD(deliver.getMsgId().getBytes()) +
" ********* MsgContent=" +
new String(deliver.getMsgContent(), "GBK"));
log.close();
*/
if (mt == 0) {
TestMT tt = new TestMT(this);
tt.start();
}
mt++;
System.out.println("\r\n AAAAAAAAAAAAAA \r\n ");
if (mt <= 12000) {
ht.add(deliver.getSrcTermId());
}
}
else {
/*if (mo == 0) {
System.out.println("start!!!!");
TestMT tt = new TestMT(this);
tt.start();
}
for (int i = 0; i < 80; i++) {
ht.add(deliver.getSrcTermId());
System.out.println("ADD!!!!!!!!!!!!!!!");
}*/
mo++;
System.out.println("");
System.out.println("B");
System.out.println("");
}
}
catch (UnsupportedEncodingException ex) {}
//System.gc();
System.out.println("mo============================" + mo);
System.out.println("mt============================" + mt);
}
/**
* submit 操作
*
*/
public void submit(String mobile) throws Exception {
byte msgType = 6; //短消息类型(1=取消订阅,2=订阅请求,3=点播,4=订阅,5=交互式操作,6=查询,其他保留)
byte needReport = 1;
byte priority = 0;
String serviceId = ServiceId;
String feeType = feetype;
String fixedFee = "000000";
String feeCode = feecode;
byte msgFormat = 15;
String srcTermId = spNumber;
String chargeTermId = mobile;
String destTermId = mobile;
//String msgContent = new String(content.getBytes("iso8859_1"), "gb2312");
System.out.print("content:"+content);
String msgContent = new String(content);
//int sequenceid=Integer.parseInt(squenceid);
/*
PHSSubmit submit = new PHSSubmit(sequenceid, needReport, priority, serviceId,
feeType, fixedFee, feeCode, msgFormat,
srcTermId, chargeTermId, destTermId,
msgContent);
}
*/
PHSSubmit submit = new PHSSubmit(msgType, needReport, priority, serviceId,
feeType, fixedFee, feeCode, msgFormat,
srcTermId, chargeTermId, destTermId,
msgContent);
PHSSubmitRep rep = client.submit(submit, 5000L);
if (rep == null) {
System.out.println("No SubmitResponse packet !");
return;
}
if (rep.getCommandId() != 0x80000002) {
System.out.println("invalid command id !" + rep.getCommandId());
}
if (rep.getSequenceId() != submit.getSequenceId()) {
System.out.println("invalid sequence id !" + rep.getSequenceId());
}
System.out.println(++result + " Result = " + rep.getResult());
System.out.println("MsgId = " + bytesToBCD(rep.getMsgId().getBytes()));
}
public static String bytesToBCD(byte[] b) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
sb.append( (b[i] >> 4) & 0x0f);
sb.append(b[i] & 0x0f);
}
return sb.toString();
}
/**
* 主程序开始
* @param args
*/
public static void main(String args[]) {
try {
TestClient cmppclient = new TestClient();
cmppclient.connect();
PHSSubmtThread phssubmit=new PHSSubmtThread(cmppclient);
phssubmit.start();
cmppclient.active();
}
catch (Exception e) {
e.printStackTrace();
}
return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -