Пример #1
0
  /** 启动初始化,他确定网络中有多少个其它UDPBase和相关的信息 */
  public void initNet() throws UDPBaseException {
    try {
      mainThread = new SoleThread(this);

      localIP = InetAddress.getLocalHost().getHostAddress();
      int i = localIP.lastIndexOf('.');
      BROADCAST_ADDR = localIP.substring(0, i) + ".255";
      // System.out.println ("lip=="+localIP) ;
      // sendSocket = new DatagramSocket () ;
      // recvSocket = new MulticastSocket (RECV_PORT) ;
      recvSocket = new DatagramSocket(RECV_PORT);
      sendSocket = recvSocket;
      // recvAckSocket = new MulticastSocket (RECV_ACK_PORT) ;
      group = InetAddress.getByName(BROADCAST_ADDR);
      // recvSocket.joinGroup (group) ;
      // recvAckSocket.joinGroup (group) ;

      procMsgThd = new ProcMsgThd();
      procMsgThd.start();
      //
      mainThread.start();
    } catch (Exception e) {
      e.printStackTrace();
      throw new UDPBaseException("UDPBase init() error=\n" + e.toString());
    }
  }
Пример #2
0
  public void destory() {
    procMsgThd.stop0();
    procMsgThd.interrupt();
    mainThread.interrupt();
    if (group != null) {
      try {
        if (recvSocket instanceof MulticastSocket) {
          ((MulticastSocket) recvSocket).leaveGroup(group);
        }
      } catch (IOException ioe) {
      }
    }

    sendSocket.close();
    recvSocket.close();
  }
Пример #3
0
  public void initGroup() throws UDPBaseException {
    try {
      mainThread = new SoleThread(this);

      sendSocket = new DatagramSocket();
      recvSocket = new MulticastSocket(RECV_PORT);
      // recvAckSocket = new MulticastSocket (RECV_ACK_PORT) ;
      group = InetAddress.getByName(GROUP_BROADCAST_ADDR);
      ((MulticastSocket) recvSocket).joinGroup(group);
      // recvAckSocket.joinGroup (group) ;
      localIP = InetAddress.getLocalHost().getHostAddress();

      procMsgThd = new ProcMsgThd();
      procMsgThd.start();
      //
      mainThread.start();

      bGroup = true;
    } catch (Exception e) {
      e.printStackTrace();
      throw new UDPBaseException("UDPBase init() error=\n" + e.toString());
    }
  }
Пример #4
0
  public void run(SoleThread st) {
    try {
      byte[] buf = new byte[MAX_HEADER_LENGTH + MAX_PACKET_LENGTH];
      int len;
      byte[] tar = null;

      while (st.isSole()) {
        DatagramPacket packet = new DatagramPacket(buf, buf.length);
        // System.out.println("recv...");
        recvSocket.receive(packet);

        len = packet.getLength();
        tar = new byte[len];
        System.arraycopy(buf, 0, tar, 0, len);

        // processMsg (tar);
        quickQueue.put(tar);
      }
    } catch (Exception e) {
      // throw new UDPBaseException ("UDPBase run() error=\n"+e.toString());
      System.out.println("Something error in recv thread!");
      e.printStackTrace();
    }
  }
Пример #5
0
  public void initVirtual(String virtualip) throws UDPBaseException {
    if (virtualip == null) {
      virtualip = "vip:" + System.currentTimeMillis();
    }
    try {
      mainThread = new SoleThread(this);

      sendSocket = new DatagramSocket();
      recvSocket = new MulticastSocket(RECV_PORT);
      // recvAckSocket = new MulticastSocket (RECV_ACK_PORT) ;
      group = InetAddress.getByName(GROUP_BROADCAST_ADDR);
      ((MulticastSocket) recvSocket).joinGroup(group);
      // recvAckSocket.joinGroup (group) ;
      localIP = virtualip;

      procMsgThd = new ProcMsgThd();
      procMsgThd.start();
      //
      mainThread.start();
    } catch (Exception e) {
      e.printStackTrace();
      throw new UDPBaseException("UDPBase init() error=\n" + e.toString());
    }
  }