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

📄 datagramclient.java

📁 这是一个以JAVA编写的程序,本人还没有试过,是一个简单的温度控制系统
💻 JAVA
字号:
package net.sf.dz.pnp.custom;import java.io.IOException;import java.net.DatagramPacket;import java.net.InetAddress;import java.util.StringTokenizer;import org.freehold.jukebox.logger.LogChannel;import net.sf.dz.pnp.MulticastClient;import net.sf.dz.pnp.MulticastEvent;import net.sf.dz.util.MessageDigestFactory;/** * @author Copyright &copy; <a href="mailto:vt@freehold.crocodile.org">Vadim Tkachenko</a> 2004 * @version $Id: DatagramClient.java,v 1.2 2004/06/28 20:35:49 vtt Exp $ */abstract public class DatagramClient extends MulticastClient {    public static final LogChannel CH_DC = new LogChannel("DatagramClient");    abstract protected void receive(DatagramPacket packet) throws IOException;        protected final MulticastEvent receive() throws Throwable {            byte data[] = new byte[1000];        DatagramPacket packet = new DatagramPacket(data, data.length);                while ( true ) {                    receive(packet);                        byte received[] = packet.getData();            String message = new String(received);            InetAddress source = packet.getAddress();                        // VT: IMPORTANT: Unless the following step is performed, a bug            // somewhere deep in JVM is triggered, and even though the            // string will look the same on debug console, .equals will            // return false, and message digest will be different!                        message = message.substring(0, packet.getLength());                        if ( message == null ) {                            complain(LOG_WARNING, CH_DC, "Null packet received");                continue;            }                        if ( message.startsWith("SHA") ) {                            // VT: FIXME: SHA should have a fixed length, but since I don't                // remember it now, I'll calculate it                                int offset = message.indexOf("}");                                if ( offset == -1 ) {                                    throw new IllegalStateException("Can't find SHA end offset in '" + message + "'");                }                String md = message.substring(4, offset);                String payload = message.substring(offset + 2);                                complain(LOG_DEBUG, CH_DC, "Source: " + packet.getAddress());                complain(LOG_DEBUG, CH_DC, "SHA: " + md);                complain(LOG_DEBUG, CH_DC, "Payload: '" + payload + "'");                                String md2 = new MessageDigestFactory().getSHA(payload);                                if ( !md.equals(md2) ) {                                    complain(LOG_WARNING, CH_DC, "SHA mismatch: " + md2);                                        // Have to discard the packet, it's garbled                    // Let's receive another                } else {                                    // We have a good packet                                        return createEvent(source, payload);                }                        } else {                            // This means that we don't have the message digest. Too                // bad, but let's allow this so far.                                //return new MulticastEvent(source, buf[0], buf[1]);                                // VT: FIXME: Unlike the previous case, there's no guarantee                // we won't screw up here, so better check any exceptions...                                return createEvent(source, message);            }        }    }        private MulticastEvent createEvent(InetAddress source, String payload) {            StringTokenizer st = new StringTokenizer(payload, "/");                // This *must* be present                String serviceSignature = st.nextToken();        int port = Integer.parseInt(st.nextToken());        boolean secure = "secure".equals(st.nextToken());                // If nothing else is available, the hell with it                String message = "";                if ( st.hasMoreTokens() ) {                    // If there's more than one slash, we'll have to reassemble the            // message                        if ( !"".equals(message) ) {                            message += "/";            }                        message += st.nextToken();        }                return new MulticastEvent(source, serviceSignature, port, secure, message.trim());    }}

⌨️ 快捷键说明

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