2013-10-24 19 views
0

我正在開發客戶端服務器應用程序。我的客戶端是一個Java應用程序,我的服務器是一個Android模擬器。我想從客戶端發送消息到服務器,但沒有消息收到android模擬器。 Java桌面客戶端代碼Java桌面客戶端不會向Android模擬器發送消息

Socket s = null; 
    BufferedReader get = null; 
    PrintWriter put = null; 
    try { 
     s = new Socket("127.0.0.1", 6000); 




     put = new PrintWriter(s.getOutputStream(), true); 



     put.println("hi"); 

    } 
    catch(Exception ex) 
    { 

    } 

Android的服務器代碼

private ServerSocket serverSocket; 

Handler updateConversationHandler; 

Thread serverThread = null; 

private TextView text; 

私人的EditText文本框;

public static final int SERVERPORT = 6000; 

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    text = (TextView) findViewById(R.id.text2); 
    updateConversationHandler = new Handler(); 
    this.serverThread = new Thread(new ServerThread()); 
    this.serverThread.start(); 
} 
@Override 
protected void onStop() { 
    super.onStop(); 
    try { 
     serverSocket.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

class ServerThread implements Runnable { 
    public void run() { 
     Socket socket = null; 
     try { 
      serverSocket = new ServerSocket(SERVERPORT); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     while (!Thread.currentThread().isInterrupted()) { 
      try { 
       socket = serverSocket.accept(); 
       CommunicationThread commThread = new CommunicationThread(socket); 
       new Thread(commThread).start(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 

class CommunicationThread implements Runnable { 

    private Socket clientSocket; 

    private BufferedReader input; 

    public CommunicationThread(Socket clientSocket) { 

     this.clientSocket = clientSocket; 

     try { 

      this.input = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream())); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public void run() { 


     while (!Thread.currentThread().isInterrupted()) { 

      try { 

       String read = input.readLine(); 

       updateConversationHandler.post(new updateUIThread(read)); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

} 

class updateUIThread implements Runnable { 
    private String msg; 

    public updateUIThread(String str) { 
     this.msg = str; 
    } 

    @Override 
    public void run() { 
     text.setText(text.getText().toString()+"Client Says: "+ msg + "\n"); 
    } 

} 

回答

1

documentation相關報價(向下滾動到節網絡地址空間

Each instance of the emulator runs behind a virtual router/firewall service 
that isolates it from your development machine's network interfaces and settings 
and from the internet. An emulated device can not see your development machine 
or other emulator instances on the network. Instead, it sees only that it is 
connected through Ethernet to a router/firewall. 

The virtual router for each instance manages the 10.0.2/24 network address 
space — all addresses managed by the router are in the form of 10.0.2.<xx>, 
where <xx> is a number. Addresses within this space are pre-allocated by the 
emulator/router as follows: 

然後他們繼續表現出預分配的地址表中的

+0

。非常感謝你 – Ahmed

相關問題