Cosc 4755 Mobile networking Overview and Stream, Socket, and Datagram.
A Overview Generic Connection Framework (GCF) – In Javax.microedition.io Now includes files, more network protocols, smart cards, RFID cards, bluetooth, and bar codes. – And is being ported back to Java SE as JSR 197
Some Connection Schemes URL SchemeProtocolGCF TypeDefined byRequired Btl2capBluetoothL2CAPConnectionJSR 82No datagramDatagramDatagramConnectionCLDC, CDC, JSR 197 No fileFile accessFileConnection, InputConnection JSR 75No httpHTTPHttpConnectionMIDP 1.0/2.0Yes httpsSecure HTTPHttpsConnectionMIDP 2.0yes comSerial I/OCommConnectionMIDP 2.0no sms, mmsSMS, MMSMessageConnectionJSR 120, JSR 205No apduApplication Protocol Data Unit (encryption and smart cards) APDUConnectionJSR 177no socket, serversocketSocketSocketConnection, ServerSocketConnection MIDP 2.0no datagramUDPUDPDatagramConnectionMIDP 2.0no
Implemented under profiles ConnectionRequired?CLCD 1.0, 1.1MIDP 1.0MIDP 2.0 CommConnectionNYes ConnectionYYes ContentConnectionYYes DatagramConnectionNyes HttpConnectionYyes HttpsConnectionYyes InputConnectionYyes OutputConnectionYyes SecureConnectionNYes ServerSocketConnectionNYes SocketConnectionNYes StreamConnectionYyes StreamConnectionNotifierYyes UDPDatagramConnectionNYes Normally implemented, but carrier or handset may not.
Common Connections Connector class is the abstracted view – Like when we used for file access. Connection interface, generic connection – defines the close method DatagramConnection interface, defines actions for a datagram connection, like UDP – Datagram interface, abstract interface for a datagram, which are bidirectional, extending the DataInput and DataOutput interfaces StreamConnection, defines actions on a stream connection, such as TCP – bidirectional, uses the InputConnection and OutputConnection ContentConnection interface, supports passes content encoded through a well-known codec – Video, audio, images.
Permission for Network Connection Network applications require privilege, which is imposed by the MIDLet. – This ensures that unauthorized network connection do not result in data use charges. – On a phone, may require sign app – At min, requires the JAD to specify the privileges they require, set in the JAD attributes MIDlet-Permissions – See Pg 327 in the book for more info. In the emulators, you app's runs as untrusted and connections are available if the user grants permission
URL syntax Where – scheme is the protocol (ie http, file, etc) – user is the username, which is optional – password is the password for the username, also optional – host is the fully qualified domain name or address – port is the optional port number, scheme provides the default – path is the path on the remote end the format varies by scheme. – Parameters are optional parameters, depends on scheme.
typical network code. String url = "socket:// … StreamConnection c = null; InputStream s = null; try { c = (StreamConnection)Connector.open(url); s = c.openInputStream(); int ch; while ((ch = s.read()) != -1) { //read every character until end of file (-1). } } catch (ConnectionNotFoundException e) { …} catch (IllegalArgumentException e) { …} catch (IOException e) { …} finally { if (s != null) s.close(); if (c != null) c.close(); }
StreamConnection Is the general method to open a network connection. – only provides openDataInputStream, openInputStream, openDataOutputStream, openOutputStream, and close methods. The following inherit StreamConnection – CommConnection, ContentConnection, HttpConnection, HttpsConnection, SecureConnection, SocketConnection
SocketConnection SocketConnection, provides us with the TCP connection (as does StreamConnection) – Provides methods some methods like we use in java SE and general UNIX socket programming. – String getAddress() Gets the remote address to which the socket is bound. – String getLocalAddress() Gets the local address to which the socket is bound. – int getLocalPort() Returns the local port to which this socket is bound. – int getPort() Returns the remote port to which this socket is bound. – int getSocketOption(byte option) Get a socket option for the connection. – void setSocketOption(byte option, int value) Set a socket option for the connection.
SocketConnection (2) Byte Option – DELAY: Socket option for the small buffer writing delay. – KEEPALIVE: Socket option for the keep alive feature. – LINGER: Socket option for the linger time to wait in seconds before closing a connection with pending data output. – RCVBUF: Socket option for the size of the receiving buffer. – SNDBUF: Socket option for the size of the sending buffer. Example s.setSocketOption(SocketConnection.DELAY,0); – basically like flush(), send small packets. int x = s.getSocketOption(SocketConnection.RCVBUF); – how big is the receive buffer?
Socket Example import javax.microedition.io.*; import java.io.*; String url = "socket://Somewhere.com:3012"; try { //connection should be it own method too. sock = (SocketConnection)Connector.open(url); in = new InputStreamReader(sock.openInputStream()); out = new OutputStreamWriter(sock.openOutputStream()); writeLine("Hi from a phone."); System.out.println("Wrote to System"); System.out.println("Now Waiting on input"); String from = getLine(); System.out.println("Server said:"+from); } catch (ConnectionNotFoundException e) { System.out.println("Something happened, ConnectionNotFoundException"); } catch (IllegalArgumentException e) { System.out.println("Something happened, IllegalArgumentException"); } catch (IOException e) { System.out.println("Something happened, IOExecption"); } finally { try { //close should be it's own method too. if (sock != null) sock.close(); if (in != null) in.close(); if (out != null) out.close(); } catch (IOException ex) { sock=null; in =null; out=null; } } And getLine and writeLine methods: public void writeLine(String to) { to +="\n"; if (out != null) { try { out.write(to.toCharArray(), 0, to.length()); } catch (IOException e) { label.setText("Failed to write"); } public String getLine() { // read from network port and have default value String from = ""; char ch; int i; if (in != null) { try { i = in.read(); while (i != -1 && (char) i != '\n') { from += (char) i; i = in.read(); } } catch (IOException e) { from = "AWGH!!!"; } return (from); }
Datagram and DatagramConnection Datagram and DatagramConnection are for UDP networking. – These work differently then Stream and Socket connection. – A datagramConnection, sends/receives a datagram. – There is no methods for OpenInputData and openOutputData streams. A datagramConnection can be a client or server
DatagramConnection like everything else, uses connector. – DatagramConnection dgc = null; Client – url = "data://somwhere.com:3012"; – dgc = (DatagramConnection)Connector.open(url); Server – url = "data://:3012"; //note no hostname! – dgc = (DatagramConnection)Connector.open(url);
datagram UDP is a connectionless protocol A datagram is the packet, which is sent or received. Datagram has – buffer, which is the data – offset, which is the pointer to current read or write – length, which is the size of the buffer. – header information is also available – There is also a reset method, to reuse the datagram.
datagram (2) The datagram uses the DataInput/Output methods readBoolean, writeBoolean readByte, WriteByte readChar, writeChar readFloat, writeFloat readShort, writeShore readInt, writeInt readLong, writeLong readUTF, writeUTF readFully, write – read/write an array of bytes.
datagram (3) Again, we can work with Strings instead. – Using the String functions to read/write bytes. – datagram has the two following methods (and the constructor) – byte[] = getData() //return the data buffer – setData(byte[] b, int offset, int length) //set buffer
UDP example import javax.microedition.io.*; import java.io.*; String url = "datagrame://somewhere.com:3012"; DatagramConnection dgc = null; Datagram d; String s = "Hello World"; byte m[] = s.getBytes(); String t; try { dgc = (DatagramConnection) Connector.open(url); //create the datagram with the data d = dgc.newDatagram(m,m.length); //send the datagram dgc.send(d); //now receive a datagram d.reset(); //reset for reuse dgc.receive(d); t = new String(d.getData()); System.out.println("Recieved: "+t); } catch (IOException ex) { …}
Server side. Sounds like an odd idea for a phone, but there are a number of reason – Mainly, what is called, "push" data sent to the phone. and other information – UDP uses the same as before, no host name and use receive method. – TCP, uses ServerSocketConnection method.
ServerSocketConnection Similar to a UNIX methods (java's too) Like UDP, no host is url line. url = "socket://:3012"; There is an acceptAndOpen() method – returns a SocketConnection useful methods – String getLocalAddress() – int getLocalPort()
ServerSocketConnection Example import javax.microedition.io.*; import java.io.*; private InputStreamReader in = null; private OutputStreamWriter out = null; private SocketConnection sock = null; private ServerSocketConnection ssc = null; String url="socket://:3012"; try { ssc = (ServerSocketConnection) Connector.open(url); System.out.println("localaddress is "+ssc.getLocalAddress()); sock = (SocketConnection)ssc.acceptAndOpen(); in = new InputStreamReader(sock.openInputStream()); out = new OutputStreamWriter(sock.openOutputStream()); //just like SocketConnection now //and close the sock, in, and out ssc.close(); } catch (IOException ex) { }
Server connections. This work until you close the application. – To have data pushed, use the MIDlet-push field in application descriptor. MIDlet-Push-1: socket://:3012, NameMIDlet,* – Now when data is received on port 3012, the server will be started up to respond to it. – Also can use the PushRegistery, part of the Wireless message API (WMA 1.1)
Q A &