안드로이드/프로그래밍
소켓 쓰레드 적용
에어버스
2015. 1. 28. 16:15
오류없이 실행되었던 이클립스 프로젝트를 안드로이드 스튜디오에서 같은 코드로 실행하면 오류발생하여
소켓 연결과 수신은 쓰레드 처리함.
AutoStockOrder.zip(Android Studio 프로젝트)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
package com.pelkan.user.autostockorder; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.net.Socket; public class AutoStockOrder extends ActionBarActivity { EditText input; Button button; TextView output; Socket socket = null; BufferedReader socket_in; BufferedWriter socket_out; final String _SERVER_ADDR = "서버 도메인 혹은 IP"; final int _PORT_NO = 4444; final String _ANDROID_DEVICE_CODE = "1"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_auto_stock_order); input = (EditText) findViewById(R.id.input); button = (Button) findViewById(R.id.button); output = (TextView) findViewById(R.id.output); ConnectSocket conSock = new ConnectSocket(); // 쓰레드처리 안하면 오류 conSock.setDaemon(true); conSock.start(); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { String data = input.getText().toString(); if (data != null) { // SEND Log.w("NETWORK", " " + data); try { socket_out.write(_ANDROID_DEVICE_CODE + data); socket_out.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }); } class ConnectSocket extends Thread { public void run() { try { socket = new Socket(_SERVER_ADDR, _PORT_NO); // MFC 에서 한글 안깨짐 socket_in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "euc-kr")); socket_out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "euc-kr")); int b = 0; char[] a = new char[1024]; while (true) { // RECIEVE b = socket_in.read(a); // RECIEVE final String c = new String(a); output.post(new Runnable() { public void run() { output.setText(c); } }); } } catch (IOException e) { e.printStackTrace(); } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_auto_stock_order, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } |