FileDocCategorySizeDatePackage
Socket.javaAPI DocJ2ME MIDP 2.06566Thu Nov 07 12:02:22 GMT 2002com.sun.midp.io.j2me.serversocket

Socket

public class Socket extends Object implements ServerSocketConnection
StreamConnectionNotifier to the TCP Server Socket API.
author
Nik Shaylor
version
1.0 10/08/99

Fields Summary
private int
handle
Socket object used by native code, for now must be the first field.
boolean
connectionOpen
Flag to indicate connection is currently open.
Constructors Summary
Methods Summary
private native intaccept()
Accepts a TCP connection socket handle to a client, accesses the handle field.

return
TCP connection socket handle
exception
IOException If some other kind of I/O error occurs.

public synchronized StreamConnectionacceptAndOpen()
Returns a connection that represents a server side socket connection.

Polling the native code is done here to allow for simple asynchronous native code to be written. Not all implementations work this way (they block in the native code) but the same Java code works for both.

return
a socket to communicate with a client.
exception
IOException if an I/O error occurs when creating the input stream


        com.sun.midp.io.j2me.socket.Protocol con;

	ensureOpen();

        while (true) {
            int handle = accept();
            if (handle >= 0) {
                con = new com.sun.midp.io.j2me.socket.Protocol();
                con.open(handle);
                break;
            }

            /* Wait a while for I/O to become ready */
            GeneralBase.iowait(); 
        }

        return con;
    
public voidclose()
Closes the connection, accesses the handle field.

exception
IOException if an I/O error occurs when closing the connection

	if (connectionOpen) {
	    close0();
	    connectionOpen = false;
	}
    
public native voidclose0()
Closes the connection, accesses the handle field.

exception
IOException if an I/O error occurs when closing the connection

voidensureOpen()
Checks if the connection is open.

exception
IOException is thrown, if the stream is not open

        if (!connectionOpen) {
            throw new IOException("Connection closed");
        }
    
private native voidfinalize()
Native finalizer

public java.lang.StringgetLocalAddress()
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")

return
the local address to which the socket is bound
exception
IOException if the connection was closed
see
ServerSocketConnection

	ensureOpen();
	return getLocalAddress0();
    
private native java.lang.StringgetLocalAddress0()
Gets the requested printable IP number.

return
the IP address as a String

public intgetLocalPort()
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

	ensureOpen();
	return getLocalPort0();
    
private native intgetLocalPort0()
Returns the local port to which this socket is bound.

return
the local port number to which this socket is connected

public voidopen(int port)
Opens a port to listen on.

param
port TCP to listen on
exception
IOException if some other kind of I/O error occurs


                                      
          
        Scheduler scheduler = Scheduler.getScheduler();
        MIDletSuite midletSuite = scheduler.getMIDletSuite();
	String root = midletSuite.getStorageName();

        byte[] asciiStorage = Util.toCString(root);

        open0(port > 0 ? port : 0, asciiStorage);
	connectionOpen = true;

        port = getLocalPort();

        // check permission after the open so we can get the assigned port
        try {
            // When asking permission use Internet protocol name.
            midletSuite.checkForPermission(Permissions.TCP_SERVER,
                "TCP://:" + port);
        } catch (SecurityException e) {
            close();
            throw e;
        } catch (InterruptedException ie) {
            throw new InterruptedIOException(
                "Interrupted while trying to ask the user permission");
        }

        registerCleanup();
    
public native voidopen0(int port, byte[] storage)
Opens a native socket and put its handle in the handle field.

Called by socket Protocol class after it parses a given URL and finds no host.

param
port TCP port to listen for connections on
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 voidregisterCleanup()
Registers with the native cleanup code, accesses the handle field.