echoserverThreaded.java


  1. package hsz.ITDP.rac.uebungen.echoClientSrv;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.PrintWriter;
  6. import java.net.ServerSocket;
  7. import java.net.Socket;
  8.  
  9.  
  10. public class echoserverThreaded implements Runnable {
  11.         private Socket sock = null;
  12.         public echoserverThreaded(Socket sockin) {
  13.                 System.out.println("create thread for "+sockin.getRemoteSocketAddress());
  14.                 this.sock = sockin;
  15.         }
  16.         public void run() {
  17.                 System.out.println("run()");
  18.                 BufferedReader in = null;
  19.                 PrintWriter out = null;
  20.        while ( true ) {
  21.                
  22.                     try {
  23.                     in = new BufferedReader(new InputStreamReader(this.sock.getInputStream()));
  24.                         out = new PrintWriter(this.sock.getOutputStream(),true);
  25.                         boolean exit = false;
  26.                             while(!exit || in != null) {
  27.                                     String userwrote = in.readLine();
  28.                                     if(userwrote.equals("exit")) {
  29.                                             exit = true;
  30.                                     } else {
  31.                                             System.out.println("User "+ sock.getInetAddress() +" on socket "+sock.getLocalSocketAddress()+"wrote: "+userwrote);
  32.                                             out.println(userwrote);
  33.                                     }
  34.                             }
  35.                         } catch (Exception e) {
  36.                                 // TODO: handle exception
  37.                         } finally {
  38.                                 if (in != null)
  39.                                         try {
  40.                                                 in.close();
  41.                                         } catch (Exception e) {
  42.                                                 // TODO: handle exception
  43.                                         }
  44.                                 if (out != null)
  45.                                         try {
  46.                                                 out.close();
  47.                                         } catch (Exception e) {
  48.                                                 // TODO: handle exception
  49.                                         }
  50.                                        
  51.                                 if (sock != null)
  52.                                         try {
  53.                                                 sock.close();
  54.                                         } catch (Exception e) {
  55.                                                 // TODO: handle exception
  56.                                         }
  57.                                        
  58.                         }
  59.        }
  60.    }
  61.        
  62.         public static void main(String[] args) throws IOException {
  63.                 try {
  64.                         ServerSocket srvsock = null;
  65.                         int srvPort = 9998;  // Integer.parseInt(args[0]);
  66.                         srvsock = new ServerSocket(srvPort);
  67.                         while(true) {
  68.                                 Thread newThread = new Thread( new echoserverThreaded(srvsock.accept()) );
  69.                                 newThread.start();
  70.                         }
  71.                        
  72.                        
  73.                 } catch (Exception e) {
  74.                         // TODO: handle exception
  75.                 }
  76.         }
  77.  
  78.  
  79. }

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Use <fn>...</fn> to insert automatically numbered footnotes.
  • You can use the <go> tags just like the <a> for nicer urls.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.