반응형
class MessageSender extends Thread { public void run() { String message = null; PrintWriter out = null; BufferedWriter bw = null; OutputStream os = null; OutputStreamWriter osw = null; while (runningMessageSender) { if (mClientSocket != null && out == null) { try { os = mClientSocket.getOutputStream(); osw = new OutputStreamWriter(os); bw = new BufferedWriter(osw); out = new PrintWriter(bw, true); } catch (IOException e) { e.printStackTrace(); } } else if (mClientSocket == null && out != null) { out.close(); out = null; } if (mClientSocket != null && out != null) { message = sendMessageQueue.poll(); if (message != null) { out.println(message); out.flush(); } } try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } if (mClientSocket != null) { try { mClientSocket.shutdownOutput(); mClientSocket.close(); mClientSocket = null; } catch (IOException e) { e.printStackTrace(); } } if (out != null) { out.close(); out = null; } if (bw != null) { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } bw = null; } if (osw != null) { try { osw.close(); } catch (IOException e) { e.printStackTrace(); } osw = null; } if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } os = null; } } }메시지 센더 부분
class MessageReceiver extends Thread { public void run() { BufferedReader br = null; InputStream is = null; InputStreamReader isr = null; while (runningMessageReceiver) { try { if (mClientSocket == null) continue; is = mClientSocket.getInputStream(); isr = new InputStreamReader(is); if (br == null) { br = new BufferedReader(isr); } String data = br.readLine(); if (data == null) { br.close(); br = null; Toast.makeText(rv_pro.this, "상대와의 접속이 끊겼습니다.", Toast.LENGTH_SHORT).show(); finish(); } else { return_msg = data; mHandler.post(showUpdate); //Toast.makeText(rv_pro.this, data, Toast.LENGTH_SHORT).show(); } } catch (IOException e) { e.printStackTrace(); } finally { } try { Thread.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } if (br != null) { try { br.close(); br = null; } catch (IOException e) { e.printStackTrace(); } } if (isr != null) { try { isr.close(); } catch (IOException e) { e.printStackTrace(); } isr = null; } if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } is = null; } } }메시지 리시버 부분
반응형
'개발자 > Android' 카테고리의 다른 글
[Android] 버튼 클릭 시 이미지 변경에 대한 방법 (0) | 2012.01.02 |
---|---|
[Android, Java] 소켓 연결시 연결시간 설정 (1) | 2011.12.01 |
안드로이드 위젯 표준 지정 크기 (0) | 2011.09.29 |
D-Day 계산 알고리즘 (날짜 오차 안나도록) (9) | 2011.09.10 |
Notification 클래스에 대한 정보 (0) | 2011.05.23 |
댓글