31
Department of Computer Science VŠB-Technical University of Ostrava 1 JavaME TAMZ by Roman Szturc 2006 & Pavel Moravec 2008 Basic JavaME Classes in CLDC

Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

1

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

Basic JavaME Classes in CLDC

Page 2: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

2

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

JavaME CLDC classes

CLDC libraries presented in CLDC specification can be divided into two categories:

classes that are a subset of standard JavaSE libraries (java.lang, java.io, java.util namespaces). The class must be either the complete implementation of JavaSE class, or its subset.

classes that are specific to CLDC (from javax.microedition namespace, which can be mapped onto equivalent JavaSE classes).

Page 3: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

3

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

System classes (1)

These classes are intimately coupled with the Java virtual machine and several standard Java tools assume their presence system:

java.lang.Object – the root of the class hierarchy, superclass of every class including arrays.java.lang.Class – classes and interfaces in a running Java application, no constructor, obtained through object.getClass()java.lang.Runtime – allows to retrieve total/available memory for the application from environment Obtained by Runtime.getRuntime()java.lang.System – allows to run GC, get current time (long), retrieve properties from system, copy arrays and provide hash codes. Contains static fields out and err representing standard and error output console (for debugging purposes).

Page 4: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

4

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

System classes (2)

java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority. The thread is started with method start() which executes the implementation of the run() method in subclass or target Runnable, specified in constructorjava.lang.Runnable (interface) – every class which needs to be run in a separate thread can implement this interface's method run()java.lang.Throwable – base class for errors and exceptions, which can be used in throw and catch statements.java.lang.StringBuffer – class for String manipulation (appending, inserting, setting and deleting parts of strings), adjusting their lengths and also reversing the String.java.lang.String – represents character strings, allows to compare and search for them, replace characters use case conversions, can construct a String from basic data types.java.lang.Math – basic math operations (only min, max are supported in CLDC 1.0)

Page 5: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

5

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

Data type classes

Provide minimal and maximal values of basic data types, type conversions including conversion to Strings and parsers to obtain values from Strings:

java.lang.Booleanjava.lang.Bytejava.lang.Shortjava.lang.Integerjava.lang.Longjava.lang.Characterjava.lang.Float – CLDC 1.1 onlyjava.lang.Double – CLDC 1.1 only

Page 6: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

6

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

Collection classes

CLDC can represent several basic types of collections:

java.util.Enumeration (interface) – interface implemented by classes which retrieve series of elements, one at a time from a collection:

for (Enumeration e = coll.elements() ; e.hasMoreElements() ; ) { doSomething(e.nextElement()); }

java.util.Vector – class representing growable array of objects that can be accessed using an integer index, capacity grows or shrinks as required.java.util.Hashtable – class mapping keys to values. Any non-null object can be used as a key or as a value.java.util.Stack – class providing a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack (empty, peek, push, pop, search).

Page 7: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Input/Output classes (1)CLDC has basic support for Input/Output operations:

java.io.DataInput (interface) – for reading bytes from a binary stream and reconstructing from them primitive types and Strings.java.io.DataOutput (interface) – ditto for writing (write*, writeUTF())java.io.InputStream – abstract superclass of all input streamsjava.io.OutputStream – abstract superclass of all output streamsjava.io.ByteArrayInputStream – reading from internal buffer (byte[]), the position of next byte is recorded, skipping is possible. May support marking and resetting the position. java.io.ByteArrayOutputStream – writing to an internal buffer, the position of next byte is recorded, buffer obtained by toByteArray() java.io.DataInputStream – DataInput implementation to load data in machine-independent/portable way, using read*, readUTF methods java.io.DataOutputStream – DataOutput implementation to store data in machine-independent/portable way using write*, writeUTF()java.io.PrintStream – adds ability to print representations of various data values conveniently.

Page 8: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

8

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

Input/Output classes (2)

We can also read localized strings of characters instead of binary data:

java.io.Reader – abstract class for reading character streamsjava.io.Writer – abstract class for writing character streamsjava.io.InputStreamReader – attaches to InputStream specified in constructor (with voluntary character set as the second parameter) and reads separate characters or arrays of themjava.io.OutputStreamWriter – attaches to OutputStream specified in constructor (with voluntary character set as the second parameter) and writes separate characters or arrays of them

Page 9: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

9

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

Date/Time classes

We have several classes, which are used to operate with date and time:

java.util.Date – class representing a specific instant in time, with millisecond precision. Can convert the time to/from the millisecond representation (since 1970/01/01 00:00) and convert it to Stringjava.util.Calendar – class to manipulate parts of a Date and compare two dates. Static members are constants such as YEAR, which are used in get/set methods to change the stored Date, which can be retrieved/set by getTime()/setTime() methods.

Calendar c = Calendar.getInstance(/*timeZone*/);

java.util.TimeZone – class to represent a time zone offset, and also incorporate daylight savings time changes.

Page 10: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

10

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

Other classes

Some of the remaining classes in java.* namespace:

java.util.Random – uniform pseudorandom numbers generator based on a (48-bit) long seed (we can use adjusted current time).

java.util.Timer – CLDC 1.1 – facility to schedule tasks for future execution in a background thread – one-time execution or repeated execution at regular periodic intervals.java.util.TimerTask – CLDC 1.1 – a class representing a task that can be scheduled for one-time or repeated execution by a Timer.

Page 11: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

11

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

Exceptions (1)Exceptions indicate non-lethal problems the application may catch and engage further activity to solve them:

java.lang.Exception – base class of all exceptionsjava.lang.ClassNotFoundException – the requested class in Class.forName() call was not found. Useful in detecting features (typically JSRs) supported by specific device.java.lang.IllegalAccessException – accessibility problems when instantiating a class with Class.newInstance()java.lang.InstantiationException – trying to instantiate interface or abstract class with Class.newInstance() java.lang.InterruptedException – event on a paused Thread when another Thread calls interrupt() methodjava.lang.RuntimeException – thrown during the normal operation, a method does not have to declare throws for it or its subclassesjava.lang.ArithmeticException – problem with calculation, typically division by zero

Page 12: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

12

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

Exceptions (2)

java.lang.NullPointerException – using null where an instance of an object is required.java.lang.ArrayStoreException – trying to store wrong object type in an array (e.g. a Long in an array of Strings)java.lang.ClassCastException – trying to cast an object to a subclass of which it is not an instance (e.g. a Long to a String)java.lang.IllegalArgumentException – trying to pass inappropriate or illegal argument to a methodjava.lang.NumberFormatException – conversion of a String to some numeric format has failed (e.g. converting “ABC” to int )java.lang.IndexOutOfBoundsException – superclass to some concrete exception, meaning the index is negative, or greater than the maximal allowable index. Subclasses are:

java.lang.ArrayIndexOutOfBoundsException java.lang.StringIndexOutOfBoundsException

java.lang.NegativeArraySizeException – self explanatory

Page 13: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

13

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

Exceptions (3)

java.lang.SecurityException – security violation, in most cases the setup of the platform or the user action prevented execution of some code (e.g. opening a connection, accessing the files,...)java.lang.IllegalThreadStateException – trying to change state of a thread in wrong way (e.g. suspend an already suspended Thread)java.lang.IllegalMonitorStateException – working with an object monitor without owning it in multi-threaded applications

java.util.EmptyStackException – calling methods which retrieve elements from Stack when there is no adequate elementjava.util.NoSuchElementException – thrown when calling nextElement when hasMoreElements would return false

Page 14: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

14

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

I/O Exceptions

java.io.IOException – base class, I/O operation failed for some reason:

java.io.EOFException – end of file (EOF) or end of stream has been reached unexpectedly during input. Some input operations return on EOF a distinguished value (typically -1) instead of throwing this exception.java.io.InterruptedIOException – I/O operation has been interrupted (the thread performing it was terminated)java.io.UnsupportedEncodingException – invalid or unsupported character setjava.io.UTFDataFormatException – signals a malformed UTF-8 string when calling the readUTF() method on an implementation of DataInput interface.

Page 15: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

15

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

Errors

Errors indicate lethal problems the application should not catch, it is supposed to terminate instead, due to of abnormal conditions. A method does not declare errors in its throws clause:

java.lang.Error – base class of all errorsjava.lang.VirtualMachineError – the JVM is broken or has run out of resources necessary for it to continue working.java.lang.OutOfMemoryError – the JVM cannot allocate an object, because it has run out of memory and no additional memory can be collected by garbage collector.java.lang.NoClassDefFoundError – CLDC 1.1 – if JVM tries to load the definition of a class which existed in time of compilation but cannot be found right now.

Page 16: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

16

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

Properties

Properties allow the application to retrieve some info about the virtual machine and running environment. Unlike JavaSE there is not a Dictionary used, but the method System.getProperty(String key) retrieves the property string. The basic properties for CLDC are:

microedition.platform – Name of the host platform or device microedition.encoding – Default character set (e.g. ISO8859_1)microedition.configuration – Name and version of supported configuration (CLDC 1.0, CLDC 1.1)microedition.profiles– Name and version of supported profiles (e.g. MIDP-1.0, MIDP-2.0, default is null)

Profiles define additional properties. Manufacturer-specific APIs add more (non-portable) properties, e.g. com.sonyericsson.imei

Page 17: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

17

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

Generic Connection Framework

Page 18: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

18

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

Generic connection framework motivation

Small footprint of JavaME applications and libraries led to some problems with the JavaME collection of I/O and networking classes. Also the mobile devices may require other ways of communication which are not covered with standard JavaME classes.

⇒ We want to use something simple, which would allow to add new types of connections, while using a standard addressing scheme.

In fact, we return to the classic „everything is a file“ UNIX philosophy, which means that in CLDC, „everything is a connector“. CLDC defines the framework, but not the implementation and supported protocols!

Page 19: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

19

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

URIConnector's methods accept a URI. The general form of the name parameter is as follows:

<scheme>:<address>;<parameters>

scheme – identifies how the connection is made: socket, http, file, datagram, ...,

address – identifies what to connect to,parameters – identify other information required by the scheme to establish a connection, such as a connection speed.

Templates of some URIs are shown in the following list:

http://server/path/file?opt1=val1&opt2=val2socket://<host>:<port>datagram://<host>:<port>comm:<port>;<options>

Page 20: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

20

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

Connections

HTTP: Connector.open("http://www.foo.com");Sockets: Connector.open("socket://129.144.111.222:9000");COM ports: Connector.open("comm:0;baudrate=9600");Datagrams: Connector.open("datagram://129.144.111.33");Files: Connector.open("file:/foo.dat");

Remember to close the input and output streams after the connection is closed (it is not done automatically!)

Page 21: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

21

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

Generic Connection Framework

Connection InputConnection

OutputConnection

DatagramConnection

ContentConnection

StreamConnection

The Generic Connection Framework (GCF) provides the foundation for all communications within the JavaME architecture. Within the configuration layer the GCF interface is defined along with a number of basic interfaces. The GCF provides no protocol implementations.

HTTPConnection

Datagram

Connector

ConnectionNotFoundException

Page 22: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

22

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

Basics of Socket-based Network Communication

Page 23: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Identifying a Machine

Machines are uniquely identified within the Internet by:

Domain Name which represents machine address by a human-readable form, for instance www.cs.vsb.cz

The dotted quad form of IP address which is four numbers separated by dots, such as 158.196.157.94.

Transport layer protocols used in IP networks:

TCP (Transmission Control Protocol) – secure logical channel, we establish connection before the communication and acknowledge all received data sent by the peer. In the end, the connection must be closed.

UDP (User Datagram Protocol) – we don't use a connection but send data in standalone packets. A packet may get duplicated, lost, come in later than the next packet and in some cases may be even damaged.

Page 24: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

24

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

Identifying an ApplicationAn IP address identifies a unique endpoint, but several domain names may resolve to the same machine.

Each machine also contains ports, and when a client or a server is set up, a port for the remote application must be chosen.

The port is not a physical location in a machine, but a software abstraction. Typically, each service is associated with a unique port number on a given server machine. We need to use two ports – one on source (usually a random one) and one on destination machine.

25 80 443

eximapache

Net

Page 25: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

25

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

HTTP Protocol

HttpConnection interface defines necessary methods and constants for an HTTP connection.

The Connector.open() method opens a URL, reads and processes headers, and returns an HttpConnection object. When the input stream is opened, data sent by HTTP server can be read.

Page 26: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

26

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

Open HTTP Connection – GET

String url = "http://...";HttpConnection connection = (HttpConnection)Connector.open(url);

int response = connection.getResponseCode();if (response != HttpConnection.HTTP_OK) throw new IOException("HTTP response code: " + response);

long length = connection.getLength();InputStream input = connection.openInputStream();...

HTTPConnection processes headers automaticaly. Response code should be tested before an input stream is opened. Amount of available data can be obtained by the getLength() method.

Page 27: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Open HTTP Connection - POST

String url = "http://...";HttpConnection connection = (HttpConnection)Connector.open(url);connection.setRequestMethod(HttpConnection.POST);connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");OutputStream os = connection.openOutputStream();os.write(("?opt1=value1&...").getBytes()); os.close();

int response = connection.getResponseCode();if (response != HttpConnection.HTTP_OK) throw new IOException("HTTP response code: " + response);

long length = connection.getLength();InputStream input = connection.openInputStream();...

HTTPConnection method and headers can be set using calls to setRequestMethod() and setRequestProperty()

Page 28: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

28

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

Open HTTPS Connection

HTTPS is the secure version of HTTP. It is usually implemented using TLS v1.0 or SSL v3.0. They offer encryption, source authentication, and data integrity as means to protect information exchanged over insecure, public networks.

String url = "https://...";HttpsConnection connection = (HttpsConnection)Connector.open(url);

SecurityInfo info = connection.getSecurityInfo();Certificate certificate = info.getServerCertificate();

InputStream input = connection.openInputStream();...

Page 29: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

29

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

Socket Connection

String uri = "socket://" + name + ":" + port;StreamConnection connection = (StreamConnection) Connector.open(uri, Connector.READ_WRITE);

InputStream input = connection.openInputStream();OutputStream output = connection.openOutputStream();

// Send a message.String outgoing = ...output.write(outgoing.getBytes());// Receive a response.byte buffer[] = new byte[SIZE];int read = input.read(buffer);

Establish a socket connection using Connector and read/write data using associated input/output streams.

Page 30: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

30

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

Socket Connection Server̶

MIDP 1.0 – the socket is reused for incomming connection

StreamConnectionNotifier serverSocket = (StreamConnectionNotifier) Connector.open("serversocket://:12345");

StreamConnection conn = serverSocket.acceptAndOpen();

MIDP 2.0 – a new socket is created. ServerSocketConnection scn = (ServerSocketConnection) Connector.open("socket://:12345");SocketConnection sc = (SocketConnection) scn.acceptAndOpen();

Both the device and the service operator must support servers on mobile devices to make it work (e.g. no private IP addresses!)

The Server Socket listens to traffic on given port on localhost. The MIDP implementations differ in handling of the server sockets.

Page 31: Basic JavaME Classes in CLDCwiki.cs.vsb.cz/images/c/c5/Tamz-L3.pdf · java.lang.Thread – class which represents a parallel branch of the program, (running) with a given priority

Department of Computer ScienceVŠB-Technical University of Ostrava

31

JavaMETAMZ

by Roman Szturc 2006 & Pavel Moravec 2008

Server's Request Handler

public class EchoRequestHandler extends Thread { public void run() { try { InputStream input = serverSocket.getInputStream(); OutputStream output = serverSocket.getOutputStream(); int read; byte data[] = new byte[BUFFER_SIZE]; while ((read = input.read(data)) != -1) { output.write(data, 0, read); output.flush(); } } ...

In order to ensure handling of multiple clients, the request handler is executed by an independent thread of execution.