📄 client.java
字号:
package com.gctech.cmpp.client;
import java.io.*;
import java.net.Socket;
import org.apache.log4j.Logger;
import com.gctech.util.Tools;
import com.gctech.cmpp.util.IDGenerator;
import com.gctech.cmpp.util.Util;
import com.gctech.cmpp.util.CMPPTimestampFormat;
import org.apache.log4j.PropertyConfigurator;
import java.util.Properties;
import com.gctech.cmpp.msg.Status;
import java.net.SocketTimeoutException;
import com.gctech.cmpp.util.MsgLogger;
import com.gctech.cmpp.msg.SubmitRequest;
import com.gctech.cmpp.msg.SubmitResponse;
import com.gctech.cmpp.msg.CommandID;
import com.gctech.cmpp.msg.ConnectRequest;
import com.gctech.cmpp.msg.ConnectResponse;
import com.gctech.cmpp.msg.ActiveTestRequest;
import com.gctech.cmpp.msg.ActiveTestResponse;
/**
* <p>Title: 短信 API</p>
* <p>Description: 短信SP API</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: GCTECH</p>
* @author 王红宝
* @version $Id: Client.java,v 1.4 2004/07/16 08:32:18 wanghb Exp $
*/
public class Client implements CmppCommunicator{
private long lastCommunicate = System.currentTimeMillis();
//接收消息
public byte[] receive() throws IOException{
DataInputStream in = new DataInputStream(sock.getInputStream());
//读取长度
int msgLength = 0;
msgLength = in.readInt();
logger.debug("receive msgSize is:" + msgLength);
if ( msgLength >= 12 && msgLength <= 2048 ){
//读取数据
byte[] resp = new byte[msgLength-4];
in.read(resp);
this.lastCommunicate = System.currentTimeMillis();
return resp;
}else{
return null;
}
}
Socket sock;
//检测请求
public boolean activeTest(int seq) throws IOException{
byte[] active = new byte[12];
Tools.int2byte(12, active, 0);
Tools.int2byte(CommandID.CMPP_ACTIVE_TEST, active, 4);
Tools.int2byte(seq, active, 8);
send(active);
return true;
}
/**
* 发送消息。
* */
/* public SubmitResponse submit(SubmitRequest submitReq){
SubmitResponse resp = new SubmitResponse();
int dataLength = 159 + submitReq.getMsgLength();
byte[] data = new byte[dataLength];
Tools.int2byte(dataLength, data, 0);
Tools.int2byte(CommandID.CMPP_SUBMIT, data, 4);
Tools.int2byte(IDGenerator.getInstance().increment(), data, 8);
//复制数据
//内容
byte[] temp = submitReq.getMsgContent().getBytes();
//modify by liya 2004-04-20 begin
data[150] = (byte)submitReq.getMsgLength();
//modify by liya 2004-04-20 end
System.arraycopy(temp, 0, data, 151, submitReq.getMsgLength());
//源手机
temp = submitReq.getSrcId().getBytes();
System.arraycopy(temp, 0, data, 107, temp.length);
//接收手机
temp = submitReq.getDestTermId().getBytes();
System.arraycopy(temp, 0, data, 129, temp.length);
//计费手机
temp = submitReq.getFeeTermId().getBytes();
System.arraycopy(temp, 0, data, 35, temp.length);
resp.setStatus(send(data));
return resp;
}
/**
* 链路检测回复消息。
* */
public static void main(String[] args) {
Client client = new Client();
}
public SubmitResponse submit2(byte[] data2){
/**
try {
File f=new File("/www/sms/business/lib/liya123");
FileOutputStream fo=new FileOutputStream(f);
fo.write(data2);
fo.close();
fo=null;
} catch (Exception e){}
SubmitResponse resp = new SubmitResponse();
return resp;
*/
int msg_length=data2[72];
String strContent=new String(data2, 73, msg_length);
String strSrcTermID=new String(data2, 16, 21).trim();
String strDestTermID=new String(data2, 50, 21).trim();
SubmitResponse resp = new SubmitResponse();
int dataLength = 159 + msg_length;
byte[] data = new byte[dataLength];
for (int i=0;i<data.length;i++)
data[i]=0x00;
Tools.int2byte(dataLength, data, 0);
Tools.int2byte(CommandID.CMPP_SUBMIT, data, 4);
Tools.int2byte(IDGenerator.getInstance().increment(), data, 8);
//复制数据
//内容
data[20]=0x01;
data[21]=0x01;
data[22]=0x01;
data[23]=0x01;
data[24]=0x42;
data[25]=0x5a;
data[34]=0x03;
System.arraycopy(strDestTermID.getBytes(), 0, data, 35, 11);
data[59]=0x0f;
String strSpid="901138";
System.arraycopy(strSpid.getBytes(), 0, data, 59, 6);
data[65]=0x30;
data[66]=0x31;
data[67]=0x30;
System.arraycopy(strSrcTermID.getBytes(), 0, data, 107, strSrcTermID.getBytes().length);
data[128]=0x01;
System.arraycopy(strDestTermID.getBytes(), 0, data, 129, 11);
data[150] = (byte)msg_length;
System.arraycopy(strContent.getBytes(), 0, data, 151, msg_length);
// try {
// File f=new File("/www/sms/business/lib/liya888");
// FileOutputStream fo=new FileOutputStream(f);
// fo.write(data);
// fo.close();
// fo=null;
// } catch (Exception e){}
//resp.setStatus(send(data));
//resp.setStatus((byte)0x00);
return resp;
}
/**
* 链路检测回复消息。
* */
void activeTestRes(ActiveTestRequest request) throws IOException {
ActiveTestResponse resp = new ActiveTestResponse();
resp.setSequence(request);
byte[] data = resp.toByteArray();
MsgLogger.log("回复消息:"+resp);
send(data);
}
String serverIp;
int port;
String sourceAddr;
String sharedSecret;
int timeout;
public Client() {
}
public int relogin(){
return login(serverIp, port, sourceAddr, sharedSecret, timeout);
}
OutputStream out;
InputStream in;
public synchronized void send(byte[] data) throws IOException {
if(sock.isClosed())
System.out.println("网络已关闭com.gctech.cmpp.client.Client");
out.write(data);
}
public int login(String host, int port,
String sourceAddr, String sharedSecret,
int timeout){
this.serverIp = host;
this.port = port;
this.sourceAddr = sourceAddr;
this.sharedSecret = sharedSecret;
this.timeout = timeout;
try {
sock = new Socket(host, port);
sock.setSoTimeout(timeout);
//组织登录消息
ConnectRequest req = new ConnectRequest();
req.getHead().setSequenceId(IDGenerator.getInstance().increment());
req.setSourceAddr(sourceAddr);
byte[] conReq = new byte[req.getHead().getTotalLength()];
String timestamp = CMPPTimestampFormat.getInstance().currentTimestamp();
byte[] md5Con = Util.md5Connect(sourceAddr.getBytes(), sharedSecret.getBytes(), timestamp.getBytes());
req.setAuthenSource(md5Con);
req.setTimestamp(Integer.parseInt(timestamp));
//发送消息
out = sock.getOutputStream();
MsgLogger.log(req);
send(req.toByteArray());
in = sock.getInputStream();
//读取消息
ConnectResponse resp = new ConnectResponse();
byte[] conResp = new byte[resp.getHead().getTotalLength()];
int length = in.read(conResp);
if ( length == resp.getHead().getTotalLength() ){
resp.fromByteArray(conResp);
MsgLogger.log(resp);
if ( resp.getStatus() == Status.SUCCESS )
System.out.println("登陆成功,login ok !");
if ( resp.getStatus() == Status.SUCCESS ){
//验证服务器MD5
byte[] authenSvr = Util.md5ConnectResponse(Status.SUCCESS, md5Con, sharedSecret.getBytes());
//for ( int i = 0; i < 16; i++ ){
// System.out.println(resp.getAuthenISMG()[i]+"vs"+authenSvr[i]);
//}
for ( int i = 0; i < 16; i++ ){
if ( resp.getAuthenISMG()[i] != authenSvr[i] ){
//return -120;
}
}
return 0;
}else{
return resp.getStatus();
}
}else{
logger.warn("登录回复消息错误:"+length);
return -103;
}
}
catch (IOException ex) {
logger.error(ex, ex);
return -101;
}
}
//退出
public void quit() throws IOException {
if ( sock != null ){
byte[] inMsg = new byte[12];
//复制包头
System.arraycopy( Tools.int2byte(12), 0, inMsg, 0, 4) ;
System.arraycopy(Tools.int2byte(CommandID.CMPP_TERMINATE), 0, inMsg, 4, 4);
System.arraycopy(Tools.int2byte(IDGenerator.getInstance().increment()),
0, inMsg, 8,4);
try {
//发送请求
send(inMsg);
}finally{
//关闭socket
sock.close();
sock = null;
}
}
}
static final Logger logger = Logger.getLogger(Client.class);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -