Methods Summary |
---|
public void | close()Close the connection to the target.
if (open) {
open = false;
close0();
}
|
private native void | close0()Close the native socket, handle field is accessed by the native code.
|
private Datagram | createDatagram(boolean createBuffer, byte[] buf, int size)Create a new datagram object with error checking.
If there is a host associated with the connection,
set the address of the datagram.
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;
|
void | ensureOpen()Ensure that the connection is open.
if (!open) {
throw new IOException("Connection closed");
}
|
private native void | finalize()Native finalizer.
|
private native java.lang.String | getHost0()Get the requested IP number.
|
static native java.lang.String | getHostByAddr(int ipn)Get a hostname for a raw IPv4 address.
|
static native int | getIpNumber(byte[] szHost)Get a raw IPv4 address for hostname.
|
public java.lang.String | getLocalAddress()Gets the local address to which the socket is bound.
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.
The local hostname (if available) can be accessed from
System.getProperty("microedition.hostname")
ensureOpen();
return getHost0();
|
public int | getLocalPort()Returns the local port to which this socket is bound.
ensureOpen();
return getPort0();
|
public int | getMaximumLength()Get the maximum length a datagram can be.
ensureOpen();
return getMaximumLength0();
|
native int | getMaximumLength0()Get the maximum length of a datagram.
|
public int | getNominalLength()Get the nominal length of a datagram.
ensureOpen();
return getNominalLength0();
|
native int | getNominalLength0()Get the nominal length of a datagram.
|
private native int | getPort0()Get the requested port number.
|
public static void | initSecurityToken(SecurityToken token)Initializes the security token for this class, so it can
perform actions that a normal MIDlet Suite cannot.
NetworkConnectionBase.initializeNativeNetwork();
if (classSecurityToken != null) {
return;
}
classSecurityToken = token;
|
public Datagram | newDatagram(int size, java.lang.String addr)Get a new datagram object.
Datagram dgram = createDatagram(true, null, size);
dgram.setAddress(addr); // override the address
return dgram;
|
public Datagram | newDatagram(byte[] buf, int size)Get a new datagram object.
return createDatagram(false, buf, size);
|
public Datagram | newDatagram(byte[] buf, int size, java.lang.String addr)Get a new datagram object.
Datagram dgram = createDatagram(false, buf, size);
dgram.setAddress(addr); // override the address
return dgram;
|
public Datagram | newDatagram(int size)Get a new datagram object.
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;
|
private native void | open0(int port, byte[] storage)Open a connection to a target, and fillin the handle field.
|
public Connection | openPrim(java.lang.String name, int mode, boolean timeouts)Open a connection to a target.
The name string for this protocol should be:
"//[address:][port]"
int incommingPort = 0;
Scheduler scheduler = Scheduler.getScheduler();
MIDletSuite midletSuite = scheduler.getMIDletSuite();
// parse name into host and port
HttpUrl url = new HttpUrl("datagram", name);
/*
* Since we reused the <code>HttpUrl</code> parser, we must
* make sure that there was nothing past the authority in the
* URL.
*/
if (url.path != null || url.query != null || url.fragment != null) {
throw new IllegalArgumentException("Malformed address");
}
host = url.host;
port = url.port;
if (name.charAt(0) != '/" || name.charAt(1) != '/") {
throw new IllegalArgumentException(
"Protocol must start with slash slash");
}
/*
* If 'host' == null then we are a server endpoint at
* port 'port'.
*
* If 'host' != null we are a client endpoint at a port
* decided by the system and the default address for
* datagrams to be send is 'host':'port'
*
* If 'host' and 'port' are omitted in
* the name, then
* the application is requesting a system assigned port
* number.
*/
if (host == null) {
try {
// When asking permission use Internet protocol name.
midletSuite.checkForPermission(Permissions.UDP_SERVER,
"UDP:" + name);
} catch (SecurityException e) {
// Give back the connection to AMS
PushRegistryImpl.checkInConnectionInternal(
classSecurityToken, "datagram:" + name);
throw e;
} catch (InterruptedException ie) {
throw new InterruptedIOException(
"Interrupted while trying to ask the user permission");
}
if (port > 0) {
incommingPort = port;
}
} else {
if (port < 0) {
throw new IllegalArgumentException("Missing port number");
}
try {
// When asking permission use Internet protocol name.
midletSuite.checkForPermission(Permissions.UDP,
"UDP:" + name);
} catch (InterruptedException ie) {
throw new InterruptedIOException(
"Interrupted while trying to ask the user permission");
}
}
/* Check the mode parameter. (See NetworkConnectionAdapter). */
switch (mode) {
case Connector.READ:
case Connector.WRITE:
case Connector.READ_WRITE:
break;
default:
throw new IllegalArgumentException("Illegal mode");
}
String root = midletSuite.getStorageName();
byte[] asciiStorage = Util.toCString(root);
open0(incommingPort, asciiStorage);
registerCleanup();
open = true;
return this;
|
public synchronized void | receive(Datagram dgram)Receive a datagram.
synchronized (dgram) {
int length;
long res;
int count;
int ipNumber;
String host;
int port;
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;
}
/* 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);
}
}
|
private native long | receive0(byte[] buf, int off, int len)Receive a datagram, handle field is accessed by the native code.
|
private native void | registerCleanup()Register this object's native cleanup function.
|
public void | send(Datagram dgram)Send a datagram.
synchronized (dgram) {
int length;
int ipNumber;
int port;
ensureOpen();
length = dgram.getLength();
// allow zero length datagrams to be sent
if (length < 0) {
throw new IOException("Bad datagram length");
}
if (dgram instanceof DatagramObject) {
DatagramObject dh = (DatagramObject)dgram;
ipNumber = dh.ipNumber;
if (ipNumber == 0) {
throw new IOException(
"No address in datagram");
}
port = dh.port;
} else {
// address is a datagram url
String addr;
HttpUrl url;
String host;
addr = dgram.getAddress();
if (addr == null) {
throw new IOException(
"No address in datagram");
}
url = new HttpUrl(addr);
host = url.host;
port = url.port;
if (host == null) {
throw new IOException("Missing host");
}
if (port == -1) {
throw new IOException("Missing port");
}
ipNumber = getIpNumber(Util.toCString(host));
if (ipNumber == -1) {
throw new IOException("Invalid host");
}
}
while (true) {
int res;
try {
res = send0(ipNumber, port, 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");
}
/* Wait a while for I/O to become ready */
GeneralBase.iowait();
}
}
|
private native int | send0(int ipNumber, int port, byte[] buf, int off, int len)Send a datagram, handle field is accessed by the native code.
|