⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threadedechoserver.java

📁 一个服务器JAVA
💻 JAVA
字号:
/* * Based on an example from Horstman & Cornell, * Core Java, 2 vols., Prentice Hall 2002, 2001. * */import java.io.*;import java.net.*;public class ThreadedEchoServer{   // the server socket:   private static ServerSocket ss;   // set when shutDown() is called to stop the server:   private static boolean shutDownCalled = false;   // shut the server down by closing the server socket   public static void shutDown()   {      // flag that the server socket has been closed      shutDownCalled = true;      try       {         ss.close();      }      catch (Exception e)      {         // something went wrong; give data:         System.err.println("problem shutting down:");         System.err.println(e.getMessage());         // and trust the JVM to clean up:         System.exit(1);      }   }   public static void main(String[] args )    {      Socket incoming;      Thread t;      try       {         ss = new ServerSocket(8189);         while (true)         {            incoming = ss.accept();            t = new Thread(new EchoHandler(incoming));            t.start();         }      }      catch (SocketException se)      {         /*          * will be thrown when accept() is called after closing          * the server socket, in method shutDown().          * If shutDownCalled, then simply exit; otherwise,          * something else has happened:          */         if (! shutDownCalled)         {            System.err.println("socket problem:");            System.err.println(se.getMessage());            System.exit(1);         }      }      catch (IOException ioe)      {         System.err.println("I/O error:");         System.err.println(ioe.getMessage());         System.exit(1);      }      finally      {         if (ss != null)         {            try             {               ss.close();            }            catch (Exception e)            {               System.err.println("closing: " + e.getMessage());            }         }      }   }}class EchoHandler implements Runnable{   private Socket client;   EchoHandler(Socket s)   {      client = s;   }   public void run()   {      BufferedReader in = null;      PrintWriter out1 = null;      try       {         in = new BufferedReader           (new InputStreamReader              (client.getInputStream()));         OutputStream out = client.getOutputStream();         out1 = new PrintWriter           (new OutputStreamWriter(out));         out1.println("Hello! Enter BYE to exit.");         out1.flush();         String line;         boolean done = false;         while (!done)         {            line = in.readLine();             if ((line == null)                || (line.trim().equals("BYE")))               done = true;            else if (line.trim().equals("skasmos"))            {               ThreadedEchoServer.shutDown();               return;            }            else            {               out1.println("Echo: " + line);               out1.flush();            }         }      }      catch (IOException e)      {         out1.println(e.getMessage());         out1.flush();         System.out.println(e.getMessage());      }      finally      {         try          {            in.close();         }         catch(IOException e)         {         }         if (out1 != null)         {            out1.close();         }         if (client != null)         {            try             {               client.close();            }            catch (IOException e)            {            }         }      }   }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -