2011-01-13 42 views
3

我想在2個仿真器中創建一個服務器和一個客戶端來寫入和讀取數據。 我寫代碼服務器:在Android中連接2個仿真器實例

public class ServerActivity extends Activity { 
    /** Called when the activity is first created. */ 
private ServerSocket serverSocket = null; 
private TextView tv; 
public static final int SERVERPORT = 4444; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     tv= (TextView) findViewById(R.id.myTextView); 
     try { 
    Connect(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    tv.setText("Not connected"); 
    e.printStackTrace(); 
    } 
    } 

    public void Connect() throws IOException 
    { 
    serverSocket = new ServerSocket(); 
    serverSocket.bind(new InetSocketAddress("10.0.2.15", 4444)); 
    while(true) 
    { 
     Socket socket = serverSocket.accept(); 
     tv.setText("Connected..."); 
    } 


    } 

和代碼客戶端

public class ClientActivity extends Activity { 
    /** Called when the activity is first created. */ 
private Button bt; 
private TextView tv; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     bt = (Button) findViewById(R.id.myButton); 
     tv = (TextView) findViewById(R.id.myTextView); 
     bt.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 
    // TODO Auto-generated method stub 
    try { 
    Socket socket = new Socket("10.0.2.2", 4445); 
    } catch (UnknownHostException e) { 
    // TODO Auto-generated catch block 
    tv.setText("Error1"); 
    e.printStackTrace(); 
    } catch (IOException e) { 
    // TODO Auto-generated catch block 
    tv.setText("Error2"); 
    e.printStackTrace(); 
    } 

    } 
    }); 
    } 
} 

我成立了一個重定向:

telnet localhost 5554 
redir add tcp:4445:4444 

但它不連接....請幫助我。我很感激。

+1

我從來沒有找到一種方法來做到這一點。祝你好運。 – Falmarri 2011-01-13 18:48:42

+0

@Falmarri:NickT發佈的內容非常棒! – prolink007 2011-11-25 06:05:52

回答

16

過了一段時間,我成功了。我在服務器或客戶端都沒有提及10.0.2.15。我以不同的方式打開了服務器套接字,並在不同的線程中處理了通信。我跑模擬器5554服務器和5556

我的服務器代碼客戶端,監聽6000

public class SocketServer extends Activity { 
    ServerSocket ss = null; 
    String mClientMsg = ""; 
    Thread myCommsThread = null; 
    protected static final int MSG_ID = 0x1337; 
    public static final int SERVERPORT = 6000; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     TextView tv = (TextView) findViewById(R.id.TextView01); 
     tv.setText("Nothing from client yet"); 
     this.myCommsThread = new Thread(new CommsThread()); 
     this.myCommsThread.start(); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     try { 
     // make sure you close the socket upon exiting 
     ss.close(); 
     } catch (IOException e) { 
     e.printStackTrace(); 
     } 
    } 

    Handler myUpdateHandler = new Handler() { 
     public void handleMessage(Message msg) { 
     switch (msg.what) { 
     case MSG_ID: 
      TextView tv = (TextView) findViewById(R.id.TextView01); 
      tv.setText(mClientMsg); 
      break; 
     default: 
      break; 
     } 
     super.handleMessage(msg); 
     } 
    }; 
    class CommsThread implements Runnable { 
     public void run() { 
     Socket s = null; 
     try { 
      ss = new ServerSocket(SERVERPORT); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     while (!Thread.currentThread().isInterrupted()) { 
      Message m = new Message(); 
      m.what = MSG_ID; 
      try { 
       if (s == null) 
        s = ss.accept(); 
       BufferedReader input = new BufferedReader(
        new InputStreamReader(s.getInputStream())); 
       String st = null; 
       st = input.readLine(); 
       mClientMsg = st; 
       myUpdateHandler.sendMessage(m); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     } 
    } 
} 

我重定向相似端口您

telnet localhost 5554 
redir add tcp:5000:6000 

我的客戶端代碼establshing連接上端口5000:

public class SocketClient extends Activity { 
    private Button bt; 
    private TextView tv; 
    private Socket socket; 
    private String serverIpAddress = "10.0.2.2"; 
    // AND THAT'S MY DEV'T MACHINE WHERE PACKETS TO 
    // PORT 5000 GET REDIRECTED TO THE SERVER EMULATOR'S 
    // PORT 6000 
    private static final int REDIRECTED_SERVERPORT = 5000; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     bt = (Button) findViewById(R.id.myButton); 
     tv = (TextView) findViewById(R.id.myTextView); 

     try { 
     InetAddress serverAddr = InetAddress.getByName(serverIpAddress); 
     socket = new Socket(serverAddr, REDIRECTED_SERVERPORT); 
     } catch (UnknownHostException e1) { 
     e1.printStackTrace(); 
     } catch (IOException e1) { 
     e1.printStackTrace(); 
     } 

     bt.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      try { 
       EditText et = (EditText) findViewById(R.id.EditText01); 
       String str = et.getText().toString(); 
       PrintWriter out = new PrintWriter(new BufferedWriter(
        new OutputStreamWriter(socket.getOutputStream())), 
        true); 
       out.println(str); 
       Log.d("Client", "Client sent message"); 

      } catch (UnknownHostException e) { 
       tv.setText("Error1"); 
       e.printStackTrace(); 
      } catch (IOException e) { 
       tv.setText("Error2"); 
       e.printStackTrace(); 
      } catch (Exception e) { 
       tv.setText("Error3"); 
       e.printStackTrace(); 
      } 
     } 
     }); 
    } 
} 

該服務器具有一個TextView的接收消息中,客戶端公頃編輯一個EditText來編寫消息,併發送一個按鈕。

0

它的偉大工程

你只需要添加兩個步驟: 1)外部的應用程序標記添加INTERNET權限標籤在AndroidManifest.xml

2)犯規與Android> 3工作,因爲網絡UI線程限制