?? multicastmessagesocket.java
字號:
/* * This file was derived from libdissipate, an open source SIP library. The original file * was modified on 1/23/2001. Please see * http://www.div8.net/dissipate for more information. * * Copyright (c) 2000 Billy Biggs <bbiggs@div8.net> * * * This library is free software; you can redistribute it and/or modify it * under the terms of the GNU Library General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at your * option) any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public * License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. * *//** * class MulticastMessageSocket * * This is the Multicast implementation of the MessageSocket class. It is used * to allow the client to register using a multicast address (sip.mcast.net). * * This code has been generated using C2J++ * C2J++ is based on Chris Laffra's C2J (laffra@watson.ibm.com) * Read general disclaimer distributed with C2J++ before using this code * For information about C2J++, send mail to Ilya_Tilevich@ibi.com */package org.mitre.jsip;import java.net.SocketException;import java.net.MulticastSocket;import java.net.DatagramPacket;import java.net.InetAddress;import java.net.UnknownHostException;import java.io.IOException;public class MulticastMessageSocket extends MessageSocket{ /** * MulticastMessageSocket */ public MulticastMessageSocket() { type = MCAST; didcomplain = false; } /** * connect * @param portnum * @return int */ public int connect(String hostname, int portnum) { fSendPort = portnum; if (hostname == null) hostname = "224.0.1.75"; sendSocket = null; try { InetAddress raddr = InetAddress.getByName( hostname ); sendSocket = new MulticastSocket(); sendSocket.connect( raddr, portnum ); } catch (UnknownHostException uhe) { System.err.println("Unable to connect to host: " + hostname); return -1; } catch (IOException ioe) { System.err.println("Unable to set up socket to remote host: " + ioe); didcomplain = true; return -1; } return 0; } /** * Connect to the socket * */ public int connect( InetAddress host, int portnum ) { InetAddress sendAddr; fSendPort = portnum; try { if ( host == null ) { sendAddr = InetAddress.getByName( "224.0.1.75" ); } else { sendAddr = host; } sendSocket = new MulticastSocket(); sendSocket.connect( sendAddr, portnum ); } catch (UnknownHostException uhe) { System.err.println("Unable to connect to host: " + host); return -1; } catch (IOException ioe) { System.err.println("Unable to set up socket to remote host: " + ioe); didcomplain = true; return -1; } return 0; } /** * send * @param sendbuffer * @param length * @return int */ public int send(String sendbuffer, int length) { int numbytes; int retries = 0; DatagramPacket dp = new DatagramPacket(sendbuffer.getBytes(), sendbuffer.length()); fSendThread = new DatagramSendThread( dp, sendSocket, fRetryTimer ); fSendThread.start(); return 0; } /** * Not implemented in this class * @param recvbuffer * @param maxlength * @return int */ public int receive(byte[] recvbuffer, int maxlength) { return 0; } /** * Not implemented in this class * @param portnum * @return int */ public int listen(MessageQueue queue) { return 0; } /** * Not implemented in this class * @return int */ public void accept() { } /** * Not implemented in this class * @return int */ public int listenOnEvenPort() { return 0; } public void close() { if ( fSocket != null ) fSocket.close(); if ( sendSocket != null ) sendSocket.close(); fSocket = null; sendSocket = null; } // class variables protected boolean didcomplain; protected MulticastSocket fSocket, sendSocket; protected DatagramPacket fSendDatagram; protected int fSendPort; protected DatagramSendThread fSendThread; protected int fRetryTimer = 5000; protected int fRetryCount = 0; public final static int DEFAULT_MAX_DATAGRAM_SIZE = 1024; public final static int DEFAULT_MAX_RETRIES = 5;}// ***********************************************************************************************/** * a class for dealing with the problems of sending a datagrams */class MulticastSendThread extends Thread{ protected final static boolean fDebugOn = true; DatagramPacket fSendPacket; MulticastSocket fSendSocket; public int fSendState = 0;//neither received nor failed protected int fRetryInterval; //in msec /** * @param sendPacket */ public MulticastSendThread(DatagramPacket sendPacket, MulticastSocket sendSocket, int retryTime) { fSendPacket = sendPacket; fSendSocket = sendSocket; fRetryInterval = retryTime; }//DatagramSendThread /** * proceed with all of the time-consuming stuff here */ public void run() { if (fDebugOn) System.out.println("MulticastSendThread running..."); try { if (fDebugOn) System.out.println("MulticastSendThread sending..."); byte ttl = 15; fSendSocket.send(fSendPacket, ttl); fSendState = 0;//sent OK! if (fDebugOn) System.out.println("DatagramSendThread sent ok!"); try {this.sleep(fRetryInterval);} catch (InterruptedException iEx) { if (fDebugOn) System.err.println("DatagramSendThread sleep interrupted!"); fSendState = -1; } } catch (IOException sendEx) { if (fDebugOn) System.err.println("send failed! " + sendEx); fSendState = -1; } finally { if (fSendSocket != null) fSendSocket.close(); } } // run }/* class DatagramSendThread */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -