?? 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 + -