简体   繁体   English

Java中的服务器客户端程序

[英]Server Client Program in Java

I'm implementing a program where the controller(server) calls the agent(client) periodically and sends it IP address. 我正在实现一个程序,其中控制器(服务器)定期调用代理(客户端)并发送它的IP地址。

Controller.java 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- 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 在代理(客户端) - 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- 和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. 当我运行Controller(服务器)时,虽然Controller仍在监听,但代理(客户端)端只执行一次。 Also, if I re-run the agent, nothing happens. 此外,如果我重新运行代理,没有任何反应。 Can someone please help me? 有人可以帮帮我吗?

If you look at the definitions of the schedule method here: 如果您在此处查看计划方法的定义:

http://download.oracle.com/javase/7/docs/api/java/util/Timer.html 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. 您可以看到具有单个long参数的方法只执行一次。 What you're looking for is the one with two long parameters. 您正在寻找的是具有两个长参数的那个。 This will wait for delay ms and then execute every period ms. 这将等待延迟 ms,然后执行每个周期 ms。

The one you are using will only execute once after delay ms. 您正在使用的那个只会在延迟 ms后执行一次。

You may also want to look at either using non-blocking io (java.nio.*) for receiving connections or using a receive timeout. 您可能还希望使用非阻塞io(java.nio。*)来接收连接或使用接收超时。 This way you won't have multiple threads running at once. 这样您就不会同时运行多个线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM