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. :)

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.InetAddress;
import java.net.SocketTimeoutException;

public class LocalServer {
	private int port;
	private boolean change=true;
	public int getPort(){return port;}
	public  void setPort(int newPort){port=newPort;}
	public  void setChange(boolean bool){change=bool;}
	
	private  boolean getChange(){return change;}
	public Thread getServerThread(int port){
		Thread t=null;
		setPort(port);
		return t=new Thread(new Runnable(){
			public void run(){
				System.out.println("Starting Server for Communication . . .");
				ServerSocket serverSocket=null;
				Socket socket=null;
				
					try{
						while(getChange()){
							System.out.println("Flag is : "+getChange());
						serverSocket=new ServerSocket(getPort());
						System.out.println("Server Started  SuccessFully . . .");
						socket=serverSocket.accept();
						System.out.println("Server Listening On "+serverSocket.getLocalPort());
						service(socket);
						//Thread.sleep(10000);
						System.out.println("Service Called SuccessFully . . . ");
						}
						
					}catch(SocketTimeoutException sex){try {
						serverSocket.close();
						System.out.println("Error In Server Initialization!");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						System.out.println("Error In Server Initialization!");
					}}
				catch(Exception ex){ex.printStackTrace();}
			}
		});
	}
	private static void service(Socket slaveSocket){
		
		System.out.println("Service Called");
	}
	public  static void main(String s[])
	{try{
		LocalServer ls=new LocalServer();
		Thread serverThread=ls.getServerThread(10000);
		serverThread.start();
		System.out.println("Thread Started.");
		Thread.sleep(1000);
		ls.setChange(false);
		Socket dummySocket=new Socket("localhost",10000);
		System.out.println("Socket created");
		serverThread=ls.getServerThread(20000);
		serverThread.start();
		
	}catch(Exception ex){}
	
	}
	
	}

From the Author Code Reengineered :-

import java.io.IOException;
import java.net.ConnectException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;


public class LocalServer {
private int port;
public  ServerSocket serverSocket=null;
private boolean change=false;
public int getPort(){return port;}
public  void setPort(int newPort){port=newPort;}
public  void setChange(boolean bool){change=bool;}
public LocalServer(int portn){setPort(portn);}
private  boolean getChange(){return change;}
public  Thread getServerThread(){
Thread t=null;
return t=new Thread(new Runnable(){
public void run(){
System.out.println("Starting Server for Communication . . .");


Socket socket=null;


try{
serverSocket=new ServerSocket(getPort());
while(true){
System.out.println("Flag is : "+getChange());
if(getChange() && serverSocket!=null){
serverSocket.close();
serverSocket=null;
serverSocket=new ServerSocket(getPort());
System.out.println("In Condition");
}
socket=serverSocket.accept();
System.out.println("Server Listening On "+serverSocket.getLocalPort());
service(socket);
socket.close();
System.out.println("Service Called SuccessFully . . . ");
}


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Error In Server Initialization!");
}
catch(Exception ex){ex.printStackTrace();}
}
});
}
private  void service(Socket slaveSocket){


System.out.println("Service Called");
}
public void changePort(int newPort){
try{
int prePort=this.getPort();
System.out.println("Changing Server Port "+prePort+" To "+newPort+" Port");
this.setPort(newPort);
this.setChange(true);
Socket dummySocket=new Socket("localhost",prePort);
dummySocket.close();
Thread.sleep(2000);//time to chage serverconfiguration according to the flag
this.setChange(false);
}catch(ConnectException conex){}
catch(Exception ex){ErrorProvider.getErrorPnl(ex.toString());}
}
public boolean checkPort(int prt){
try{
this.setChange(true);
Socket dummySocket=new Socket("localhost",prt);
dummySocket.close();
return true;
}catch(java.net.ConnectException conex){return false;}
catch(Exception ex){ErrorProvider.getErrorPnl(ex.toString());return false;}



}
public  static void main(String s[])
{try{
LocalServer ls=new LocalServer(10000);//initial port number of the server
//creates serversocket with 10000 port number
Thread serverThread=ls.getServerThread();
serverThread.start();
//server started
Thread.sleep(3000);     //proper time to server Startup


ls.changePort(40000);
ls.changePort(50000);
if(ls.checkPort(40000)){ErrorProvider.getInfoPnl("Port is OK");}
else
{ErrorProvider.getInfoPnl("Port is not OK");}


}
catch(Exception ex){ErrorProvider.getErrorPnl(ex.getMessage());}


}


}

;)
Try it and add your suggestions

Edited 3 Years Ago by happygeek: fixed formatting