From 6efb14dd91131bb56c5895b6db3e7b95e043b3a7 Mon Sep 17 00:00:00 2001 From: VINAY Date: Sat, 18 Apr 2015 21:29:07 -0400 Subject: [PATCH] LB-ServerThread Made the while loop a separate Thread --- src/network/LBClientServer.java | 13 +------------ src/network/ServerThread.java | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 src/network/ServerThread.java diff --git a/src/network/LBClientServer.java b/src/network/LBClientServer.java index 9b7e38a..f16a03f 100644 --- a/src/network/LBClientServer.java +++ b/src/network/LBClientServer.java @@ -13,22 +13,11 @@ public class LBClientServer { public LBClientServer(){ try { LBSocket = new ServerSocket(serverPort); - this.startListening(); + new Thread(new ServerThread(LBSocket)).start(); } catch (IOException e) { System.out.println("Error while trying to connect with the client "); e.printStackTrace(); } } - private void startListening(){ - while(true){ - try { - (new Thread(new ClientConnectionHandler(LBSocket.accept()))).start(); - } catch (IOException e) { - System.out.println("Error in the accept block "); - e.printStackTrace(); - } - } - } - } diff --git a/src/network/ServerThread.java b/src/network/ServerThread.java new file mode 100644 index 0000000..261814f --- /dev/null +++ b/src/network/ServerThread.java @@ -0,0 +1,28 @@ +package network; + +import java.io.IOException; +import java.net.ServerSocket; + +import edu.tomr.loadbalancer.ClientConnectionHandler; + +public class ServerThread implements Runnable { + private ServerSocket LBSocket = null; + + public ServerThread(ServerSocket newSocket){ + this.LBSocket = newSocket; + } + + @Override + public void run() { + while(true){ + try { + (new Thread(new ClientConnectionHandler(LBSocket.accept()))).start(); + } catch (IOException e) { + System.out.println("Error in the accept block "); + e.printStackTrace(); + } + } + + } + +}