Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Apr 6th, 2013  |  syntax: Java  |  size: 4.73 KB  |  views: 106  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.  * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  *   - Redistributions of source code must retain the above copyright
  9.  *     notice, this list of conditions and the following disclaimer.
  10.  *
  11.  *   - Redistributions in binary form must reproduce the above copyright
  12.  *     notice, this list of conditions and the following disclaimer in the
  13.  *     documentation and/or other materials provided with the distribution.
  14.  *
  15.  *   - Neither the name of Oracle or the names of its
  16.  *     contributors may be used to endorse or promote products derived
  17.  *     from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31.  
  32. import java.net.*;
  33. import java.io.*;
  34.  
  35. //import KnockKnockServer.receiver;  
  36.  
  37. public class KnockKnockServer {
  38.  
  39.         public static void main(String[] args) throws IOException {
  40.  
  41.                 ServerSocket serverSocket = null;
  42.                 try {
  43.                         serverSocket = new ServerSocket(4444);
  44.                 } catch (IOException e) {
  45.                         System.err.println("Could not listen on port: 4444.");
  46.                         System.exit(1);
  47.                 }
  48.  
  49.                 Socket clientSocket = null;
  50.                 try {
  51.                         clientSocket = serverSocket.accept();
  52.  
  53.                         // KnockKnockServer newServer = new KnockKnockServer(); //SO-code
  54.                         // KnockKnockServer.receiver receiv = newServer.new
  55.                         // receiver(clientSocket);//SO-code
  56.                         // receiv.run(); //SO-code
  57.  
  58.                 }  catch (SocketException e) {
  59.                         System.err.println("SocketException /!!!!.");
  60.                         System.exit(1);
  61.                 }
  62.                    catch (IOException e) {
  63.                         System.err.println("Accept failed.");
  64.                         System.exit(1);
  65.                 }
  66.  
  67.                 /*
  68.                  * try { //new code KnockKnockServer newS = new KnockKnockServer();
  69.                  * KnockKnockServer.receiver receiv = newS.new receiver(clientSocket);
  70.                  * receiv.run(); } catch(IOException e) {
  71.                  * System.err.println("Accept failed."); System.exit(1); }
  72.                  */
  73.  
  74.                 PrintWriter out;
  75.                 BufferedReader in;
  76.                 try {
  77.                         out = new PrintWriter(clientSocket.getOutputStream(), true);
  78.                         in = new BufferedReader(new InputStreamReader(
  79.                                         clientSocket.getInputStream()));
  80.                         String inputLine, outputLine;
  81.                         KnockKnockProtocol kkp = new KnockKnockProtocol();
  82.  
  83.                         outputLine = kkp.processInput(null);
  84.                         out.println(outputLine);
  85.  
  86.                         while ((inputLine = in.readLine()) != null) {
  87.                                 outputLine = kkp.processInput(inputLine);
  88.                                 out.println(outputLine);
  89.                                 if (outputLine.equals("Bye."))
  90.                                         break;
  91.                                
  92.                                  
  93.                                 out.close();
  94.                                 in.close();
  95.                                 clientSocket.close();
  96.                                 serverSocket.close();
  97.                                
  98.                         }
  99.                 } catch (SocketException e1) {
  100.                         System.out.println(" Socet exprssion");
  101.                         // TODO Auto-generated catch block
  102.                         e1.printStackTrace();
  103.                 }
  104.  
  105.                 catch (Exception e1) {
  106.                         // TODO Auto-generated catch block
  107.                         e1.printStackTrace();
  108.                 }
  109.                
  110.  
  111.  
  112.                 // Socket clientSocket = null;
  113. /*              try {  commnted out 1238PM
  114.                         // clientSocket = serverSocket.accept();
  115.  
  116.                         KnockKnockServer newServer = new KnockKnockServer(); // SO-code
  117.                         KnockKnockServer.receiver receive = newServer.new receiver(
  118.                                         clientSocket);// SO-code
  119.                         receive.run(); // SO-code
  120.  
  121.                 } catch (IOException e) {
  122.                         System.err.println("NEW Accept failed.");
  123.                         System.exit(1);
  124.                 }*/
  125.  
  126.         }// end main
  127.  
  128.        
  129.        
  130.         // new code from SO
  131.         public class receiver extends Thread {
  132.  
  133.                 public boolean heartbeatmessage = false;
  134.  
  135.                 Socket clientSocket = new Socket("localhost ", 4444);
  136.                 PrintWriter out; // =new PrintWriter(clientSocket.getOutputStream());
  137.  
  138.                 public receiver(Socket clientsocket) throws IOException {
  139.                         clientSocket = clientsocket;
  140.                         out = new PrintWriter(clientSocket.getOutputStream(), true);
  141.                 }
  142.  
  143.                 public void run() {
  144.  
  145.                         while (true) {
  146.                                 if (heartbeatmessage) {
  147.                                         try {
  148.                                                 Thread.sleep(1000);
  149.                                                 out.println("heartbeat");
  150.                                         } catch (InterruptedException e) {
  151.                                                 System.out.println(" other side die");
  152.                                         }
  153.                                 } else {
  154.                                         break;
  155.                                 }
  156.  
  157.                         }
  158.                 }
  159.         }
  160.  
  161.         // </new code>
  162.  
  163. }
clone this paste RAW Paste Data