六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 91|回复: 0

udp简单通讯示例---心跳示例

[复制链接]

升级  26%

3

主题

3

主题

3

主题

童生

Rank: 1

积分
13
 楼主| 发表于 2013-2-5 09:10:39 | 显示全部楼层 |阅读模式
//发送心跳
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<barList.size();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<barList.size();i++){
                    Global.setBarStatus(Global.BAR_STATUS_UNONLINE, barList.get(i).toString());
                    }
                    }
                    }
                }
            }catch(Exception e){
            logger.error(e,e);
            }
        }
}
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表