📄 multiuser.java
字号:
/************************************************************************************************************************
* 头文件定义区
*
*************************************************************************************************************************/
import java.net.*;
import java.io.*;
import java.util.Date;
import java.util.Calendar;
import java.util.Hashtable;
import java.util.Vector;
import net.shareinfo.dxw.SmsSender;
import net.shareinfo.dxw.exceptions.SmkException;
/*********************************************************************************************************************
* 类名: MultiUser
功能:接收ALINK发送的短信内容并向网通转发
* 编写人: 张明
日期: 20060425
*
*
**********************************************************************************************************************/
public class MultiUser extends Thread
{
private ServerSocket server = null;
private Socket client = null;
private String str_message = null;
private String str_Message =null;
private String str_Mobile = null;
private String str_Query = null;
private boolean success_or_false = false ;
private boolean exit_bool;
public MultiUser()
{
exit_bool = false;
}
public void run()
{
try
{
server= new ServerSocket(8005);
while(true)
{
System.out.println("java 在端口在等待alink连接!");
this.client=server.accept();
//BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()) );
DataInputStream in = new DataInputStream(client.getInputStream());
DataOutputStream out = new DataOutputStream(client.getOutputStream());
//服务器端读去客户端的数据
while(true)
{
int read_len = 0 ;
int message_len = 0;
byte[] b = new byte[4];
read_len = in.read(b,0,4);
if (read_len ==-1)
{
System.out.println(" alink 已退出连接");
loginWangTong.writeLog(" alink 已退出连接");
break ;
}
String str1 = null;
str1 = new String(b);
message_len = Integer.parseInt(str1);
if(message_len<0||message_len>300)
{
client.close();
System.out.println(" java与alink 连接已断开 正在重新连接");
loginWangTong.writeLog("java与alink 连接已断开 正在重新连接");
break ;
}
read_len = 0 ;
b = new byte[message_len-4];
read_len = in.read(b);
str1 = new String(b);
if (read_len ==-1)
{
System.out.println(" alink 已退出连接");
loginWangTong.writeLog(" alink 已退出连接");
break ;
}
if(str1==null)
{
client.close();
System.out.println(" java与alink 连接已断开 正在重新连接");
loginWangTong.writeLog("java与alink 连接已断开 正在重新连接");
break ;
}
str_message = str1.trim();
if (str_message.equals("ARE YOU OK"))
{
String outStr = "OK";
out.write(outStr.getBytes(),0,outStr.length());
out.flush();
System.out.println("alink发送的是ARE YOU OK 返回的是OK");
}
else if(str_message.equals("TEST"))
{
String outStr = "OK";
out.write(outStr.getBytes(),0,outStr.length());
out.flush();
System.out.println("alink发送的是TEST 返回的是OK");
}
else
{
if(str_message.length()>=27)
{
String str_Mobile1 = null , str_Query1 = null , str_Message1 = null;
str_Mobile1 = str_message.substring(0,21);
str_Query1 = str_message.substring(21,27);
str_Message1 = str_message.substring(27,str_message.length());
str_Mobile = str_Mobile1.trim();
str_Query = str_Query1.trim();
str_Message = str_Message1.trim();
//System.out.println("str_Mobile"+str_Mobile1+"str_Mobilelenght():"+str_Mobile1.length());
//System.out.println("str_Query"+str_Query1 +" " +str_Query1.length());
//System.out.println("str_Message"+str_Message1+" " + str_Message1.length());
//loginWangTong.write_No_Send_Message(str_message);
if(!(success_or_false = sendMessage(str_Message,str_Query,str_Mobile))) //张明
{
loginWangTong.write_No_Send_Message(str_message);
}
}
}
if (exit_bool)
{
server.close();
break;
}
}
if(!client.isClosed())
{
client.close();
}
if(exit_bool)
{
if(!server.isClosed())
server.close();
break;
}
}
}
catch(IOException ioe )
{
try
{
if(!client.isClosed())
client.close();
if(!server.isClosed())
server.close();
}
catch(IOException e)
{
}
System.out.println("alink java 移动接收线程错误");
loginWangTong.writeLog("\tjava与alink 移动接收线程错误,线程已停止 正在重新连接");
}
finally {
if (!(server.isClosed()))
{
try
{
if(!server.isClosed())
server.close();
}
catch(IOException e)
{
}
}
}
}
public static void main(String[] args)throws IOException
{
RemainAction action = null;
MultiUser mu = null;
int i = 0 ;
loginWangTong login1 = new loginWangTong() ; // 注册类向网通进行初始注册
if (login1.login())
{
System.out.println(" java system login success");
action = new RemainAction(); //每格3.5分钟自动向网通查询状态
action.start();
mu= new MultiUser(); //一直在监听8005端口并转发报文
mu.start();
Xiao_Ling_Tong xiao_ling_tong = new Xiao_Ling_Tong();
xiao_ling_tong.start();
while(true)
{
try
{
sleep(120000);
loginWangTong.writeLog("\tjava 正在检查各个线程的状态");
System.out.println("java 正在检查各个线程的状态");
if(!(action.isAlive()))
{
mu.sleep(100);
xiao_ling_tong.sleep(100);
login1 = new loginWangTong() ;
if(!(login1.login()))
{
sleep(240000);
continue;
}
action = new RemainAction(); //每格3.5分钟自动向网通查询状态
action.start();
}
if(!(mu.isAlive()))
{
xiao_ling_tong.sleep(100);
action.sleep(100);
login1 = new loginWangTong() ;
if(!(login1.login()))
{
sleep(240000);
continue;
}
mu= new MultiUser(); //一直在监听8005端口并转发报文
mu.start();
}
if(!(xiao_ling_tong.isAlive()))
{
mu.sleep(100);
action.sleep(100);
login1 = new loginWangTong();
if(!(login1.login()))
{
sleep(240000);
continue;
}
xiao_ling_tong = new Xiao_Ling_Tong();
xiao_ling_tong.start();
}
i = i + 2 ;
if(i == 60)
{
i = 0 ;
if(loginWangTong.read_No_Send_Message())
{
System.out.println("正在创建文件");
try
{
File write_file = null;
String str_file = new String("No_Send_Mobile_Message"+".txt");
write_file = new File(".\\log\\No_Send_Message\\"+str_file);
write_file.delete();
write_file.createNewFile();
}
catch(IOException e)
{
; //loginWangTong.writeLog(" 创建新文件出错");
}
}
else
{
;
}
}
}
catch(InterruptedException e)
{
System.out.println("\t向网通注册失败:在重新注册时被异常中断java 退出");
loginWangTong.writeLog("\t向网通注册失败:在重新注册时被异常中断java 退出");
System.exit(1);
}
}
}
else
{
System.out.println("向网通第一次注册失败,java已停止运行 请重新启动 java");
loginWangTong.writeLog("向网通第一次注册失败,java已停止运行 请重新启动 java");
}
}
/***************************************************************************************************************
****************************************************************************************************************/
public void MultiUser_exit()
{
exit_bool = true ;
}
/**********************************************************************************************************************
* 函数名: sendMessage()
* 功能: 组织报文在通过SmsSender类(由网通提供)将其转发
* 编写者: 张明
***********************************************************************************************************************/
public static synchronized boolean sendMessage(String str_Message,String str_Query,String str_Mobile)
{
Date nowDate = new Date();
Hashtable objMobile = new Hashtable();
//Hashtable hashtable_recive = null;
objMobile.put("mobile",str_Mobile);
objMobile.put("smsId",str_Query);
Vector objMobiles = new Vector();
//Vector obj_Recive = null;
objMobiles.add(objMobile);
try
{
loginWangTong.smsSender.sendMsg(str_Message, nowDate ,objMobiles); //张明
/*obj_Recive = loginWangTong.smsSender.getStatusReport();
if(obj_Recive == null)
{
System.out.println("向网通发送短信失败 网通返回的数据为空 obj_Recive = null");
loginWangTong.writeLog("向网通发送短信失败 网通返回的数据为空 obj_Recive = null");
return true;
}
if(obj_Recive.isEmpty())
{
System.out.println("向网通发送短信失败的内容是:"+str_Message+"手机号是:"+str_Mobile);
loginWangTong.writeLog("向网通发送短信失败 网通返回的数据为空");
loginWangTong.writeLog("向网通发送短信失败的内容是:"+str_Message+"手机号是:"+str_Mobile);
//return false;
}
else
{
loginWangTong.writeLog("网通返回的数据是: "+"[" + "]" );
hashtable_recive = (Hashtable)obj_Recive.lastElement();
String state = (String)hashtable_recive.get("smsId");
System.out.println("网通返回的数据是: "+"[" + state + "]" );
loginWangTong.writeLog("网通返回的数据是: "+"[" + state + "]" );
if(!(state.equalsIgnoreCase("10")))
{
System.out.println("向网通发送短信失败的内容是:"+str_Message+"手机号是:"+str_Mobile);
loginWangTong.writeLog("向网通发送短信失败的内容是:"+str_Message+"手机号是:"+str_Mobile);
//return false;
}
}*/
System.out.println("短消息向网通发送成功");
loginWangTong.writeLog("\t成功发送的内容是:\t"+str_Message+"\t手机号是:"+str_Mobile);
System.out.println("\t成功发送的内容是:\t"+str_Message+"\t手机号是:"+str_Mobile);
return true;
}
catch(SmkException e)
{
System.out.println("向网通发送短信失败");
loginWangTong.writeLog("\t向网通发送短信失败!!! \t发送的的内容是:\t"+str_Message+"\t手机号是:"+str_Mobile);
return false;
}
}
}
/************************************************************************************************************************
* 类名:loginWangTong
功能 : 向网通发送注册码
包括的函数:login() ; readfile(), smkDemoLogin(String pwd, String url, String LoginCode)
* 编写人:张明
* 时间:20060425
************************************************************************************************************************/
class loginWangTong
{
public static SmsSender smsSender = null;
boolean successorfalse = false ;
byte[] passwd = new byte[50];
byte[] username = new byte[50];
byte[] addr = new byte[100];
String str_username = null;
String str_passwd = null;
String str_addr = null;
public loginWangTong()
{
smsSender = null;
}
/*login()函数*/
public boolean login()
{
if((successorfalse = readfile()))
{
if(smkDemoLogin(str_addr, str_username, str_passwd))
{
System.out.println("\t调用网通的注册函数向网通注册成功");
}
else
{
System.out.println("向网通注册失败");
writeLog("\t调用网通的注册函数向网通注册失败");
successorfalse = false;
}
}
else
{
successorfalse = false;
}
return successorfalse;
}
/** readfile 函数 **/
public boolean readfile()
{
boolean successorfalse = false ;
FileInputStream infile = null;
// 生成对象infile 准备读取文件
try
{
infile = new FileInputStream("..\\init\\init.txt");
}
catch (FileNotFoundException e)
{
System.err.println("java 没有找到 init.txt文件,请查看javatest目录下是否有此文件");
writeLog("\t java 没有找到 init.txt文件,请查看javatest目录下是否有此文件");
System.exit(1);
}
try
{
int n = 0 , i = 0; //n为文件结尾判断,i为读取的行数
int usernamelong=0 ,passwdlong = 0, addrlong = 0;
while(((n= infile.read())!= -1))
{
switch (i)
{
case 0 :
if(usernamelong>=50)
break;
username[usernamelong++] = (byte) n;
break;
case 1:
if(passwdlong>=50)
break;
passwd[passwdlong++] = (byte) n;
break;
case 2:
if(addrlong>=100)
break;
addr[addrlong++] = (byte) n;
break;
}
if(((char) n =='\n'))
i++;
}
String str_username1 = new String(username);
str_username = str_username1.trim();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -