Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6632005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:37:32+00:00 2026-05-25T22:37:32+00:00

I’m implementing a program where the controller(server) calls the agent(client) periodically and sends it

  • 0

I’m implementing a program where the controller(server) calls the agent(client) periodically and sends it IP address.

Controller.java

public class Controller {
         static int discoveryInterval;
         NetworkDiscovery n1;

   Controller(){        
         discoveryInterval=6000;
}
public static void main(String[] args) throws IOException {
         Timer t1=new Timer();
         t1.schedule(new NetworkDiscovery(), discoveryInterval);
}
}

NetworkDiscovery.java-

import java.io.*;
public class NetworkDiscovery extends TimerTask {
        protected DatagramSocket socket = null;
        protected BufferedReader in = null;

        public NetworkDiscovery() throws IOException {
               this("NetworkDiscovery");
        }
public NetworkDiscovery(String name) throws IOException {
        super(name);
        socket = new DatagramSocket(4445);
}

public void run() {
        try {
        byte[] buf = new byte[256];

       // receive request
       DatagramPacket packet = new DatagramPacket(buf, buf.length);
       socket.receive(packet);

       // figure out response
       String dString = InetAddress.getLocalHost().toString();
       buf = dString.getBytes();

       // send the response to the client at "address" and "port"
       InetAddress address = packet.getAddress();
       int port = packet.getPort();

       packet = new DatagramPacket(buf, buf.length, address, port);
       socket.send(packet);
} catch (IOException e) {
       e.printStackTrace();
}
socket.close();
}
}

On the Agent(client’s side)-
Agent.java

public class Agent {
      ackDiscovery ackDisc=new ackDiscovery();
      public static void main(String[] args) throws  SocketException,UnknownHostException,IOException {
             ackDiscovery ackDisc=new ackDiscovery();
             ackDisc.ackInfo();
             }
}

And ackDiscovery.java-

public class ackDiscovery {
            int agentListenPort;
            void ackDiscovery(){
                      agentListenPort=4455;
}

public void ackInfo() throws SocketException, UnknownHostException, IOException{
              // get a datagram socket
              DatagramSocket socket = new DatagramSocket();

              // send request
              byte[] buf = new byte[256];
              InetAddress address = InetAddress.getByName(MY_IP);
              DatagramPacket packet = new DatagramPacket(buf, buf.length, address,      4445);
              socket.send(packet);
              // get response
              packet = new DatagramPacket(buf, buf.length);
              socket.receive(packet);

              // display response
              String received = new String(packet.getData());
              System.out.println("Data received:"+ received);
              socket.close();
}
}

When I run the Controller(Server), the Agent’s(client’s) side get executed only once though the Controller is still listening. Also, if I re-run the agent, nothing happens. Can someone please help me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:37:33+00:00Added an answer on May 25, 2026 at 10:37 pm

    If you look at the definitions of the schedule method here:

    http://download.oracle.com/javase/7/docs/api/java/util/Timer.html

    You can see that the method with a single long parameter, will only execute once.
    What you’re looking for is the one with two long parameters. This will wait for delay ms and then execute every period ms.

    The one you are using will only execute once after delay ms.

    You may also want to look at either using non-blocking io (java.nio.*) for receiving connections or using a receive timeout. This way you won’t have multiple threads running at once.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.