当前位置: 技术问答>java相关
高分,不够再加!!如何用java applet实现在客户端浏览器中显示即时的web server的系统时间
来源: 互联网 发布时间:2015-02-06
本文导语: 如何用java applet实现在客户端浏览器中显示即时的web server的系统时间 | lionhero:我改了一下 你可以用这个看看,但你还是要作修改,我要下班了。byebye package daytimeclient; import java.awt.*; impor...
如何用java applet实现在客户端浏览器中显示即时的web server的系统时间
|
lionhero:我改了一下
你可以用这个看看,但你还是要作修改,我要下班了。byebye
package daytimeclient;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.net.*;
/**
* Title: DaytimeClient
* Description:
* Copyright: Copyright (c) 2001
* Company: Huateng
* @author lu shengxiao
* @version 1.0
*/
public class DayTimeClient extends Applet {
private static final int TIME_PORT=13;
private static final int TIMEOUT=10000;
private DatagramSocket timeSocket=null;
private static final int SMALL_ARRAY=1;
private static final int TIME_ARRAY=100;
InetAddress remoteMachine;
private String timeString;
private Label lblDayTime = new Label();
/**Start the applet*/
public void start() {
try{
timeSocket=new DatagramSocket();
}catch(SocketException e){
System.err.println("Unable to open socket:"+e);
}
try{
remoteMachine=InetAddress.getByName("HOSTNAME");
}catch(UnknownHostException uhe){
System.out.println("Unknown host"+remoteMachine+":"+uhe);
}
getDayTime();
}
public String getDayTime(){
DatagramPacket timeQuery;
DatagramPacket response;
byte[] emptyBuffer;
try{
emptyBuffer=new byte[SMALL_ARRAY];
timeQuery=new DatagramPacket(emptyBuffer,emptyBuffer.length,remoteMachine,TIME_PORT);
timeSocket.send(timeQuery);
}catch(IOException e){
System.out.println("Unable to send to"+remoteMachine+":"+e);
}
try {
timeSocket.setSoTimeout(TIMEOUT);
}catch (SocketException ex) {}
try{
emptyBuffer=new byte[TIME_ARRAY];
response=new DatagramPacket(emptyBuffer,emptyBuffer.length);
timeSocket.receive(response);
timeString=new String(response.getData());
lblDayTime.setText(timeString);
}catch(InterruptedIOException iie){
System.out.println("Timeout on receive:"+iie);
}catch(IOException ioe){
System.out.println("Failed I/O:"+ioe);
}
return timeString;
}
/**Stop the applet*/
public void stop() {
if (timeSocket!=null) timeSocket.close();
}
public DayTimeClient() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
lblDayTime.setText("label1");
this.add(lblDayTime, null);
}
}
你可以用这个看看,但你还是要作修改,我要下班了。byebye
package daytimeclient;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.net.*;
/**
* Title: DaytimeClient
* Description:
* Copyright: Copyright (c) 2001
* Company: Huateng
* @author lu shengxiao
* @version 1.0
*/
public class DayTimeClient extends Applet {
private static final int TIME_PORT=13;
private static final int TIMEOUT=10000;
private DatagramSocket timeSocket=null;
private static final int SMALL_ARRAY=1;
private static final int TIME_ARRAY=100;
InetAddress remoteMachine;
private String timeString;
private Label lblDayTime = new Label();
/**Start the applet*/
public void start() {
try{
timeSocket=new DatagramSocket();
}catch(SocketException e){
System.err.println("Unable to open socket:"+e);
}
try{
remoteMachine=InetAddress.getByName("HOSTNAME");
}catch(UnknownHostException uhe){
System.out.println("Unknown host"+remoteMachine+":"+uhe);
}
getDayTime();
}
public String getDayTime(){
DatagramPacket timeQuery;
DatagramPacket response;
byte[] emptyBuffer;
try{
emptyBuffer=new byte[SMALL_ARRAY];
timeQuery=new DatagramPacket(emptyBuffer,emptyBuffer.length,remoteMachine,TIME_PORT);
timeSocket.send(timeQuery);
}catch(IOException e){
System.out.println("Unable to send to"+remoteMachine+":"+e);
}
try {
timeSocket.setSoTimeout(TIMEOUT);
}catch (SocketException ex) {}
try{
emptyBuffer=new byte[TIME_ARRAY];
response=new DatagramPacket(emptyBuffer,emptyBuffer.length);
timeSocket.receive(response);
timeString=new String(response.getData());
lblDayTime.setText(timeString);
}catch(InterruptedIOException iie){
System.out.println("Timeout on receive:"+iie);
}catch(IOException ioe){
System.out.println("Failed I/O:"+ioe);
}
return timeString;
}
/**Stop the applet*/
public void stop() {
if (timeSocket!=null) timeSocket.close();
}
public DayTimeClient() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
lblDayTime.setText("label1");
this.add(lblDayTime, null);
}
}
|
开启Web server 的时钟服务
服务/协议 端口 说 明
daytime 13 时钟服务,返回当前服务器时间的文本描述。
import java.io.*;
import java.net.*;
public class DaytimeTCPClient
{
public static final String NETASCIIEOL = "rn";
private static final SocketFactory DEFAULTSOCKETFACTORY = new DefaultSocketFactory();
protected int timeout; //超时时间
protected Socket socket;
protected boolean isConnected; //连接标志
protected int defaultPort; //默认端口号
protected InputStream input; //Socket的输入流
protected OutputStream output; //Socket的输出流
protected SocketFactory socketFactory; //SocketFactory是Socket抽象类
public static final int DEFAULTPORT = 13;
private char buffer[];
public DaytimeTCPClient()
{
socket = null;
input = null;
output = null;
timeout = 0;
defaultPort = 0;
isConnected = false;
socketFactory = DEFAULTSOCKETFACTORY;
buffer = new char[64];
setDefaultPort(13);
}
public String getTime()
throws IOException
{
StringBuffer stringbuffer = new StringBuffer(buffer.length);
BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(input));
do
{
int i = bufferedreader.read(buffer, 0, buffer.length);
if(i > 0)
stringbuffer.append(buffer, 0, i);
else
return stringbuffer.toString();
} while(true);
}
protected void connectAction()
throws IOException
{
socket.setSoTimeout(timeout);
input = socket.getInputStream();
output = socket.getOutputStream();
isConnected = true;
}
public void connect(InetAddress inetaddress, int i)
throws SocketException, IOException
{
socket = socketFactory.createSocket(inetaddress, i);
connectAction();
}
public void connect(String s, int i)
throws SocketException, IOException
{
socket = socketFactory.createSocket(s, i);
connectAction();
}
public void connect(InetAddress inetaddress, int i, InetAddress inetaddress1, int j)
throws SocketException, IOException
{
socket = socketFactory.createSocket(inetaddress, i, inetaddress1, j);
connectAction();
}
public void connect(String s, int i, InetAddress inetaddress, int j)
throws SocketException, IOException
{
socket = socketFactory.createSocket(s, i, inetaddress, j);
connectAction();
}
public void connect(InetAddress inetaddress)
throws SocketException, IOException
{
connect(inetaddress, defaultPort);
}
public void connect(String s)
throws SocketException, IOException
{
connect(s, defaultPort);
}
public void disconnect()
throws IOException
{
socket.close();
input.close();
output.close();
socket = null;
input = null;
output = null;
isConnected = false;
}
public boolean isConnected()
{
return isConnected;
}
public void setDefaultPort(int i)
{
defaultPort = i;
}
public int getDefaultPort()
{
return defaultPort;
}
public void setDefaultTimeout(int i)
{
timeout = i;
}
public int getDefaultTimeout()
{
return timeout;
}
public void setSoTimeout(int i)
throws SocketException
{
socket.setSoTimeout(i);
}
public int getSoTimeout()
throws SocketException
{
return socket.getSoTimeout();
}
public void setTcpNoDelay(boolean flag)
throws SocketException
{
socket.setTcpNoDelay(flag);
}
public boolean getTcpNoDelay()
throws SocketException
{
return socket.getTcpNoDelay();
}
public void setSoLinger(boolean flag, int i)
throws SocketException
{
socket.setSoLinger(flag, i);
}
public int getSoLinger()
throws SocketException
{
return socket.getSoLinger();
}
public int getLocalPort()
{
return socket.getLocalPort();
}
public InetAddress getLocalAddress()
{
return socket.getLocalAddress();
}
public int getRemotePort()
{
return socket.getPort();
}
public InetAddress getRemoteAddress()
{
return socket.getInetAddress();
}
public boolean verifyRemote(Socket socket)
{
InetAddress inetaddress = socket.getInetAddress();
InetAddress inetaddress1 = getRemoteAddress();
return inetaddress.equals(inetaddress1);
}
public void setSocketFactory(SocketFactory socketfactory)
{
if(socketfactory == null)
{
socketFactory = DEFAULTSOCKETFACTORY;
return;
} else
{
socketFactory = socketfactory;
return;
}
}
}
服务/协议 端口 说 明
daytime 13 时钟服务,返回当前服务器时间的文本描述。
import java.io.*;
import java.net.*;
public class DaytimeTCPClient
{
public static final String NETASCIIEOL = "rn";
private static final SocketFactory DEFAULTSOCKETFACTORY = new DefaultSocketFactory();
protected int timeout; //超时时间
protected Socket socket;
protected boolean isConnected; //连接标志
protected int defaultPort; //默认端口号
protected InputStream input; //Socket的输入流
protected OutputStream output; //Socket的输出流
protected SocketFactory socketFactory; //SocketFactory是Socket抽象类
public static final int DEFAULTPORT = 13;
private char buffer[];
public DaytimeTCPClient()
{
socket = null;
input = null;
output = null;
timeout = 0;
defaultPort = 0;
isConnected = false;
socketFactory = DEFAULTSOCKETFACTORY;
buffer = new char[64];
setDefaultPort(13);
}
public String getTime()
throws IOException
{
StringBuffer stringbuffer = new StringBuffer(buffer.length);
BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(input));
do
{
int i = bufferedreader.read(buffer, 0, buffer.length);
if(i > 0)
stringbuffer.append(buffer, 0, i);
else
return stringbuffer.toString();
} while(true);
}
protected void connectAction()
throws IOException
{
socket.setSoTimeout(timeout);
input = socket.getInputStream();
output = socket.getOutputStream();
isConnected = true;
}
public void connect(InetAddress inetaddress, int i)
throws SocketException, IOException
{
socket = socketFactory.createSocket(inetaddress, i);
connectAction();
}
public void connect(String s, int i)
throws SocketException, IOException
{
socket = socketFactory.createSocket(s, i);
connectAction();
}
public void connect(InetAddress inetaddress, int i, InetAddress inetaddress1, int j)
throws SocketException, IOException
{
socket = socketFactory.createSocket(inetaddress, i, inetaddress1, j);
connectAction();
}
public void connect(String s, int i, InetAddress inetaddress, int j)
throws SocketException, IOException
{
socket = socketFactory.createSocket(s, i, inetaddress, j);
connectAction();
}
public void connect(InetAddress inetaddress)
throws SocketException, IOException
{
connect(inetaddress, defaultPort);
}
public void connect(String s)
throws SocketException, IOException
{
connect(s, defaultPort);
}
public void disconnect()
throws IOException
{
socket.close();
input.close();
output.close();
socket = null;
input = null;
output = null;
isConnected = false;
}
public boolean isConnected()
{
return isConnected;
}
public void setDefaultPort(int i)
{
defaultPort = i;
}
public int getDefaultPort()
{
return defaultPort;
}
public void setDefaultTimeout(int i)
{
timeout = i;
}
public int getDefaultTimeout()
{
return timeout;
}
public void setSoTimeout(int i)
throws SocketException
{
socket.setSoTimeout(i);
}
public int getSoTimeout()
throws SocketException
{
return socket.getSoTimeout();
}
public void setTcpNoDelay(boolean flag)
throws SocketException
{
socket.setTcpNoDelay(flag);
}
public boolean getTcpNoDelay()
throws SocketException
{
return socket.getTcpNoDelay();
}
public void setSoLinger(boolean flag, int i)
throws SocketException
{
socket.setSoLinger(flag, i);
}
public int getSoLinger()
throws SocketException
{
return socket.getSoLinger();
}
public int getLocalPort()
{
return socket.getLocalPort();
}
public InetAddress getLocalAddress()
{
return socket.getLocalAddress();
}
public int getRemotePort()
{
return socket.getPort();
}
public InetAddress getRemoteAddress()
{
return socket.getInetAddress();
}
public boolean verifyRemote(Socket socket)
{
InetAddress inetaddress = socket.getInetAddress();
InetAddress inetaddress1 = getRemoteAddress();
return inetaddress.equals(inetaddress1);
}
public void setSocketFactory(SocketFactory socketfactory)
{
if(socketfactory == null)
{
socketFactory = DEFAULTSOCKETFACTORY;
return;
} else
{
socketFactory = socketfactory;
return;
}
}
}
|
import java.awt.Graphics;
import java.util.*;
import java.text.DateFormat;
import java.applet.Applet;
public class Clock extends Applet implements Runnable {
private Thread clockThread = null;
public void start() {
if (clockThread == null) {
clockThread = new Thread(this, "Clock");
clockThread.start();
}
}
public void run() {
Thread myThread = Thread.currentThread();
while (clockThread == myThread) {
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e){
// the VM doesn't want us to sleep anymore,
// so get back to work
}
}
}
public void paint(Graphics g) {
// get the time and convert it to a date
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
// format it and display it
DateFormat dateFormatter = DateFormat.getTimeInstance();
g.drawString(dateFormatter.format(date), 5, 10);
}
// overrides Applet's stop method, not Thread's
public void stop() {
clockThread = null;
}
}
java tutorial上抄的
import java.util.*;
import java.text.DateFormat;
import java.applet.Applet;
public class Clock extends Applet implements Runnable {
private Thread clockThread = null;
public void start() {
if (clockThread == null) {
clockThread = new Thread(this, "Clock");
clockThread.start();
}
}
public void run() {
Thread myThread = Thread.currentThread();
while (clockThread == myThread) {
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e){
// the VM doesn't want us to sleep anymore,
// so get back to work
}
}
}
public void paint(Graphics g) {
// get the time and convert it to a date
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
// format it and display it
DateFormat dateFormatter = DateFormat.getTimeInstance();
g.drawString(dateFormatter.format(date), 5, 10);
}
// overrides Applet's stop method, not Thread's
public void stop() {
clockThread = null;
}
}
java tutorial上抄的
|
Daytime协议
有一个有用的调试工具就是daytime服务。它的作用就是返回当前时间和日期,格式是字符串格式。
基于TCP的daytime服务
daytime服务是基于TCP的应用,服务器在TCP端口13侦听,一旦有连接建立就返回ASCII形式的日期和时间,在传送完后关闭连接。接收到的数据被忽略。
基于UDP的daytime服务
daytime服务也可以使用UDP协议,它的端口也是13,不过UDP是用数据报传送当前时间的。接收到的数据被忽略。
Daytime格式
对于daytime没有特定的格式,建议使用ASCII可打印字符,空格和回车换行符。daytime应该在一行上。下面是两种流行的格式:
Weekday, Month Day, Year Time-Zone
例子:Tuesday, February 22, 1982 17:37:43-PST
和用于SMTP中的格式:
dd mmm yy hh:mm:ss zzz
例子:02 FEB 82 07:59:01 PST
有一个有用的调试工具就是daytime服务。它的作用就是返回当前时间和日期,格式是字符串格式。
基于TCP的daytime服务
daytime服务是基于TCP的应用,服务器在TCP端口13侦听,一旦有连接建立就返回ASCII形式的日期和时间,在传送完后关闭连接。接收到的数据被忽略。
基于UDP的daytime服务
daytime服务也可以使用UDP协议,它的端口也是13,不过UDP是用数据报传送当前时间的。接收到的数据被忽略。
Daytime格式
对于daytime没有特定的格式,建议使用ASCII可打印字符,空格和回车换行符。daytime应该在一行上。下面是两种流行的格式:
Weekday, Month Day, Year Time-Zone
例子:Tuesday, February 22, 1982 17:37:43-PST
和用于SMTP中的格式:
dd mmm yy hh:mm:ss zzz
例子:02 FEB 82 07:59:01 PST
|
从applet里面建立一个udp向服务器的13号端口发送请求,就能获得server的时间了。
(13号端口是时间服务端口)
(13号端口是时间服务端口)
|
web server 支持jsp,asp的话就可以不用applet了。否则就写个server与applet通信,但好像不会用在实际中。
|
作个server与applet通信,从server端取时间,然后显示到客户端!
|
同意: rinehart(rinehart) 的看法首先建立连接,然后从服务器端取得值传到客户端进行显示。我才想通的。
|
你可以参考《java 2编程详解》,里面有udp方式实现获取远程主机时间的例子。如果你用tcp来实时显示时间,我想主机性能将大幅度下降。
|
你需要安装Simple TCP/IP services ,缺省是不装的。
|
你问我windows2000下的时钟服务在Simple TCP/IP services 下面,但我没用过不知道是不是udp的.