import java.sql.*; import java.io.*; import java.net.*; import com.google.gson.Gson; class Handle implements Runnable { private Socket s; public Handle(Socket socket) { s=socket; } public void run() { System.out.println("New Connection."); InputStream is=null; BufferedReader br=null; OutputStream os=null; PrintWriter pw=null; try { is=s.getInputStream(); br=new BufferedReader(new InputStreamReader(is)); os=s.getOutputStream(); pw=new PrintWriter(os); while(true) { byte[] buf = new byte[1024]; int len =is.read(buf); if(len < 0){ break; } String str = new String(buf,0,len); str=new String(str.trim()); System.out.println("String is: "+str); Gson gson=new Gson(); AppJsonData data=null; try { data=gson.fromJson(str, AppJsonData.class); } catch(Exception e) { e.printStackTrace(); System.out.println("Receive a incorrect json string."); continue; } System.out.println("AppJsonData:"+data.getType()); switch(data.getType()) { case "register": { RegisterData reg=null; try { reg=gson.fromJson(str, RegisterData.class); } catch(Exception e) { e.printStackTrace(); System.out.println("Incorrect Register Data"); break; } int ret=BookFloatingServer.Register(reg.getEmail(), reg.getPass(), reg.getNickname(), reg.getSchool()); System.out.println("Register Finished. Ret("+ret+")"); pw.write("{'ret':'"+ret+"'}"); pw.flush(); } break; case "login": { LoginData login=null; try { login=gson.fromJson(str, LoginData.class); } catch(Exception e) { e.printStackTrace(); System.out.println("Incorrect Login Data"); break; } int ret=BookFloatingServer.Login(login.getEmail(),login.getPass()); pw.write("{'ret':'"+ret+"'}"); pw.flush(); } break; case "getpart": { } break; case "getfull": { } break; case "upload": { UploadData up=null; try { up=gson.fromJson(str,UploadData.class); } catch(Exception e) { e.printStackTrace(); System.out.println("Incorrect Upload Data"); continue; } int ret=BookFloatingServer.Upload(up.getBookname(),up.getAuthor(),up.getContext(),up.getImageURL()); pw.write("{'ret':'"+ret+"'}"); pw.flush(); } break; default: System.out.println("Unknown Type"); break; } } } catch(Exception e) { e.printStackTrace(); } finally { try{pw.close();}catch(Exception e){e.printStackTrace();} try{os.close();}catch(Exception e){e.printStackTrace();} try{br.close();}catch(Exception e){e.printStackTrace();} try{is.close();}catch(Exception e){e.printStackTrace();} try{s.close();}catch(Exception e){e.printStackTrace();} } System.out.println("Connection Lost."); } } public class BookFloatingServer { public static int Login(String Email,String Password) { System.out.println("*Database*: Login with "+Email+" "+Password); Connection conn = null; String url="jdbc:mysql://gameharbor.cn:3306/test"; Statement stmt = null; ResultSet rs = null; int ret=0; String RemotePassword = new String(); try{ conn=DriverManager.getConnection(url,"testuser","pwdtest"); stmt=conn.createStatement(); rs=stmt.executeQuery("SELECT pwass FROM user where email=\""+Email+"\""); if(rs.next()) { RemotePassword=rs.getString(1); } if(RemotePassword.equals(Password)) { ret=1;///Password Matchs } else { ret=0;///Password Does not Matchs } } catch(SQLException e) { e.printStackTrace(); ret=-1;// Connection Error } finally { try{ if(rs!=null) rs.close(); } catch(SQLException e) { e.printStackTrace(); } try{ if(stmt!=null) stmt.close(); } catch(SQLException e){ e.printStackTrace(); } try{ if(conn!=null) conn.close(); } catch(SQLException e) { e.printStackTrace(); } } return ret; } public static int Register(String Email,String Password,String Nickname,String School){ System.out.println("*Database*:Register with "+Email+" "+Password+" "+Nickname+" "+School); int upret=-1; Connection conn=null; String url="jdbc:mysql://gameharbor.cn:3306/test"; PreparedStatement stmt = null; try{ conn=DriverManager.getConnection(url,"testuser","pwdtest"); String sqlString=new String("INSERT into test.user VALUES (?,?,?,?,?,?)"); stmt=conn.prepareStatement(sqlString,Statement.RETURN_GENERATED_KEYS); stmt.setString(1,Email); stmt.setString(2, Password); stmt.setString(3, Nickname); stmt.setString(4, School); stmt.setString(5, ""); stmt.setString(6, ""); upret=stmt.executeUpdate(); } catch(SQLException e) { e.printStackTrace(); } finally { try { if(stmt!=null) stmt.close(); } catch(SQLException e) { e.printStackTrace(); } try{ if(conn!=null) conn.close(); } catch(SQLException e) { e.printStackTrace(); } } return upret; } public static int Upload(String Bookname,String Author,String Context,String ImageURL){ System.out.println("*Database*: Upload with "+Bookname+" "+Author+" "+Context+" "+ImageURL); int ret=-1; Connection conn=null; String url="jdbc:mysql://gameharbor.cn:3306/test"; PreparedStatement stmt = null; try { conn=DriverManager.getConnection(url,"testuser","pwdtest"); String sqlString=new String("INSERT into test.book VALUES (?,?,?,?,?)"); stmt=conn.prepareStatement(sqlString,Statement.RETURN_GENERATED_KEYS); stmt.setString(1, Bookname); stmt.setString(2,Author); stmt.setString(3,"Provider Here"); stmt.setString(4, ImageURL); stmt.setString(5, Context); ret=stmt.executeUpdate(); } catch(SQLException e) { e.printStackTrace(); } finally { try { if(stmt!=null) stmt.close(); } catch(SQLException e) { e.printStackTrace(); } try{ if(conn!=null) conn.close(); } catch(SQLException e) { e.printStackTrace(); } } return ret; } public static FetchInfo Fetch() { FetchInfo ff=null; return ff; } public static void main(String[] args) { /* try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); System.exit(-1); } /// ret=Login("test2@me.com","030201"); int ret=Register("test3@me.com","030201","LiuTongYuan","QUST"); System.out.println("Ret is "+ret); */ System.out.println("Program Started."); ServerSocket ss=null; try { ss=new ServerSocket(55555); } catch(IOException e) { e.printStackTrace(); System.out.println("Error while create ServerSocket."); System.exit(0); } while(true) { Socket s=null; try { s=ss.accept(); Thread workThread=new Thread(new Handle(s)); workThread.start(); } catch(Exception e) { e.printStackTrace(); System.out.println("Error while starting a new connection."); break; } } try { ss.close(); } catch(Exception e) { e.printStackTrace(); } } } /* import java.io.*; import java.net.*; public class BookFloatingServer { public class ServerThread implements Runnable { Socket s=null; BufferedReader br=null; public ServerThread(Socket s) { System.out.println("Thread Ready, ID="+Thread.currentThread().getId()); this.s=s; try { br=new BufferedReader( new InputStreamReader( s.getInputStream()) ); } catch(Exception e) { e.printStackTrace(); return; } } public void run() { System.out.println("Thread Start, ID="+Thread.currentThread().getId()); try { s.close(); } catch(Exception e) { e.printStackTrace(); } System.out.println("Thread Over, ID="+Thread.currentThread().getId()); } } public static void main(String[] args) { ServerSocket ss; try { ss=new ServerSocket(50001); } catch(Exception e) { e.printStackTrace(); return; } while(true) { Socket s; try { s=ss.accept(); } catch(Exception e) { e.printStackTrace(); continue; } new Thread(new ServerThread(s)).start(); } } } */