📄 sdcncapi.java
字号:
package com.gctech.sms.gateway.cncsd;
import java.io.*;
import java.util.*;
import java.net.*;
import org.apache.log4j.Logger;
import com.gctech.sms.msg.*;
/**
* 负责向山东网关发送mt,以及接收山东网关mo
*
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author lijz@gctech.com.cn
* @version 1.0
*/
public class SDCNCAPI {
private static SDCNCAPI api = null;
Logger logger = Logger.getLogger(SDCNCAPI.class);
String ip;
int port;
OutputStream out = null;
InputStream in = null;
private static final byte[] MO_REQUEST = "GETMO\r\n".getBytes();
private Socket socket = null;
private SDCNCAPI() {
}
/**
*
* @param sdGateWayIp 山东网关ip
* @param MtPort 山东网关下行端口
* @param MoPort 山东网关上行端口
*/
public static void init(String sdGateWayIp, int port) throws IOException {
api = new SDCNCAPI();
api.ip = sdGateWayIp;
api.port = port;
api.connect();
//.....
}
public static SDCNCAPI instance() {
return api;
}
public int sendMt(MTInfo mtInfo) throws IOException {
try {
String result = DataConvert.convertMt(mtInfo);
if(result!=null)
{
byte[] bs = result.getBytes();
out.write(bs);
System.out.println("send:"+mtInfo);
}
return 0;
}
catch (IOException ioe) {
throw ioe;
}
}
public List receiveMo() throws IOException {
List list = new LinkedList();
try {
out.write(MO_REQUEST);
BufferedReader bfin = new BufferedReader(new InputStreamReader(in));
String line = null;
MOInfo info = null;
while ( (line = bfin.readLine()) != null) {
System.out.println(line);
if(line.startsWith("ST_OK"))
{
handleResponse(line);
}
info = DataConvert.convertMo(line);
if (info != null) {
list.add(info);
System.out.println("read line:" + line + ".");
}
}
return list;
}
catch (IOException ioe) {
throw ioe;
}
}
private void handleResponse(String line)
{
System.out.println("reveive response:"+line);
}
public void connect() throws IOException {
System.out.println("连接山东网关....");
socket = new Socket(ip, port);
socket.setKeepAlive(true);
socket.setSoTimeout(1000 * 180);
this.in = socket.getInputStream();
this.out = socket.getOutputStream();
System.out.println("连接山东网关....成功");
}
private void close() {
try {
in.close();
out.close();
socket.close();
}
catch (Exception ex) {
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -