한 디바이스에 서버앱 / 클라이언트앱을 만들어서 통신하려고 하고 있습니다.

 

PC 서버 / 안드로이드 클라이언트 예제를 이용해서

안드로이드에서 서버앱 / 클라이언트앱 만드는 것 까지 구현을 했습니다.

 

 

하지만 예제가 서버는 데이터를 받는 기능만 되어 있는 소스여서 서버에서도 클라이언트로 전송이 가능하게끔 수정하는 중입니다.

 

 

그런데 실행해보는 도중에 클라이언트앱에서 버튼을 누르면 메시지가 전송이 되어야 하는데, 널포인트 에러가 나버리네요..

 

 

워낙 초보라 문제점을 찾고 있지 못해서 소스 올려봅니다...

 

 

서버 앱

 package choi.test.server;public class ServerApp extends Activity implements Runnable{
 public static TextView chatting;
 public static EditText inText;
 public static Button sendBtn;
 public static ServerSocket server;
 public static Socket client;
 public static String str = "";
 public static Thread ServerThread;
 
 public static PrintWriter out;
 
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  chatting = (TextView) findViewById(R.id.chatText);
  inText = (EditText) findViewById(R.id.inText);
  sendBtn = (Button) findViewById(R.id.send);
  ServerThread = new Thread(new ServerApp());
  ServerThread.start();
  
  sendBtn.setOnClickListener(new OnClickListener() { // 전송 버튼
   public void onClick(View v) {
    String it = inText.getText().toString();
    out.println(it);
   }
  });
 }
 public void run() {
  setServer();
  process();
 }
 
 public void setServer(){
  try {
   server = new ServerSocket(8088);// ServerSocket 객체 생성, 포트8088
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 public void process(){
  while (true) {
   try {
    client = server.accept();
    BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
    str = in.readLine();
    
    out = new PrintWriter(new BufferedWriter(
      new OutputStreamWriter(client.getOutputStream())), true);
//    out.println("Server Received " + str);
    
    chatting.setText("클라이언트 : " + str);
    
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 }
}

 

 

클라이언트 앱

 public class ClientApp extends Activity implements Runnable{
 
 public static Button sendBtn;
 public static Socket cSocket;
 public static String str = "";
 public static EditText inText;
 public static TextView chatting;
 public static PrintWriter out;
 public static BufferedReader in;
 public static Thread ClientThread;
 
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        chatting = (TextView) findViewById(R.id.chatText);
        inText = (EditText) findViewById(R.id.inText);
        sendBtn = (Button) findViewById(R.id.send);
        
        ClientThread = new Thread(new ClientApp());
        ClientThread.start();
        
  sendBtn.setOnClickListener(new OnClickListener() { // 전송 버튼
   public void onClick(View v) {
    
    String it = inText.getText().toString();
    out.println(it); // 널포인트 에러가 나는 부분
   }
  });
    }
 public void run() {
  try{
   String serverIP = InetAddress.getLocalHost().getHostAddress();
   InetAddress serverAddr = InetAddress.getByName(serverIP);
   
   cSocket = new Socket(serverAddr, 8088);//Socket 객체 생성 8088포트로 연결
   
   in = new BufferedReader(new InputStreamReader(cSocket.getInputStream()));//input스트림 가져오기
   out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(cSocket.getOutputStream())), true);
   
   while (true) {
    str = in.readLine();// input스트림에서 데이터 읽어오기
    chatting.setText("서버 : " + str);
   }
  }catch(IOException e){
  }
 }
}

아. 그리고... 휴대폰에서 계속 테스트를 하는데 폰이 뜨거워져서 봤더니 램을 많이 잡아먹고 있더라구요.

 

이건 쓰레드 때문인건가요? 앱을 실행했다가 백버튼으로 나가도 쓰레드가 돌고 있는건가요?