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

📄 protocol.java

📁 This is a resource based on j2me embedded,if you dont understand,you can connection with me .
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            /*             * JTWI security check, untrusted MIDlets cannot open             * WAP gateway ports 9200-9203.             */            if (!ownerTrusted && (locPort >= 9200 && locPort <= 9203)) {                throw new SecurityException(                    "Target port denied to untrusted applications");            }            while (true) {                int res;                try {                    res = send0(ipNumber, locPort,                        dgram.getData(), dgram.getOffset(), length);                } finally {                    if (!open) {                        throw new InterruptedIOException("Socket closed");                    }                }                if (res == dgram.getLength()) {                    break;                }                if (res != 0) {                    throw new IOException("Failed to send datagram");                }            }        }    }    /**     * Receive a datagram.     *     * @param     dgram        a datagram     * @exception IOException  if an I/O error occurs     */    public void receive(Datagram dgram)        throws IOException {        /*         * Synchronization should be done for a reader lock and not for         * the DatagramObject. Reader lock would ensure that no         * more than one thread is performing read for the same socket(handle)         * at the same time.         */        synchronized (readerLock) {            int length;            long res;            int count;            int ipNumber;            String locHost;            int locPort;            String addr;            ensureOpen();            length = dgram.getLength();            if (length <= 0) {                throw new IOException("Bad datagram length");            }            while (true) {                try {                    res = receive0(dgram.getData(),                        dgram.getOffset(), length);                } finally {                    if (!open) {                        throw new InterruptedIOException("Socket closed");                    }                }                // check res, not count so we can receive zero length datagrams                if (res != 0) {                    break;                }            }            count = ((int)res) & 0xffff;            /*             * There should be another field for bytes received so             * the datagram can be reused without an extra effort, but             * to be consistent with J2SE DatagramSocket we shrink the buffer             * length.             */            dgram.setLength(count);            ipNumber = (int)((res >> 32));            locHost = addrToString(ipNumber).trim();            locPort = (int)((res >> 16)) & 0xffff;            addr = "datagram://" + locHost + ":" + locPort;            if (dgram instanceof DatagramObject) {                // save this data for sending back a message                DatagramObject dh = (DatagramObject)dgram;                dh.address = addr;                dh.ipNumber = ipNumber;                dh.port = locPort;            } else {                dgram.setAddress("datagram://" + locHost + ":" + locPort);            }        }    }    /**     * Close the connection to the target.     *     * @exception IOException  if an I/O error occurs     */    public void close() throws IOException {        if (open) {            open = false;	    close0();        }    }    /**     * Get a new datagram object.     *     * @param  size            the length of the buffer to be allocated     *                         for the datagram     * @return                 a new datagram     * @exception IOException  if an I/O error occurs     * @exception IllegalArgumentException if the length is negative     *                                     or larger than the buffer    */    public Datagram newDatagram(int size) throws IOException {        Datagram dgram;        ensureOpen();        if (size < 0) {            throw new IllegalArgumentException("Size is negative");        }	byte[] buf = new byte[size];        dgram = new DatagramObject(buf, size);        if (host != null) {	    try {		dgram.setAddress("datagram://" + host + ":" + port);	    } catch (IllegalArgumentException iae) {		// Intercept a bad address, here.		// It'll be caught on send if used.	    }        }        return dgram;    }    /**     * Get a new datagram object.     *     * @param  size            the length of the buffer to be allocated     *                         for the datagram     * @param     addr         the address to which the datagram must go     * @return                 a new datagram     * @exception IOException  if an I/O error occurs     * @exception IllegalArgumentException if the length is negative or     *                         larger than the buffer, or if the address     *                         parameter is invalid     */    public Datagram newDatagram(int size, String addr) throws IOException {        Datagram dgram = createDatagram(true, null, size);        dgram.setAddress(addr); // override the address        return dgram;    }    /**     * Get a new datagram object.     *     * @param  buf             the buffer to be used in the datagram     * @param  size            the length of the buffer to be allocated     *                         for the datagram     * @return                 a new datagram     * @exception IOException  if an I/O error occurs     * @exception IllegalArgumentException if the length is negative or     *                         larger than the buffer, or if the address     *                         or buffer parameters is invalid     */    public Datagram newDatagram(byte[] buf, int size) throws IOException {        return createDatagram(false, buf, size);    }    /**     * Get a new datagram object.     *     * @param  buf             the buffer to be used in the datagram     * @param  size            the length of the buffer to be allocated     *                         for the datagram     * @param     addr         the address to which the datagram must go     * @exception IOException  if an I/O error occurs     * @return                 a new datagram     */    public Datagram newDatagram(byte[] buf, int size, String addr)        throws IOException {        Datagram dgram = createDatagram(false, buf, size);        dgram.setAddress(addr); // override the address        return dgram;    }    /**     * Create a new datagram object with error checking.     * If there is a <code>host</code> associated with the connection,     * set the address of the datagram.     *     * @param  createBuffer    if true the buffer is created     * @param  buf             the buffer to be used in the datagram     * @param  size            the length of the buffer to be allocated     *                         for the datagram     * @return                 a new datagram     * @exception IOException  if an I/O error occurs, or the connection     *                         was closed     * @exception IllegalArgumentException if the length is negative or     *                         larger than the buffer, or if the address     *                         or buffer parameters is invalid     */    private Datagram createDatagram(boolean createBuffer, byte[] buf, int size)        throws IOException {        Datagram dgram;        ensureOpen();        if (size < 0) {            throw new IllegalArgumentException("Size is negative");        }        if (createBuffer) {            buf = new byte[size];        } else if (buf == null) {            throw new IllegalArgumentException("Buffer is invalid");        } else if (size > buf.length) {            throw new                IllegalArgumentException("Size bigger than the buffer");        }        dgram = new DatagramObject(buf, size);        if (host != null) {            dgram.setAddress("datagram://" + host + ":" + port);        }        return dgram;    }    /**     * Gets the local address to which the socket is bound.     *     * <P>The host address(IP number) that can be used to connect to this     * end of the socket connection from an external system.     * Since IP addresses may be dynamically assigned a remote application     * will need to be robust in the face of IP number reassignment.</P>     * <P> The local hostname (if available) can be accessed from     * <code>System.getProperty("microedition.hostname")</code>     * </P>     *     * @return the local address to which the socket is bound.     * @exception  IOException  if the connection was closed     * @see ServerSocketConnection     */    public String getLocalAddress() throws IOException {        ensureOpen();	return getHost0();    }    /**     * Returns the local port to which this socket is bound.     *     * @return the local port number to which this socket is connected     * @exception  IOException  if the connection was closed     * @see ServerSocketConnection     */    public int getLocalPort() throws IOException {        ensureOpen();	return getPort0();    }    /**     * Opens a datagram connection on the given port.     *     * @param inpPort port to listen on, or 0 to have one selected     * @param suiteId the ID of the current midlet suite     *     * @exception IOException  if some other kind of I/O error occurs     *  or if reserved by another suite     */    private native void open0(int inpPort, int suiteId)        throws IOException;    /**     * Sends a datagram.     *     * @param ipNumber raw IPv4 address of the remote host     * @param inpPort UDP port of the remote host     * @param buf the data buffer to send     * @param off the offset into the data buffer     * @param len the length of the data in the buffer     * @return number of bytes sent     * @exception IOException  if an I/O error occurs     */    private native int send0(int ipNumber, int inpPort,                             byte[] buf, int off, int len)        throws IOException;    /**     * Receives a datagram.     *     * @param buf the data buffer     * @param off the offset into the data buffer     * @param len the length of the data in the buffer     * @return The upper 32 bits contain the raw IPv4 address of the     *         host the datagram was received from. The next 16 bits     *         contain the port. The last 16 bits contain the number     *         of bytes received.     * @exception IOException  if an I/O error occurs     */    private native long receive0(byte[] buf, int off, int len)        throws IOException;    /**     * Closes the datagram connection.     *     * @exception IOException  if an I/O error occurs     */    private native void close0()        throws IOException;    /**     * Get a string representation for the given raw IPv4 address.     *     * @param ipn raw IPv4 address     * @return dotted-quad     */    static native String addrToString(int ipn);    /**     * Get a raw IPv4 address for the given hostname.     *     * @param sHost the hostname to lookup     * @return raw IPv4 address or -1 if there was an error     */    public static native int getIpNumber(String sHost);    /**     * Get the maximum length of a datagram.     *     * @return the maximum length     *     * @exception IOException  if an I/O error occurs     */    native int getMaximumLength0()        throws IOException;    /**     * Gets the nominal length of a datagram.     *     * @return the nominal length     *     * @exception IOException  if an I/O error occurs     */    native int getNominalLength0()        throws IOException;    /**     * Native finalizer.     */    private native void finalize();    /**     * Gets the local IP number.     *     * @return the local IP address as a dotted-quad <tt>String</tt>     */    private static native String getHost0();    /**     * Gets the local port number of this datagram connection.     *     * @return the port number     */    private native int getPort0();}

⌨️ 快捷键说明

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