如何使用java代码查找tomcat服务器系统信息?

时间:2014-09-11 10:39:23

标签: java tomcat java-ee

我正在开发Web应用程序,我将它托管在tomcat服务器中。我的要求是客户端系统想知道tomcat服务器位于网络中的系统的ip地址。之前使用java.or向服务器发出任何可能的设置为tomcat server设置默认的ip地址。我怎么能如果系统IP和tomcat ip不同,请使用默认IP地址发出请求。

1 个答案:

答案 0 :(得分:0)

我不知道我是否理解你的问题。所以服务器在局域网中运行,而你的客户端软件不知道连接到哪个地址/ ip?您可以将multicast服务器添加到服务器应用程序中,并将多播接收器添加到客户端。服务器将频繁发送一个Boardcast数据包到指定的Boardcast地址/频道,即203.0.113.0。

一旦您的客户端启动,它将收到服务器发送的Boardcast数据包,只要他连接到同一个频道。数据包包含服务器地址。您可以使用该地址连接到服务器。

<强>更新

这是一个非常简单的发送方,接收方和常量类来共享设置的示例。它发送一个字符串(服务器在这里)作为数据包数据,但在实际应用程序中,您应该创建一个对象在服务器上序列化它并在客户端反序列化它。该对象可以包含有关服务器的更多信息,可以在客户端进行验证。

服务器/客户端共享代码:

public final class MulticastConstants {
 public static final String MULTICAST_PACKET_DATA = "Server is here!";
 public static final String MULTICAST_CHANNEL = "230.0.0.1";
 public static final int MULTICAST_PORT = 8881;
 public static final int MULTICAST_PACKET_SIZE = MULTICAST_PACKET_DATA.getBytes().length;
}

服务器端代码:

发件人是WebListener,将随您的应用程序启动和停止。

@WebListener
public class MulticastSender implements ServletContextListener {

private MulticastSocket socket;
private boolean running = true;
private Thread mcss;

public MulticastSender() {
    System.out.println("New " + this.getClass().getSimpleName());
    try {
        socket = new MulticastSocket(MulticastConstants.MULTICAST_PORT);
        mcss = new MulticastServerThread();
    } catch (IOException e) {
        System.out.println("Error creating MulticastSender: " + e.getMessage());
    }
}

@Override
public void contextInitialized(ServletContextEvent sce) {
    System.out.println("Starting " + this.getClass().getSimpleName());
    mcss.start();
}

@Override
public void contextDestroyed(ServletContextEvent sce) {
    System.out.println("Stopping " + this.getClass().getSimpleName());
    this.running = false;
    socket.disconnect();
    socket.close();
}

private class MulticastServerThread extends Thread {

    public MulticastServerThread() {
        super("MulticastServer");
    }

    public void run() {

        System.out.println("Start sending multicast packets ...");
        while (running) {
            System.out.println("Sending multicast packet ...");
            try {
                byte[] dataBuffer = MulticastConstants.MULTICAST_PACKET_DATA.getBytes();
                InetAddress group = InetAddress.getByName(MulticastConstants.MULTICAST_CHANNEL);
                DatagramPacket packet = new DatagramPacket(dataBuffer, dataBuffer.length, group, MulticastConstants.MULTICAST_PORT);
                socket.send(packet);
                System.out.println("Packet send ...");
                try {
                    sleep(2000);
                } catch (InterruptedException e) {
                }
            } catch (IOException e) {
                System.out.println("Error sending multicast packet: " + e.getMessage());
                running = false;
                break;
            }
        }
        socket.close();
    }
}
}

客户端代码:

为简单起见,客户端仅接收单个数据包。您可以创建一个不冻结客户端的线程。

public class MulticastReceiver {

private MulticastSocket socket;
private InetAddress address;

public static void main(String[] args) throws InterruptedException {
    new MulticastReceiver();
}

public MulticastReceiver() {
    System.out.println("Starting MulticastReceiver ...");
    try {
        address = InetAddress.getByName(MulticastConstants.MULTICAST_CHANNEL);
        socket = new MulticastSocket(MulticastConstants.MULTICAST_PORT);
        socket.joinGroup(address);

        DatagramPacket packet;
        try {
            byte[] buf = new byte[MulticastConstants.MULTICAST_PACKET_SIZE];
            packet = new DatagramPacket(buf, buf.length);
            System.out.println("Waiting for packets ...");
            socket.receive(packet);
            System.out.println("Received a packet (" + packet.getLength() + " bytes) ...");
            // deserialize packet.getData() to your own object (for simplicity a String is used) ...
            // check if type and serialVersionId are ok, otherwise dispose packet ...
            System.out.println("Server is located at: " + packet.getAddress());
            socket.close();
            // connect to server ...
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } catch (UnknownHostException e) {
        System.out.println("Could not connect to host \"" + address + "\": " + e.getMessage());
    } catch (Exception e) {
        System.out.println("Error initializing: " + e.getMessage());
    }
}
}

在Glassfish 4上测试过,希望有所帮助。