0

我有兴趣在 JSF 页面中测量客户端和服务器之间的连接速度。

我发现这个java代码连接速度很差:

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Calendar;

public class UdpEchoClient {
  static final String testString = "Greeks bearing gifts";

  public static void main(String[] args) {
    InetAddress address;
    try {
      address = InetAddress.getByName(args[0]);
    } catch (UnknownHostException host) {
      System.out.println(host);
      return;
    }
    DatagramPacket pack = new DatagramPacket(testString.getBytes(),
        testString.length(), address, 7);
    DatagramPacket incoming = new DatagramPacket(new byte[256], 256);
    DatagramSocket sock = null;
    try {
      Calendar start, end;
      sock = new DatagramSocket();
      start = Calendar.getInstance();
      sock.send(pack);
      sock.setSoTimeout(5000);
      sock.receive(incoming);
      end = Calendar.getInstance();
      String reply = new String(incoming.getData());
      reply = reply.substring(0, testString.length());
      if (reply.equals(testString)) {
        System.out.println("Success");
        System.out.println("Time = "
            + (end.getTime().getTime() - start.getTime().getTime())
            + "mS");
      } else
        System.out.println("Reply data did not match");
    } catch (SocketException socke) {
      System.out.println(socke);
    } catch (IOException ioe) {
      System.out.println(ioe);
    } finally {
      sock.close();
    }
  }
}

但这可以用于 JSF 页面还是我必须使用 JavaScript 来做到这一点?

4

1 回答 1

2

当服务器在物理上与客户端不同的机器上运行时,客户端上的任何自尊防火墙都可能会阻止这种情况。

XMLHttpRequest在 JavaScript 中将a 计时到相同的来源是一个更好的选择恕我直言。

于 2012-04-18T20:57:18.233 回答