📄 ismgmain.java
字号:
package com.zznode.dp.ismg;
import java.net.Socket;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.net.ServerSocket;
import com.zznode.dp.ismg.data.*;
import com.zznode.dp.ismg.util.*;
import com.zznode.dp.ismg.data.*;
import com.zznode.dp.ismg.sender.*;
public class ISMGMain {
/**实例化日志操作对象*/
private static Log log = null;
private static DataParser dataParser = null;
private static ReadConfig readConfig = null;
private static ConnectionModel connectionModel = null;
private static SubmitMessageModel submitmMessageModel = null;
private static Send2HuaWei send2HuaWei = null;
private static Send2DongRuan send2DongRuan = null;
public ISMGMain() {
log = new Log().getLog();
dataParser = new DataParser().getDataParser();
readConfig = new ReadConfig("config.xml").getReadConfig();
connectionModel = readConfig.getConnectionModel();
submitmMessageModel = readConfig.getSubmitMessageModel();
send2HuaWei = new Send2HuaWei(connectionModel, submitmMessageModel);
send2DongRuan = new Send2DongRuan(connectionModel, submitmMessageModel);
}
public void runServer(int PORT) {
ServerSocket sock;
Socket clientSocket;
try {
sock = new ServerSocket(PORT);
System.out.println("Server started...");
//waiting for connection that comes from any client
while (true) {
clientSocket = sock.accept();
//new thread and start it
new Handler(clientSocket).start();
}
}
catch (IOException e) {
System.err.println("Could not accept" + e);
}
}
/*
* Class Handler(subClass of Thread)
*
*dealing with the conversation which Server and Client will be going on
* @author weich
*
*/
class Handler
extends Thread {
Socket socket;
Handler(Socket socket) {
this.socket = socket;
}
public void run() {
String reponse = "";
final int MAX_BUF_SIZE = 1024 * 4;
byte[] buf = new byte[MAX_BUF_SIZE];
int bufstart = 0;
int bufend = 0;
int size = 0;
try {
System.out.println("Socket starting: " + socket);
InputStream in = socket.getInputStream();
PrintWriter os = new PrintWriter(new OutputStreamWriter(socket.
getOutputStream()), true);
while (size != -1) {
//取得大小,并把“_sock.getInputStream()”中的数据流读入"buf"
size = in.read(buf, bufstart, MAX_BUF_SIZE - bufstart);
if (size < 0) {
break;
}
bufend = bufstart + size;
//处理消息串
String line = new String(buf, bufstart, bufend).trim();
log.writeLog("\n---------------------------------------------");
log.writeLog("服务器端接收到的消息是:" + line);
/**处理接收到的信息*/
MessageModel[] messageModelArry = dataParser.parseMessage(line);
if (connectionModel.getName() != null &&
connectionModel.getName().equalsIgnoreCase("huawei") &&
connectionModel.getUsing_status() != null &&
connectionModel.getUsing_status().equalsIgnoreCase("true")) {
reponse = send2HuaWei.sendSms(messageModelArry);
}
else if (connectionModel.getName() != null &&
connectionModel.getName().equalsIgnoreCase("dongruan") &&
connectionModel.getUsing_status() != null &&
connectionModel.getUsing_status().equalsIgnoreCase("true")) {
reponse = send2DongRuan.sendSms(messageModelArry);
}
/**返回的消息字符串*/
log.writeLog("服务器端返回的消息是:" + reponse);
log.writeLog("---------------------------------------------\n");
os.println(reponse);
os.flush();
}
}
catch (IOException ex) {
System.out.println("IO Error on socket " + ex);
log.writeLog(ex.getMessage());
ex.printStackTrace();
}
}
}
static public void main(String[] argv) {
if (argv.length != 1) {
System.err.println(
"port not null!");
}
int PORT = Integer.parseInt(argv[0]);
new ISMGMain().runServer(PORT);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -