From 99519f915a278f3caba0f7310b0733f687943024 Mon Sep 17 00:00:00 2001 From: Kiritow <1362050620@qq.com> Date: Wed, 8 Mar 2017 20:19:00 +0800 Subject: [PATCH] Update 'BookFloatingServer.java' --- BookFloatingServer.java | 96 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) diff --git a/BookFloatingServer.java b/BookFloatingServer.java index bce95fc..a82e02d 100644 --- a/BookFloatingServer.java +++ b/BookFloatingServer.java @@ -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(); + } + } }