主题:向高手们求助,关于UDP监听和SERVLET的。
我想做一个后台程序用来监听UDP端口。如果超过十秒钟没收到数据,则让前端的JSP显示:断线.
这个是后台监听程序
现在问题是:
1,如果UDP端口超过十秒不收到数据并不会显示断线,而是当超过UDP接口超过十秒后再次收到数据后,才会返回断线。
package userve;
import java.io.*;
import java.lang.*;
import java.net.*;
import java.util.*;
public class userve
{
public static String sf;
private DatagramSocket ser;
private DatagramPacket pac;
private byte rb[];
private String rev;
private Calendar cal=Calendar.getInstance();
private Calendar cal1 = Calendar.getInstance();
private java.util.Date date= cal.getTime();
private java.util.Date date1= cal1.getTime();
private long days;
public userve()
{
//Init();
}
public void Init()
{
try
{
sf="sss";
ser=new DatagramSocket(10005);
rb=new byte[1024];
pac=new DatagramPacket(rb,rb.length);
rev="";
int i=0;
while(0==0)
//无数据,则循环
{
cal1 = Calendar.getInstance();
date1=cal1.getTime();
ser.receive(pac);
i=pac.getLength();
//接收数据
if(i>0)
{
//指定接收到数据的长度,可使接收数据正常显示,开始时很容易忽略这一点
rev=new String(rb,0,pac.getLength());
sf="sdf";
cal=Calendar.getInstance();
date=cal.getTime();
System.out.println(sf);
System.out.println(rev);
i=0;//循环接收
}
days=date.getTime()-date1.getTime();
//System.out.println(date1);
//System.out.println(date);
//System.out.println(days);
if (days>=10000)//超过十秒
{
sf="sss";
break;
// System.out.println(date);
// System.out.println(date1);
}
}
ser.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String args[])
{
new userve();
}
}
2,我从网上找了个Servlet配置,目的是让这个程序一启动,就自动将UDP监听运行。
但是,因为我上边那段代码本身是个死循环,所以TOMCAT在启动时会因此卡死,从而无法启动。我应该如何设置以达到目的
WEB.XML,内容如下:
<servlet>
<servlet-name>TestAutoRun</servlet-name>
<servlet-class>com.ee2ee.servlet.TestAutoRun</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>TestAutoRun</servlet-name>
<url-pattern>/servlet/TestAutoRun</url-pattern>
</servlet-mapping>
package com.ee2ee.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
//import java.lang.*;
//import java.net.*;
//import java.util.*;
public class TestAutoRun extends HttpServlet
{
private HttpServletRequest request;
private PrintWriter out;
//当servlet类被加载时,执行本函数
//在本方法中放置代码即可完成自动加载
public void init(ServletConfig config) throws ServletException
{
super.init(config);
//此处放置需要自动执行的代码
System.out.println("I 'm ok now.");
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//以下是标准的GET方法处理,由http的GET方法触发(调用)
this.doPost(request, response);
}
//Process the HTTP Post request
//标准的POST方法处理,由http的POST方法触发(调用)
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html; charset=GB2312");
PrintWriter out = response.getWriter();
this.request = request;
this.out = out;
out.println("<html>");
out.println("<head>");
out.println("</head>");
out.println("<body>");
out.println("this is a test.<br>");
out.println("</body>");
out.println("</html>");
}
//Clean up resources
public void destroy()
{
}
}
这个是后台监听程序
现在问题是:
1,如果UDP端口超过十秒不收到数据并不会显示断线,而是当超过UDP接口超过十秒后再次收到数据后,才会返回断线。
package userve;
import java.io.*;
import java.lang.*;
import java.net.*;
import java.util.*;
public class userve
{
public static String sf;
private DatagramSocket ser;
private DatagramPacket pac;
private byte rb[];
private String rev;
private Calendar cal=Calendar.getInstance();
private Calendar cal1 = Calendar.getInstance();
private java.util.Date date= cal.getTime();
private java.util.Date date1= cal1.getTime();
private long days;
public userve()
{
//Init();
}
public void Init()
{
try
{
sf="sss";
ser=new DatagramSocket(10005);
rb=new byte[1024];
pac=new DatagramPacket(rb,rb.length);
rev="";
int i=0;
while(0==0)
//无数据,则循环
{
cal1 = Calendar.getInstance();
date1=cal1.getTime();
ser.receive(pac);
i=pac.getLength();
//接收数据
if(i>0)
{
//指定接收到数据的长度,可使接收数据正常显示,开始时很容易忽略这一点
rev=new String(rb,0,pac.getLength());
sf="sdf";
cal=Calendar.getInstance();
date=cal.getTime();
System.out.println(sf);
System.out.println(rev);
i=0;//循环接收
}
days=date.getTime()-date1.getTime();
//System.out.println(date1);
//System.out.println(date);
//System.out.println(days);
if (days>=10000)//超过十秒
{
sf="sss";
break;
// System.out.println(date);
// System.out.println(date1);
}
}
ser.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void main(String args[])
{
new userve();
}
}
2,我从网上找了个Servlet配置,目的是让这个程序一启动,就自动将UDP监听运行。
但是,因为我上边那段代码本身是个死循环,所以TOMCAT在启动时会因此卡死,从而无法启动。我应该如何设置以达到目的
WEB.XML,内容如下:
<servlet>
<servlet-name>TestAutoRun</servlet-name>
<servlet-class>com.ee2ee.servlet.TestAutoRun</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>TestAutoRun</servlet-name>
<url-pattern>/servlet/TestAutoRun</url-pattern>
</servlet-mapping>
package com.ee2ee.servlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
//import java.lang.*;
//import java.net.*;
//import java.util.*;
public class TestAutoRun extends HttpServlet
{
private HttpServletRequest request;
private PrintWriter out;
//当servlet类被加载时,执行本函数
//在本方法中放置代码即可完成自动加载
public void init(ServletConfig config) throws ServletException
{
super.init(config);
//此处放置需要自动执行的代码
System.out.println("I 'm ok now.");
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
//以下是标准的GET方法处理,由http的GET方法触发(调用)
this.doPost(request, response);
}
//Process the HTTP Post request
//标准的POST方法处理,由http的POST方法触发(调用)
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html; charset=GB2312");
PrintWriter out = response.getWriter();
this.request = request;
this.out = out;
out.println("<html>");
out.println("<head>");
out.println("</head>");
out.println("<body>");
out.println("this is a test.<br>");
out.println("</body>");
out.println("</html>");
}
//Clean up resources
public void destroy()
{
}
}