마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

이클립스 안드로이드 클라이언트 서버 데이터 전송 질문

0 추천
app클라이언트1 -> java 서버 ->app 클라이언트2

이렇게 데이터를 전송하려고 하는데요.

클라이언트1에서 서버로는 전송이 되는데 서버에서 클라이언트2로는 전송이 안되요. 클라이언트2에는 출력이 안되요.

소스좀 봐주세요ㅠㅠ

 

------------------------------------------------------------------------------------

서버소스

 

메인

 

public class Main {

 
 public static void main(String args[]){
 
  new Manager();
 }

}

 

매니져

 

public class Manager {

 
 private HashMap<String,SocketItem> clients,drones,admins;
 public Manager() {

  runServer();
 }

 public void runServer() {
  final int port = 3331;
  try {
   ServerSocket ss = new ServerSocket(port);
   System.out.println("**서버시작**");

   while (true) {

   // System.out.println("Waiting for new client");
    
    Socket socket = ss.accept();
    
   // System.out.println("Accepted");
       System.out.println("IP : " + socket.getInetAddress()+" 접속");

    BufferedReader in = new BufferedReader(new InputStreamReader(
      socket.getInputStream()));
    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
    
    //int inputCase = Integer.parseInt(br.readLine());
    //String id = br.readLine();
    SocketItem item = new SocketItem(in, out, socket, this);
    Thread t = new Thread(item);
    t.start();
   }
   
  } catch (IOException e) {
    e.printStackTrace();
    System.out.println("io exception");
  }finally{
   System.out.println("finally");
  }
  System.out.println("Manager.runServer ends");
 }
 
 public void forwardMessage(){
  
 }
 

}

 

소켓 아이템

 

import java.io.*;
import java.net.*;

public class SocketItem implements Runnable {

 private BufferedReader in;
 private PrintWriter out;
 private Socket socket;
 private Manager manager;
 

 public SocketItem(BufferedReader in, PrintWriter out, Socket socket,
   Manager manager) {
  this.in = in;
  this.out = out;
  this.socket = socket;
  this.manager = manager;
 }

 @Override
 public void run() {
//  System.out.println("socketitem run()");
  boolean b = true;
  String input = null;
  int count =0;
  
  while (b) {
   count ++ ;

   try {
    input = in.readLine().toString();
    int inputCase = Integer.parseInt(input);

    
    switch(inputCase){
    case 1:
     System.out.println("Light On");
     break;
    case 2:
     System.out.println("Light Off");
     break;
    case 3:
     System.out.println("Window Open");
     break;
    case 4:
     System.out.println("Window Close");
     break;
    case 5:
     System.out.println("Blind Open");
     break;
    case 6:
     System.out.println("Blind Close");
     break;
    case 0:
     System.out.println("Dimming brightness 0");
     break;
    case 30:
     System.out.println("Dimming brightness 30");
     break;
    case 70:
     System.out.println("Dimming brightness 70");
     break;
    case 100:
     System.out.println("Dimming brightness 100");
     break;
    }
    
        
    out.println(input +" from server");
  
    
   }catch (IOException e) {
    e.printStackTrace();
    b = false;
    System.out.println("read thread exception");
   }

   
  }
  System.out.println("thread terminated");
 }

 public void disconnect() {
  try {
   out.close();
   in.close();
   socket.close();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 private void write(String msg) throws IOException {
  out.println(msg);
  out.flush();
 }
 
}
 

 

-----------------------------------------------------------

클라이언트 2 소스

 

package com.example.andsocktestclient;

 

public class MainActivity extends Activity {
 
 public static final String TAG = "MainActivity";
 
 TextView text01;
 Handler handler;
 Socket socket;
 BufferedReader in;
 PrintWriter out;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  

  Button b1 = (Button) findViewById(R.id.button1);
  text01 = (TextView) findViewById(R.id.text01);
  

  b1.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
    RequestThread thread = new RequestThread(9);
    thread.start();

   }
  });

  
  handler = new Handler();
  
 }

 class RequestThread extends Thread{
  int sel;
  String inStr;
  RequestThread(){}
  RequestThread(int a){sel = a;}
  Boolean loop;
  private final int connection_timeout = 3000;
  public void run(){
   con();
  
  }
  private void con() {
   try {
    socket = new Socket("168.131.174.35",3331);
    appendText("Client started : 168.131.174.35, 3331");
   
    while(true){
     in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
     inStr = in.readLine().toString();
     text01 = (TextView) findViewById(R.id.text01);

     text01.post(new Runnable(){
      public void run(){
       text01.setText(inStr);
      }
     });
     }
   
  // while(true){
    
   
   
  // }
   } catch (UnknownHostException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  
  private void appendText(String msg){
   final String inMsg = msg;
   handler.post(new Runnable() {
    public void run() {
     text01.append(inMsg + "\n");
    }
   });
   
  }
  public void quit() {
   loop = false;
   try {
    if(socket != null) {
     socket.close();
     socket = null;
    }
    Thread.sleep(connection_timeout);
     
    }catch(InterruptedException e) {
    }catch (IOException e) {
      // e.printStackTrace();
     
    }
  }
 }
}

 

 

 

왜 출력이 안되는지.... 부탁드려요ㅠㅠㅠ
leejw2112 (160 포인트) 님이 2015년 11월 20일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...