📄 protocol.java
字号:
length); } finally { if (!open) { throw new InterruptedIOException("Socket closed"); } } // check res, not count so we can receive zero length datagrams if (res != 0) { break; } /* Wait a while for I/O to become ready */ GeneralBase.iowait(); } 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 consistant with J2SE DatagramSocket we shrink the buffer * length. */ dgram.setLength(count); ipNumber = (int)((res >> 32)); host = getHostByAddr(ipNumber).trim(); port = (int)((res >> 16)) & 0xffff; addr = "datagram://" + host + ":" + port; if (dgram instanceof DatagramObject) { // save this data for sending back a message DatagramObject dh = (DatagramObject)dgram; dh.address = addr; dh.ipNumber = ipNumber; dh.port = port; } else { dgram.setAddress("datagram://" + host + ":" + port); } } } /** * 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 reasssignment.</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(); } /** * Open a connection to a target, and fillin the handle field. * * @param port port to listen on, or 0 to have one selected * @param storage name of current suite storage * * @exception IOException if some other kind of I/O error occurs * or if reserved by another suite */ private native void open0(int port, byte[] storage) throws IOException; /** * Send a datagram, handle field is accessed by the native code. * * @param ipNumber raw IPv4 address * @param port UDP port * @param buf the data buffer * @param off the offset into the data buffer * @param len the length of the data in the buffer * @return number of bytes to send * @exception IOException if an I/O error occurs */ private native int send0(int ipNumber, int port, byte[] buf, int off, int len) throws IOException; /** * Receive a datagram, handle field is accessed by the native code. * * @param buf the data buffer * @param off the offset into the data buffer * @param len the length of the data in the buffer * @return upper 32 bits: raw IPv4 address, middle 16 bits: port, bytes * in datagram * @exception IOException if an I/O error occurs */ private native long receive0(byte[] buf, int off, int len) throws IOException; /** * Close the native socket, handle field is accessed by the native code. * * @exception IOException if an I/O error occurs */ private native void close0() throws IOException; /** Register this object's native cleanup function. */ private native void registerCleanup(); /** * Get a hostname for a raw IPv4 address. * * @param ipn raw IPv4 address * @return hostname or the dot notation if not found */ static native String getHostByAddr(int ipn); /** * Get a raw IPv4 address for hostname. * * @param szHost hostname as ASCII chars with zero terminator * @return raw IPv4 address or -1 if there was an error */ static native int getIpNumber(byte[] szHost); /** * Get the maximum length of a datagram. * * @return maximum length of a datagram * * @exception IOException if an I/O error occurs */ native int getMaximumLength0() throws IOException; /** * Get the nominal length of a datagram. * * @return nomimal length of a datagram * * @exception IOException if an I/O error occurs */ native int getNominalLength0() throws IOException; /** * Native finalizer. */ private native void finalize(); /** * Get the requested IP number. * * @return the IP address as a String */ private native String getHost0(); /** * Get the requested port number. * * @return the port number of the requested end point */ private native int getPort0();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -