I am developing an application in which i want to set up ServerSocket's port on requirement. I have given an interface which accepts port number and this port number i want to assign my previously running serverSocket but serverSocket.accept() method blocks and I am unable to execute the code after accept what can I do Please Help. Code Is As Above :
package comm;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.InetAddress;
import proto.ErrorProvider;
public class LocalServer implements Runnable{
private ServerSocket serverSocket=null;
private Socket slaveSocket=null;
private int port;
private boolean change=false;
public int getPort(){return port;}
public void setPort(int newPort){port=newPort;}
public void setChange(boolean bool){change=bool;}
public void run() {
try{
// TODO Auto-generated method stub
if(serverSocket==null){
serverSocket=new ServerSocket(getPort());
ErrorProvider.getPlainPnl("Server Started On :"+getPort());
slaveSocket=serverSocket.accept();
System.out.println(slaveSocket.getLocalPort());
ErrorProvider.getPlainPnl("Accepted");
if(change){
serverSocket.close();
serverSocket=null;
//serverSocket=new ServerSocket(getPort());
ErrorProvider.getErrorPnl("In Change");
restart();
}
}
}catch(Exception ex){ErrorProvider.getErrorPnl(ex.getMessage());}
}
public void restart(){
System.out.println("Restart Called... ");
run();
}
public void interrupt(){
System.out.println("Interrupt Called... ");
this.interrupt();
}
public void stop(){
try{
System.out.println("Stop Called... ");
serverSocket.close();
serverSocket=null;
}catch(Exception ex){ErrorProvider.getErrorPnl(ex.getMessage());}
}
public void sleep(long time){
try{
System.out.println("Sleep Called... ");
Thread.sleep(time);
stop();
restart();
}catch(Exception ex){ErrorProvider.getErrorPnl(ex.getMessage());}
}
public void start(){
run();
}
/*public static void main(String s[])
{try{
LocalServer ls=new LocalServer();
ls.setPort(10000);
System.out.println("Thread Started.");
ls.start();
ls.interrupt();
ls.setChange(true);
ls.setPort(20000);
Socket dummySocket=new Socket("localhost",10000);
System.out.println("Socket created");
ls.sleep(1000);
}catch(Exception ex){ErrorProvider.getErrorPnl(ex.getMessage());}
}
*/
}:(