20.11.2016 Views

Android App Development ฉบับสมบูรณ์

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

} catch (UnknownHostException e) {<br />

_client.updateStatus("Unknown host " + serverName);<br />

Log.e(TAG, "Unknown host " + serverName);<br />

e.printStackTrace();<br />

} catch (SocketTimeoutException e) {<br />

_client.updateStatus("Connect timeout");<br />

Log.e(TAG, "Connect timeout");<br />

e.printStackTrace();<br />

} catch (IOException e) {<br />

e.printStackTrace();<br />

}<br />

}<br />

public void sendMessage(String text) {<br />

if (text == null)<br />

return;<br />

if (text != "") {<br />

try {<br />

if (streamOut != null) {<br />

streamOut.writeUTF(text);<br />

streamOut.flush();<br />

}<br />

}<br />

} catch (IOException ioe) {<br />

Log.e(TAG, "Sending error: " + ioe.getMessage());<br />

close();<br />

}<br />

}<br />

public void close() {<br />

try {<br />

if (streamOut != null)<br />

streamOut.close();<br />

if (socket != null)<br />

socket.close();<br />

} catch (IOException ioe) {<br />

Log.e(TAG, "Error closing ...");<br />

}<br />

}<br />

public void handle(String msg) {<br />

Log.i(TAG, "Revieve from server : " + msg);<br />

_client.updateConversation(msg);<br />

}<br />

public class ClientThread extends Thread {<br />

public static final String TAG = "Client Thread";<br />

private Socket socket = null;<br />

private ClientTCP client = null;<br />

private DataInputStream streamIn = null;<br />

208<br />

<strong>Android</strong> <strong>App</strong> <strong>Development</strong> <strong>ฉบับสมบูรณ์</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!