DaniWeb IT Discussion Community

Code Snippets (http://www.daniweb.com/code/)
-   java (http://www.daniweb.com/code/java.html)
-   -   Restart Your ServerSocket with dummySocket (http://www.daniweb.com/code/snippet828.html)

shaikh_mshariq java syntax
Feb 18th, 2008
Here is the code to write a serverSocket which listens on a given port. I have used a dummy Socket to change serverSocket's port dynamically. This method uses a inner class which is basically a thread and it creates a serversocket in thread by given port no. If you want to change its port dynamically just create a dummysocket to the given serversocket and alter the flag boolean variable then create new thread with new port. If any one can optimize it please add your ideas. I am not the Best.

  1. import java.io.IOException;
  2. import java.net.InetSocketAddress;
  3. import java.net.ServerSocket;
  4. import java.net.Socket;
  5. import java.net.InetAddress;
  6. import java.net.SocketTimeoutException;
  7.  
  8. public class LocalServer {
  9. private int port;
  10. private boolean change=true;
  11. public int getPort(){return port;}
  12. public void setPort(int newPort){port=newPort;}
  13. public void setChange(boolean bool){change=bool;}
  14.  
  15. private boolean getChange(){return change;}
  16. public Thread getServerThread(int port){
  17. Thread t=null;
  18. setPort(port);
  19. return t=new Thread(new Runnable(){
  20. public void run(){
  21. System.out.println("Starting Server for Communication . . .");
  22. ServerSocket serverSocket=null;
  23. Socket socket=null;
  24.  
  25. try{
  26. while(getChange()){
  27. System.out.println("Flag is : "+getChange());
  28. serverSocket=new ServerSocket(getPort());
  29. System.out.println("Server Started SuccessFully . . .");
  30. socket=serverSocket.accept();
  31. System.out.println("Server Listening On "+serverSocket.getLocalPort());
  32. service(socket);
  33. //Thread.sleep(10000);
  34. System.out.println("Service Called SuccessFully . . . ");
  35. }
  36.  
  37. }catch(SocketTimeoutException sex){try {
  38. serverSocket.close();
  39. System.out.println("Error In Server Initialization!");
  40. } catch (IOException e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. System.out.println("Error In Server Initialization!");
  44. }}
  45. catch(Exception ex){ex.printStackTrace();}
  46. }
  47. });
  48. }
  49. private static void service(Socket slaveSocket){
  50.  
  51. System.out.println("Service Called");
  52. }
  53. public static void main(String s[])
  54. {try{
  55. LocalServer ls=new LocalServer();
  56. Thread serverThread=ls.getServerThread(10000);
  57. serverThread.start();
  58. System.out.println("Thread Started.");
  59. Thread.sleep(1000);
  60. ls.setChange(false);
  61. Socket dummySocket=new Socket("localhost",10000);
  62. System.out.println("Socket created");
  63. serverThread=ls.getServerThread(20000);
  64. serverThread.start();
  65.  
  66. }catch(Exception ex){}
  67.  
  68. }
  69.  
  70. }