关于java socket一个非常简单的项目,服务端代码我这样写可以吗?
需求很简单:
客户端发送一个十六进制的字符串给服务端,服务端把他变成十进制,然后取前面4位数(剩余的不要了)作为userId,去sqlserver相关表里面查找(sqlserver数据库相关表里面的字段是userId,userLabel),得到相应的userLabel,然后将他存入oracle数据库中,很简单吧!
本人刚学java socket希望高人能凭经验告诉我的server端代码存在的问题:
如果觉得我的代码太臭了,你就狠狠的骂我吧!!!
Server端代码:
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(); }}
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.Socket;import java.net.UnknownHostException;public class Client { private static Socket client; private static final int SERVER_PORT = 10000; private static BufferedReader in; private static PrintWriter out; private static String messageFromServer; private static String sysMessage; public Client() throws UnknownHostException, IOException { client = new Socket("127.1.0.0", SERVER_PORT);// 192.168.1.103 BufferedReader sin = new BufferedReader(new InputStreamReader(System.in)); in = new BufferedReader(new InputStreamReader(client.getInputStream())); out = new PrintWriter(client.getOutputStream(), true); System.out.println(in.readLine()); while (!(sysMessage = sin.readLine()).equals("Quit")) { out.println(sysMessage); this.messageFromServer = in.readLine(); System.out.println(this.messageFromServer); } in.close(); out.close(); sin.close(); client.close(); } public static void main(String args[]) { try { new Client(); } catch (Exception e) { e.printStackTrace(); } }}