1 / 23

Cosc 4755

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.

mae
Download Presentation

Cosc 4755

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Cosc 4755 Mobile networking Overview and Stream, Socket, and Datagram.

  2. 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

  3. Some Connection Schemes

  4. Implemented under profiles Normally implemented, but carrier or handset may not.

  5. 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.

  6. 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

  7. URL syntax • scheme://user:password@host:port/path;parameters • 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.

  8. typical network code. String url = "socket://www.nowhere.com:3012"; … StreamConnection c = null; InputStream s = null; try { c = (StreamConnection)Connector.open(url); s = c.openInputStream(); intch; 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(); }

  9. 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

  10. 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.   • intgetLocalPort() • Returns the local port to which this socket is bound.   • intgetPort() • Returns the remote port to which this socket is bound.   • intgetSocketOption(byte option) • Get a socket option for the connection.   • void setSocketOption(byte option, int value) • Set a socket option for the connection.

  11. 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?

  12. 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; inti; 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); }

  13. 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

  14. DatagramConnection • like everything else, uses connector. • DatagramConnectiondgc = null; • Client • url = "data://somwhere.com:3012"; • dgc = (DatagramConnection)Connector.open(url); • Server • url = "data://:3012"; //note no hostname! • dgc = (DatagramConnection)Connector.open(url);

  15. 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.

  16. 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.

  17. 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

  18. UDP example import javax.microedition.io.*; import java.io.*; String url = "datagrame://somewhere.com:3012"; DatagramConnectiondgc = 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) { …}

  19. 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. email and other information • UDP uses the same as before, no host name and use receive method. • TCP, uses ServerSocketConnection method.

  20. 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() • intgetLocalPort()

  21. ServerSocketConnection Example import javax.microedition.io.*; import java.io.*; private InputStreamReader in = null; private OutputStreamWriter out = null; private SocketConnection sock = null; private ServerSocketConnectionssc = 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) { }

  22. 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)

  23. Q A &

More Related