Listing 1312 Requester class implementation

public class Requester extends Thread { <-1 Requester class extends

Socket requestSocket; O the Thread class

String message;

StringBuilder returnStringBuffer = new StringBuilder(); Message lmsg; int ch;

communications public void run() {

try { C Socket requestSocket = new Socket ("localhost", 1024) ; <1— InputStreamReader isr = new InputStreamReader(requestSocket.getInputStream(),"ISO-8859-1"); while ( (ch = isr.read()) != -1) {

returnStringBuffer.append((char) ch) ;

message = returnStringBuffer.toString(); Create a lmsg = new Message () ; <-1 Message object lmsg.obj = (Object) message;

lmsg.what = 0; O Send the Message h. sendMessage (lmsg) ; <-1 to main thread requestSocket.close(); } catch (Exception ee) {

Log.d("CH13","failed to read data" + ee.getMessage());

The Requestor O class extends the Thread class by implementing the run method. Communications take place via an instance of the Socket class C, which is found in the java.net package. Note the port number being used—1024, just like our socket server! A Message © is used to communicate back to the UI thread. Once the Message object is initialized, it is sent back to the calling thread O.

With the Daytime Client now coded, it's time to test the application. In order for the Daytime Client to access a TCP socket, a special permission entry is required in the AndroidManifest.xml file: <uses-permission android:name="android.permission. INTERNET"></uses-permission>.

0 0

Post a comment