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

321 lines
6.0 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 20:19:00 +08:00
{
2017-03-08 21:14:22 +08:00
String str=br.readLine();
if(str==null)
{
break;
}
Gson gson=new Gson();
AppJsonData data=null;
try
{
data=gson.fromJson(str, AppJsonData.class);
}
catch(Exception e)
{
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)
{
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+"'}");
}
break;
case "login":
break;
case "getpart":
break;
case "getfull":
break;
case "upload":
break;
}
2017-03-08 20:19:00 +08:00
}
2017-03-08 21:14:22 +08:00
2017-03-08 20:19:00 +08:00
String reply="Connection is OK";
pw.write(reply);
pw.flush();
}
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) {
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){
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 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();
}
}
}
*/