'TCP/IP'에 해당되는 글 1건

  1. 2011.05.26 안드로이드에서 TCP/IP 통신 (스레드 포함)
 
   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;
			}
		}
	}
메시지 리시버 부분
신고
Posted by 냥아