开发者

Querying the client periodically for update via sockets-Reusing ports

using the Controller.java, I' implementing the run() in NetworkDiscovery.java which queries all the machine in the subnet . The active machines reply with their status. This happens at regular intervals.

public class Controller {
NetworkDiscovery n;
public static int discoveryInterval=2000;
PM pmList;
List pmlist=(List) new PM();

public static void main(String[] args) throws UnknownHostException{
Timer t1=new Timer();
t1.scheduleAtFixedRate(new NetworkDiscovery(), 2000, discoveryInterval);
}

public class NetworkDiscovery extends TimerTask{

InetAddress controllerIP;
int controllerPort;

NetworkDiscovery() throws UnknownHostException {
    controllerIP=InetAddress.getLocalHost();
    controllerPort=4455;
}

@Override
public void run() {
    try {
        byte[] recvB开发者_JAVA百科uf = new byte[5000];
        DatagramPacket packet = new DatagramPacket(recvBuf, recvBuf.length);
        DatagramSocket dSock = new DatagramSocket(4445);
        dSock.receive(packet);
//implementation related code follows
**dSock.close();**
}
}

On the client's side a similar Datagram socket is opened and objects are received/sent.

The problem is that on the COntroller's side, I'm executing NetworkDiscovery's run() after a specific time interval and during the second execution it says -

java.net.BindException: Address already in use

Since I'm closing the Controller's socket by close(), why does it still show that this address is already being in use? How can I make sure that during the next iteration, the controller starts over fresh call of networkDiscovery?


Perhaps the second task starts before the first was completly executed? Have you tried to insert debug messages and see if the first task was finished?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜