Cosc 4755

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

description

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. - PowerPoint PPT Presentation

Transcript of Cosc 4755

Page 1: Cosc  4755

Cosc 4755

Mobile networkingOverview and Stream, Socket, and

Datagram.

Page 2: Cosc  4755

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

Page 3: Cosc  4755

Some Connection SchemesURL Scheme Protocol GCF Type Defined by Required

Btl2cap Bluetooth L2CAPConnection JSR 82 No

datagram Datagram DatagramConnection CLDC, CDC, JSR 197

No

file File access FileConnection, InputConnection

JSR 75 No

http HTTP HttpConnection MIDP 1.0/2.0 Yes

https Secure HTTP HttpsConnection MIDP 2.0 yes

com Serial I/O CommConnection MIDP 2.0 no

sms, mms SMS, MMS MessageConnection JSR 120, JSR 205 No

apdu Application Protocol Data Unit (encryption and smart cards)

APDUConnection JSR 177 no

socket, serversocket Socket SocketConnection, ServerSocketConnection

MIDP 2.0 no

datagram UDP UDPDatagramConnection MIDP 2.0 no

Page 4: Cosc  4755

Implemented under profilesConnection Required? CLCD 1.0, 1.1 MIDP 1.0 MIDP 2.0

CommConnection N Yes

Connection Y Yes Yes Yes

ContentConnection Y Yes Yes Yes

DatagramConnection N yes yes yes

HttpConnection Y yes yes yes

HttpsConnection Y yes yes yes

InputConnection Y yes yes yes

OutputConnection Y yes yes yes

SecureConnection N Yes

ServerSocketConnection N Yes

SocketConnection N Yes

StreamConnection Y yes yes yes

StreamConnectionNotifier Y yes yes yes

UDPDatagramConnection N Yes

Normally implemented, but carrier or handset may not.

Page 5: Cosc  4755

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.

Page 6: Cosc  4755

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

Page 7: Cosc  4755

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.

Page 8: Cosc  4755

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(); 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(); }

Page 9: Cosc  4755

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

Page 10: Cosc  4755

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.

Page 11: Cosc  4755

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?

Page 12: Cosc  4755

Socket Exampleimport 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); }

Page 13: Cosc  4755

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

Page 14: Cosc  4755

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);

Page 15: Cosc  4755

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.

Page 16: Cosc  4755

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.

Page 17: Cosc  4755

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

Page 18: Cosc  4755

UDP exampleimport 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) { …}

Page 19: Cosc  4755

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.

Page 20: Cosc  4755

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

Page 21: Cosc  4755

ServerSocketConnection Exampleimport 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) { }

Page 22: Cosc  4755

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)

Page 23: Cosc  4755

QA&