Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * - Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * - Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * - Neither the name of Oracle or the names of its
- * contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
- * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- import java.net.*;
- import java.io.*;
- //import KnockKnockServer.receiver;
- public class KnockKnockServer {
- public static void main(String[] args) throws IOException {
- ServerSocket serverSocket = null;
- try {
- serverSocket = new ServerSocket(4444);
- } catch (IOException e) {
- System.err.println("Could not listen on port: 4444.");
- System.exit(1);
- }
- Socket clientSocket = null;
- try {
- clientSocket = serverSocket.accept();
- // KnockKnockServer newServer = new KnockKnockServer(); //SO-code
- // KnockKnockServer.receiver receiv = newServer.new
- // receiver(clientSocket);//SO-code
- // receiv.run(); //SO-code
- } catch (SocketException e) {
- System.err.println("SocketException /!!!!.");
- System.exit(1);
- }
- catch (IOException e) {
- System.err.println("Accept failed.");
- System.exit(1);
- }
- /*
- * try { //new code KnockKnockServer newS = new KnockKnockServer();
- * KnockKnockServer.receiver receiv = newS.new receiver(clientSocket);
- * receiv.run(); } catch(IOException e) {
- * System.err.println("Accept failed."); System.exit(1); }
- */
- PrintWriter out;
- BufferedReader in;
- try {
- out = new PrintWriter(clientSocket.getOutputStream(), true);
- in = new BufferedReader(new InputStreamReader(
- clientSocket.getInputStream()));
- String inputLine, outputLine;
- KnockKnockProtocol kkp = new KnockKnockProtocol();
- outputLine = kkp.processInput(null);
- out.println(outputLine);
- while ((inputLine = in.readLine()) != null) {
- outputLine = kkp.processInput(inputLine);
- out.println(outputLine);
- if (outputLine.equals("Bye."))
- break;
- out.close();
- in.close();
- clientSocket.close();
- serverSocket.close();
- }
- } catch (SocketException e1) {
- System.out.println(" Socet exprssion");
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- catch (Exception e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- // Socket clientSocket = null;
- /* try { commnted out 1238PM
- // clientSocket = serverSocket.accept();
- KnockKnockServer newServer = new KnockKnockServer(); // SO-code
- KnockKnockServer.receiver receive = newServer.new receiver(
- clientSocket);// SO-code
- receive.run(); // SO-code
- } catch (IOException e) {
- System.err.println("NEW Accept failed.");
- System.exit(1);
- }*/
- }// end main
- // new code from SO
- public class receiver extends Thread {
- public boolean heartbeatmessage = false;
- Socket clientSocket = new Socket("localhost ", 4444);
- PrintWriter out; // =new PrintWriter(clientSocket.getOutputStream());
- public receiver(Socket clientsocket) throws IOException {
- clientSocket = clientsocket;
- out = new PrintWriter(clientSocket.getOutputStream(), true);
- }
- public void run() {
- while (true) {
- if (heartbeatmessage) {
- try {
- Thread.sleep(1000);
- out.println("heartbeat");
- } catch (InterruptedException e) {
- System.out.println(" other side die");
- }
- } else {
- break;
- }
- }
- }
- }
- // </new code>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement