Update 'BookFloatingServer.java'

This commit is contained in:
Kirigaya Kazuto 2017-03-08 20:19:00 +08:00
parent 858ac00c26
commit 99519f915a

View File

@ -1,5 +1,58 @@
import java.sql.*;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_ADDPeer;
import java.io.*;
import java.net.*;
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);
String str;
while(!((str=br.readLine())==null))
{
System.out.println("Receive Message: "+str);
}
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.");
}
}
public class BookFloatingServer
{
public static int Login(String Email,String Password) {
@ -92,6 +145,7 @@ public class BookFloatingServer
}
public static void main(String[] args) {
/*
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
@ -102,6 +156,48 @@ public class BookFloatingServer
/// 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();
}
}
}