日期:2014-05-20 浏览次数:20811 次
import java.io.*; import java.math.BigInteger; import java.net.*; import java.sql.SQLException; import org.db.factory.DAOFactory; import org.oracle.dao.ICM_T_SCALE_MESSAGEDAO; import org.oracle.dbc.DBConnection_Oracle; import org.sqlserver.dao.IStudioDAO; import org.sqlserver.dbc.DBConnection; import org.sqlserver.exception.StudioNotFoundException; import org.sqlsever.vo.Studio; public class Server extends ServerSocket { private static final int SERVER_PORT = 10000; public Server() throws IOException { super(SERVER_PORT); try { while (true) { Socket socket = accept(); new CreateServerThread(socket); } } catch (IOException e) { } finally { close(); } } // --- CreateServerThread class CreateServerThread extends Thread { private Socket client; private BufferedReader in; private PrintWriter out; private String userIdFilter = null; private String decimalism; private IStudioDAO studioDAO = null; private ICM_T_SCALE_MESSAGEDAO cm_t_scale_messagedao = null; private Studio studio = null; private DBConnection conn = null; private DBConnection_Oracle conn_oracle = null; private int id; public CreateServerThread(Socket s) throws IOException { client = s; studioDAO = DAOFactory.getStudioDAOInstance(); cm_t_scale_messagedao = DAOFactory.getCM_T_SCALE_MESSAGEDAOInstance(); conn_oracle = cm_t_scale_messagedao.getDBConnection_Oracle(); conn = studioDAO.getDBConnection(); in = new BufferedReader(new InputStreamReader(client.getInputStream(), "GB2312")); out = new PrintWriter(client.getOutputStream(), true); out.println("--- Input Hexadecimal (Input 'bye' to quit)---"); start(); } public void run() { try { String messageFromClient = in.readLine(); while (!messageFromClient.equals("bye")) { if (!isHexadecimal(messageFromClient)) { // out.println("不满足十六进制输入格式,请重新输入。正确的输入字符范围为[a-fA-F0-9]!"); out.println("This is not the correct Hexadecimal,please try again.You are called to" + "input the charactor which in [a-fA-F0-9]"); } else { this.decimalism = this.change_H_To_D(messageFromClient); if (!this.isChangeAble(this.decimalism)) { // out.println("该十六进制转换为十进制后不足四位数!请重新输入!"); out.println("The Hexadecimal you input is not 4 byte when it translated " + "to decimalism,please try again!"); } else { this.userIdFilter = this.decimalism.substring(0, 4); try { this.studio = studioDAO.findById(this.userIdFilter); System.out.println("数据查找结果:"); System.out.println("UserLabel:" + studio.getUserLabel() + " " + "id:" + studio.getUserId() + " " + "status:" + studio.getStatus()); this.id = Integer.parseInt(this.studio.getUserLabel()); this.cm_t_scale_messagedao.doInsert(this.id); System.out.println("插入" + this.id + "完成!"); out.println("Hexadecimal-->Decimalism:" + this.decimalism + "=" + this.userIdFilter + "+" + this.decimalism.substring(4, this.decimalism.length())); } catch (NumberFormatException e) { // exceptionInfo += "字符串转化为Int型出错!"; out.println("An error occured when String transform to Int!"); } catch (StudioNotFoundException e) { // exceptionInfo += "发生不明错误,请重新输入!"; out.println("Could not find the studio which you want!"); } catch (SQLException e) { out.println("DB operat error! Maybe occured in insert!"); } } } messageFromClient = in.readLine(); } } catch (IOException e) { }finally{ out.println("--- See you, bye! ---"); conn.close(); this.conn_oracle.close(); out.close(); try { in.close(); } catch (IOException e) { e.printStackTrace(); } try { client.close(); } catch (IOException e) { e.printStackTrace(); } } } private String change_H_To_D(String messageFromClient) { String d = new BigInteger(messageFromClient, 16).toString(); return d; } private boolean isHexadecimal(String message) { if (!message.matches("[a-fA-F0-9]+$")) { return false; } else { return true; } } private boolean isChangeAble(String decimalism) { if (decimalism.length() >= 4) { return true; } else { return false; } } } public static void main(String[] args) throws IOException { new Server(); } }