Download presentation
Presentation is loading. Please wait.
1
Cosc 5/4730 Blackberry: More of the GCF
2
Common Connections URL SchemeProtocolGCF TypeDefined by Btl2capBluetoothL2CAPConnectionJSR 82 datagramDatagramDatagramConnectionCLDC, CDC, JSR 197 fileFile accessFileConnection, InputConnection JSR 75 httpHTTPHttpConnectionMIDP 1.0/2.0 httpsSecure HTTPHttpsConnectionMIDP 2.0 comSerial I/OCommConnectionMIDP 2.0 sms, mmsSMS, MMSMessageConnectionJSR 120, JSR 205 apduApplication Protocol Data Unit (encryption and smart cards) APDUConnectionJSR 177 socket, serversocketSocketSocketConnection, ServerSocketConnection MIDP 2.0 datagramUDPUDPDatagramConnectionMIDP 2.0
3
This lecture We cover briefly – StreamConnection – SocketConnection The methods I skipped the last time. – ServerSocketConnection – UDP, client and server UDPDatagramConnection – ContentConnection – HttpConnection
4
As a note. While it is not listed the following slides Over wifi you’ll need to add ;deviceside=true;interface=wifi Or just direct networking. ;deviceside=true
5
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
6
SocketConnection SocketConnection, provides us with the TCP connection (as does StreamConnection) – Provides methods like we can 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.
7
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?
8
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
9
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);
10
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.
11
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.
12
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
13
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) { …}
14
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.
15
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()
16
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) { }
17
Server connections. This work until you close the application. – There are ways to use the push registry and others methods in the book to have your app launch when data comes on a port you have registered with the phone.
18
ContentConnection Normally for pictures, audio, video, something the device has a codec for. – But doesn't have to. likely getting content from a web server, such as images – We'll come back to more of this in Audio/Video lecture as well. Methods – String getEncoding() Returns a string describing the encoding of the content which the resource connected to is providing. – long getLength() Returns the length of the content which is being provided. – String getType() Returns the type of content that the resource connected to is providing. – openDataInputStream() and openDataOutputStream()
19
ContentConnection example url = "http://www.cs.uwyo.edu/~seker/courses/4730/example/pic.jpg"; try { cc = (ContentConnection) Connector.open(url); in = cc.openDataInputStream(); int len = (int)cc.getLength(); String type = cc.getType(); if (len >0 && type.compareTo("image/jpeg")==0) { byte[] data = new byte[len]; in.readFully(data); img= EncodedImage.createEncodedImage(data, 0, len, type); updatelabel("Image should be displayed\n"); } else { img = null; updatelabel("Bad... no image, type is "+type+"\n"); } } catch (IOException ex) { cc = null; in = null; img = null; updatelabel("Didn't work\n"); } See the handout for the rest of code.
20
http Quick Review A client makes a request to a web/httpd server for a URL – The request contains header information and an empty body object. The server parses the request and returns – header information and a body object, containing the data. Example client request – connect to the server GET index.html HTTP/1.1 Host: www.somewhere.com – Host line is some header information.
21
http Quick Review (2) Valid "requests" – HEAD get only the header information, no body – GET get the header and body May also contain data to be processed at part of the request line – POST submits data to be processed. data is in the header information – PUT Uploads a specified resource – DELETE deletes a specified resource – TRACE echoes back the received request
22
http Quick Review (3) Post example: POST /do.php HTTP/1.1 Host: www.somewhere.com name=value A set of named values to process. server returns HTTP/1.1 200 OK Date: Mon, 14 Dec 2009 21:26:08 GMT Server: Apache/2.2.3 (CentOS) Accept-Ranges: bytes Content-Length: 205 Connection: close Content-Type: text/html; charset=UTF-8 <meta http-equiv="Refresh" content="0; URL=http://www.cs.uwyo.edu/"> If you can still see this, there was a problem. Please click here
23
HttpConnection makes a request to a web server On return gives you access to the header information – getHeadField(string name) – getResponseCode() status code status codes – 200 success HTTP_OK – 404 failure HTTP_NOT_FOUND – There are lots of response codes, see the java doc's for HttpConnection for the full list.
24
HttpConnection example Same as content example, get an image url = "http://www.cs.uwyo.edu/~seker/courses/4730/example/pic.jpg"; try { cc = (HttpConnection) Connector.open(url); int rc = cc.getResponseCode(); //connection is open and data is read at this point if (rc == HttpConnection.HTTP_OK) { //so data has been returned. int len = (int)cc.getLength(); String type = cc.getType(); if (len >0 && type.compareTo("image/jpeg")==0) { in = cc.openDataInputStream(); byte[] data = new byte[len]; in.readFully(data); img= EncodedImage.createEncodedImage(data, 0, len, type); } else { img = null; System.out.println("Bad... no image, type is "+type); } …
25
HttpConnection example (2) get some html text. url = "http://www.cs.uwyo.edu/~seker/courses/4730/"; try { cc = (HttpConnection) Connector.open(url); int rc = cc.getResponseCode(); if (rc == HttpConnection.HTTP_OK) { //so data has been returned. int len = (int)cc.getLength(); String type = cc.getType(); in = cc.openDataInputStream(); if (len >0 ) { // Note, no header info included, on body object int ch; String data=""; while ((ch = in.read()) != -1) { data+= (char)ch; } //now ready to process the text. System.out.println(data); }
26
POST method Get is the default method for HttpConnection – Open the connector doesn't make the data request. Until the data is read or headers are read, the request has not be made. – Use the setRequestMethod to change to POST method, setRequestProperty to add the header info, openOuputStream to add the data. – Same is true for the HEAD method, if we only want the header info. The other methods are not implemented, you'll need to use the StreamConnection for the rest.
27
POST method Example url = "http://www.somewhere.edu/do.php"; try { cc = (HttpConnection) Connector.open(url); // Set the request method and headers cc.setRequestMethod(HttpConnection.POST); cc.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0"); cc.setRequestProperty("Content-Language", "en-US"); // Getting the output stream may flush the headers out = cc.openDataOutputStream(); out.write("name=value".getBytes()); out.flush(); // Optional, getResponseCode will flush // Getting the response code will open the connection, // send the request, and read the HTTP response headers. int rc = cc.getResponseCode(); if (rc == HttpConnection.HTTP_OK) { //data has been processed, now get response (if any) int len = (int)cc.getLength(); String type = cc.getType(); in = cc.openDataInputStream(); if (len >0 ) { int ch; String data=""; while ((ch = in.read()) != -1) { data+= (char)ch; } System.out.println(data); } } catch …
28
User agent header line. The User-Agent line allows the web server to response correctly to different types of browsers. – Firefox uses (or variant) Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 – A blackberry uses (or variant) "BlackBerry9530/5.0.0.328 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/105 – generic mobile phone (should work for most phones) Profile/MIDP-2.0 Configuration/CLDC-1.0 – If you want to a web server to respond to a type of browser, change the User-Agent header. Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8 – This is what the blackberry browser used, when I told it to say it was firefox.
29
HTTPS HTTPS provides a layer of security – using certificates and encryption. – Help to prevent man-in-the-middle and eavesdropping types of attacks. Works the same as http, but change to https HttpsConnection hc = (HttpsConnection) Connector.open("https://www.somewhere.com"); – use it in the same way as http connection at this point.
30
HTTPS (2) additionally – getSecurityInfo() method returns an instance of SecurityInfo – From there get the following: getCipherSuite getProtocolName getProtocolVersion getServerCertificate
31
HTTPS (3) While the communication is now secure – Doesn't imply a secure application You still have to do the work to make a secure app. – Data that is sensitive enough to encrypt accross a network, should be secured on the phone or not stored at all. What happens if the device/phone is lost or stolen? – Is the E-commerce/website application secure?
32
Q A &
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.