当前位置:我的异常网» 软件架构设计 » udp简略通讯示例-心跳示例
udp简略通讯示例-心跳示例
www.myexceptions.net 网友分享于:2013-09-04 浏览:44次
udp简单通讯示例---心跳示例
//发送心跳
new UDPToPointCenter(18056).start();
public class UDPToPointCenter extends Thread {
private static Logger logger = Logger.getLogger("com.hintsoft.pubwin.netcafe.socket.UDPToPointCenter");
private int listenPort; //发送的端口号
private static DatagramSocket sock;
private Timer timer;
private PooledExecutor _processExecutor = new PooledExecutor(new LinkedQueue(), 1); //序列化
protected static PooledExecutor clientPool;
public UDPToPointCenter(int aListenPort) {
try {
this.timer = new Timer();
this.listenPort = aListenPort;
sock = new DatagramSocket(this.listenPort);
this.timer.schedule(new ConnSvrTask(),1 * 10 * 1000,Global.UDP_SYNC_SIGNAL_INTERVAL);
_processExecutor.setMaximumPoolSize(5);
_processExecutor.setMinimumPoolSize(1);
_processExecutor.setKeepAliveTime(10 * 1000);
_processExecutor.waitWhenBlocked();
_processExecutor.createThreads(1);
}
catch (SocketException e) {
logger.error(e, e);
}
catch (Exception e) {
logger.error(e, e);
}
}
private class ConnSvrTask extends TimerTask {
private Logger logger = Logger.getLogger("com.hintsoft.pubwin.netcafe.socket.ConnSvrTask");
public ConnSvrTask() {
super();
}
public void run() {
try {
byte[] b = new byte[21];
DatagramPacket packet = new DatagramPacket(b, b.length);
// 发送心跳
try {
byte[] desData = new byte[1024];
packet = new DatagramPacket(desData, desData.length, InetAddress.getByName(CachedConfigObject.getServerCenterCfgOtherObj().getPointCenterIP()), 18060);
sock.send(packet);
}catch (UnknownHostException e) {
logger.error(e, e);
}catch (IOException e) {
logger.error(e, e);
}
}
catch (Exception e) {
logger.error(e, e);
}
}
}
}
//接收心跳
new UderServerListen(18060).start();
public class UderServerListen extends Thread {
private static Logger logger = Logger.getLogger("com.hintsoft.pubwin.netcafe.socket.UdpServerClient");
private static DatagramSocket sock;
private List iplist;
private int listenPort;
private PooledExecutor _processExecutor = new PooledExecutor(new LinkedQueue(), 1);
private long udpBarBeg = System.currentTimeMillis();//开始搜索心跳的时间
public UderServerListen(int listenPort){
try {
this.iplist = Global.getAllIpAddress();
this.listenPort = listenPort;
sock = new DatagramSocket(this.listenPort);
_processExecutor.setMaximumPoolSize(5);
_processExecutor.setMinimumPoolSize(1);
_processExecutor.setKeepAliveTime(10 * 1000);
_processExecutor.waitWhenBlocked();
_processExecutor.createThreads(1);
}
catch (SocketException e) {
logger.error(e, e);
}catch (Exception e) {
logger.error(e, e);
}
}
public void run() {
if (sock == null) {
return;
}
while(true){
try {
byte[] data = new byte[1024];
DatagramPacket packet = new DatagramPacket(data, data.length);
sock.receive(packet);
UDPPacketProcessor processor = new UDPPacketProcessor(packet);
_processExecutor.execute(processor);
}
catch (java.net.SocketException e) {
logger.error(e, e);
}
catch (java.net.UnknownHostException e) {
logger.error(e, e);
}
catch (java.io.IOException e) {
logger.error(e, e);
}
catch (Exception e) {
logger.error(e, e);
}
}
}
private class UDPPacketProcessor implements Runnable {
private DatagramPacket _packet;
public UDPPacketProcessor(DatagramPacket packet) {
_packet = packet;
}
public void run() {
try {
List barList = new ArrayList();
barList.addAll(iplist);
String clientIP = _packet.getAddress().getHostAddress();
if(logger.isDebugEnabled()){
logger.debug("接收到的ip "+clientIP);
}
if (iplist.contains(clientIP)) {
Global.setBarStatus(Global.BAR_STATUS_ONLINE, clientIP);
for(int index=0;index
if(barList.get(index).equals(clientIP)){
barList.remove(index);
}
}
}else{
//判断心跳断开时间是否超过1小时,超过1小时则视为不在线
if (System.currentTimeMillis() - udpBarBeg > 1 * 60 * 60 * 1000L){
if(null!=barList && barList.size()>0){
for(int i=0;i
Global.setBarStatus(Global.BAR_STATUS_UNONLINE, barList.get(i).toString());
}
}
}
}
}catch(Exception e){
logger.error(e,e);
}
}
}
}
文章评论