This repository has been archived on 2021-11-25. You can view files and clone it, but cannot push or open issues or pull requests.
BookFloatingServer/BookFloatingServer.java

419 lines
8.2 KiB
Java
Raw Normal View History

2017-03-06 21:47:55 +08:00
import java.sql.*;
2017-03-08 20:19:00 +08:00
import java.io.*;
import java.net.*;
2017-03-08 21:14:22 +08:00
import com.google.gson.Gson;
2017-03-08 20:19:00 +08:00
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);
2017-03-08 21:14:22 +08:00
while(true)
2017-03-08 22:09:00 +08:00
{
byte[] buf = new byte[1024];
int len =is.read(buf);
if(len < 0){
2017-03-08 21:14:22 +08:00
break;
}
2017-03-08 22:09:00 +08:00
String str = new String(buf,0,len);
str=new String(str.trim());
2017-03-08 22:09:00 +08:00
System.out.println("String is: "+str);
2017-03-08 21:14:22 +08:00
Gson gson=new Gson();
AppJsonData data=null;
try
{
data=gson.fromJson(str, AppJsonData.class);
}
catch(Exception e)
{
2017-03-08 22:09:00 +08:00
e.printStackTrace();
2017-03-08 21:14:22 +08:00
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)
{
2017-03-08 22:09:00 +08:00
e.printStackTrace();
2017-03-08 21:14:22 +08:00
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+"'}");
2017-03-08 21:27:36 +08:00
pw.flush();
2017-03-08 21:14:22 +08:00
}
break;
case "login":
2017-03-08 21:27:36 +08:00
{
LoginData login=null;
try
{
login=gson.fromJson(str, LoginData.class);
}
catch(Exception e)
{
2017-03-08 22:09:00 +08:00
e.printStackTrace();
2017-03-08 21:27:36 +08:00
System.out.println("Incorrect Login Data");
break;
}
int ret=BookFloatingServer.Login(login.getEmail(),login.getPass());
pw.write("{'ret':'"+ret+"'}");
pw.flush();
}
break;
2017-03-08 21:14:22 +08:00
case "getpart":
{
}
break;
2017-03-08 21:14:22 +08:00
case "getfull":
{
}
break;
2017-03-08 21:14:22 +08:00
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");
2017-03-08 21:14:22 +08:00
break;
}
2017-03-08 20:19:00 +08:00
}
}
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.");
}
}
2017-03-06 21:47:55 +08:00
public class BookFloatingServer
{
public static int Login(String Email,String Password) {
System.out.println("*Database*: Login with "+Email+" "+Password);
2017-03-06 21:47:55 +08:00
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);
2017-03-06 21:47:55 +08:00
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;
}
2017-03-06 21:47:55 +08:00
public static void main(String[] args) {
2017-03-08 20:19:00 +08:00
/*
2017-03-06 21:47:55 +08:00
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);
2017-03-08 20:19:00 +08:00
*/
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();
}
2017-03-06 21:47:55 +08:00
}
}
/*
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();
}
}
}
*/