⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplebroadcastserver.java

📁 这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统
💻 JAVA
字号:
package net.sf.dz.pnp.custom;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.net.UnknownHostException;import java.util.HashSet;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Set;import java.util.StringTokenizer;import org.freehold.jukebox.logger.LogChannel;import net.sf.dz.pnp.MulticastServer;import net.sf.dz.util.HostHelper;import net.sf.dz.util.MessageDigestFactory;/** * @author Copyright &copy; <a href="mailto:vt@freehold.crocodile.org">Vadim Tkachenko</a> 2004 * @version $Id: SimpleBroadcastServer.java,v 1.2 2004/06/28 20:35:49 vtt Exp $ */public class SimpleBroadcastServer extends MulticastServer {    public static final LogChannel CH_SMS = new LogChannel("SimpleBroadcastServer");        private Set allowedAddresses = new HashSet();    private int port = 5001;        private DatagramSocket socket;    private Set addressSet = new HashSet();    public SimpleBroadcastServer() {            super();    }    /**     * Create an instance.     *     * @param allowedAddresses Set of <code>String</code> objects     * representing internet addresses we're allowed to announce from. If     * the set is empty, it is considered a wildcard, and all local     * addresses will be used.     *     * @param port Port to send the announce on.     */    public SimpleBroadcastServer(Set allowedAddresses, int port) throws UnknownHostException {            super();                if ( allowedAddresses == null ) {                    throw new IllegalArgumentException("allowed addresses set can't be null");        }                for ( Iterator i = allowedAddresses.iterator(); i.hasNext(); ) {                    InetAddress addr = InetAddress.getByName(i.next().toString());                        // VT: Can't complain here, logger is not yet set            //complain(LOG_DEBUG, CH_SMS, "Allowed: " + addr);                    this.allowedAddresses.add(addr);        }                this.port = port;    }    protected void startup() throws Throwable {            socket = new DatagramSocket();        socket.setBroadcast(true);                // Compose a broadcast address                // VT: FIXME: Right now, the broadcast address will be a class C        // broadcast address. Unlikely, but it may happen that the host        // address is class B or even class A address, in this case this        // address will not be correct. I guess it would be a good idea to        // determine the address class and adjust accordingly.                Set localAddresses = HostHelper.getLocalAddresses();                for ( Iterator i = localAddresses.iterator(); i.hasNext(); ) {                    InetAddress niAddress = (InetAddress)i.next();            String hostAddress = niAddress.getHostAddress();                        if ( "127.0.0.1".equals(hostAddress) ) {                            // Just add it                                addressSet.add(niAddress);                        } else {                            // Let's see first if we're allowed to broadcast on this                // address                                if ( !allowedAddresses.isEmpty() && allowedAddresses.contains(niAddress) ) {                                StringTokenizer st = new StringTokenizer(hostAddress, ".");                                            // VT: FIXME: Damn it, is it 0 or 255? I remember 255,                    // but why does 0 work???                                        String targetAddress = st.nextToken() + "." + st.nextToken() + "." + st.nextToken() + ".0";                                        niAddress = InetAddress.getByName(targetAddress);                    addressSet.add(niAddress);                }            }        }                // VT: FIXME: Right now, only the last address found will be used                complain(LOG_INFO, CH_SMS, "Announcing on " + addressSet + ":" + port);    }        protected void shutdown(Throwable cause) throws Throwable {            socket.close();    }        protected void announce() throws Throwable {            String payload = getMessage();                if ( payload == null ) {                    // This shouldn't have happened, but anyway...                        return;        }                String md = new MessageDigestFactory().getSHA(payload);        String message = "SHA{" + md + "} " + payload;                byte data[] = message.getBytes();                for ( Iterator i = addressSet.iterator(); i.hasNext(); ) {                    InetAddress address = (InetAddress)i.next();            DatagramPacket packet = new DatagramPacket(data, message.length(), address, port);                        socket.send(packet);        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -