📄 sgipconnection.java
字号:
package com.tssx.ebiz.sgip;
import java.io.*;
import java.net.*;
/**
* <p>类名: SgipConnection</p>
* <p>功能: 与短信网关的连接</p>
* <p>版权: Copyright (c) 2002</p>
* <p>公司: 深讯信科</p>
* <p>版本: 1.0</p>
* @程序 xuke
* @修改纪录
*/
public class SgipConnection implements Runnable {
Socket m_sk;
DataOutputStream m_os;
// DataInputStream m_is;
String m_strHost;
int m_iPort;
// int seqAddr;
// int seqDate;
// int sequence;
AsynReader m_Reader;
/** 每隔m_iKeepAliveTimeGap就发送一个ENQUIRE_LINK,保持sgip连接 */
// int m_iKeepAliveTimeGap;
/** 负责保持连接的线程 */
// Thread m_thdKeepAlive;
public SgipConnection(int keepAliveTimeGap) {
super();
// m_iKeepAliveTimeGap = keepAliveTimeGap;
// m_thdKeepAlive = null;
m_sk = null;
}
public SgipConnection() {
this(20000);
}
public String getHost() {
return m_strHost;
}
public int getPort() {
return m_iPort;
}
public void connect(String strHost, int iPort) throws IOException {
m_sk = new Socket(strHost, iPort);
m_os = new DataOutputStream(m_sk.getOutputStream());
// m_is = new DataInputStream(new BufferedInputStream(m_sk.getInputStream()));
m_Reader = new AsynReader(m_sk.getInputStream());
//启动保持连接线程
// m_thdKeepAlive = new Thread(this, "SgipConnection Keep Alive Thread");
// m_thdKeepAlive.start();
}
public void close() {
// if (m_sk == null && m_is == null && m_os == null) {
// return;
// }
if (m_sk == null && m_Reader == null && m_os == null) {
return;
}
// m_thdKeepAlive = null;
try {
m_sk.close();
} catch (Exception e) {
}
m_sk = null;
// m_is = null;
m_Reader=null;
m_os = null;
}
public boolean isConnected() {
// if (m_sk != null && m_is != null && m_os != null) {
// return true;
// }
if (m_sk != null && m_Reader != null && m_os != null) {
return true;
}
close();
return false;
}
/**
* 与SMG建立一个发送/接受通道。
*/
public void bind(String loginName, String loginPassword,int loginType,String corpID) throws SgipException,IOException {
SMSBind smsbind=new SMSBind();
smsbind.getValue(corpID);
smsbind.setLoginName(loginName);
smsbind.setLoginPassword(loginPassword);
smsbind.setLoginType(loginType);
smsbind.write(m_os);
// SMSBindResp smsbindresp=new SMSBindResp();
// long beginTime=System.currentTimeMillis();
// while(true){
// if(m_is.available()>0){
// smsbindresp.read(m_is);
// break;
// }else{
// if(System.currentTimeMillis()-beginTime>=30000L){
// throw new SgipException();
// }
// }
// }
SMSBindResp smsbindresp=(SMSBindResp) m_Reader.blockRead(CommandID.SGIP_BIND_RESP,
smsbind.seqAddr,smsbind.seqDate,smsbind.sequence);
if(smsbindresp.getResult()!=0){
throw new SgipException();
}
}
/**
* 与SP建立一个发送/接受通道。
*/
/**
* 与SMG建立一个发送通道。
*/
public void bindTransmitter(String loginName, String loginPassword,String corpID) throws SgipException,IOException {
SMSBindTransmitter smsbindtransmitter=new SMSBindTransmitter();
smsbindtransmitter.getValue(corpID);
smsbindtransmitter.setLoginName(loginName);
smsbindtransmitter.setLoginPassword(loginPassword);
smsbindtransmitter.write(m_os);
// SMSBindTransmitterResp smsbindtransmitterresp=new SMSBindTransmitterResp();
// long beginTime=System.currentTimeMillis();
// while(true){
// if(m_is.available()>0){
// smsbindtransmitterresp.read(m_is);
// break;
// }else{
// if(System.currentTimeMillis()-beginTime>=30000L){
// throw new SgipException();
// }
// }
// }
// if(smsbindtransmitterresp.getResult()!=0){
// throw new SgipException();
// }
SMSBindResp smsbindresp=(SMSBindResp) m_Reader.blockRead(CommandID.SGIP_BIND_RESP,
smsbindtransmitter.seqAddr,smsbindtransmitter.seqDate,smsbindtransmitter.sequence);
if(smsbindresp.getResult()!=0){
throw new SgipException();
}
}
/**
* 与SMG建立一个接受通道。
*/
public void bindReceiver(String loginName, String loginPassword,String corpID) throws SgipException,IOException {
SMSBindReceiver smsbindreceiver=new SMSBindReceiver();
smsbindreceiver.getValue(corpID);
smsbindreceiver.setLoginName(loginName);
smsbindreceiver.setLoginPassword(loginPassword);
smsbindreceiver.write(m_os);
// SMSBindReceiverResp smsbindreceiverresp=new SMSBindReceiverResp();
// long beginTime=System.currentTimeMillis();
// while(true){
// if(m_is.available()>0){
// smsbindreceiverresp.read(m_is);
// break;
// }else{
// if(System.currentTimeMillis()-beginTime>=30000L){
// throw new SgipException();
// }
// }
// }
// if(smsbindreceiverresp.getResult()!=0){
// throw new SgipException();
// }
SMSBindResp smsbindresp=(SMSBindResp) m_Reader.blockRead(CommandID.SGIP_BIND_RESP,
smsbindreceiver.seqAddr,smsbindreceiver.seqDate,smsbindreceiver.sequence);
if(smsbindresp.getResult()!=0){
throw new SgipException();
}
}
/**
* 关闭已与SMG建立的通道。
*/
public void unBind(String corpID) throws SgipException,IOException {
SMSUnBind smsunbind=new SMSUnBind();
smsunbind.getValue(corpID);
smsunbind.write(m_os);
// SMSUnBindResp smsunbindresp=new SMSUnBindResp();
// long beginTime=System.currentTimeMillis();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -