2012-02-02 3 views
0

クライアントとサーバーの通信にTCPソケットを使用しました。Android:2つのシステム間でデータを送受信する

クライアントコード:

InetAddress inet = InetAddress.getByName("localhost"); 
int TCP_SERVER_PORT = 21111; 
Socket s = new Socket(inet, TCP_SERVER_PORT); 
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); 
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); 
String outMsg = "connect" + TCP_SERVER_PORT+System.getProperty("line.separator"); 
out.write(outMsg); 
out.flush(); 
Log.i("TcpClient", "Client sent - : " + outMsg); 
String inMsg = in.readLine() + System.getProperty("line.separator"); 
textReceived.append("Client received - : " + inMsg); 
Log.i("TcpClient", "Client received - : " + inMsg); 
s.close(); 

Serverコード:

ServerSocket ss = null; 
int TCP_SERVER_PORT = 21111; 
ss = new ServerSocket(TCP_SERVER_PORT); 
Socket s = ss.accept(); 
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); 
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); 
String incomingMsg = in.readLine() + System.getProperty("line.separator"); 
Log.i("TcpServer", "received: " + incomingMsg); 
textDisplay.append("Server received - : " + incomingMsg); 
String outgoingMsg = "Port " + TCP_SERVER_PORT + System.getProperty("line.separator"); 
out.write(outgoingMsg); 
out.flush(); 
Log.i("TcpServer", "sent: " + outgoingMsg); 
textDisplay.append("Server sent - : " + outgoingMsg); 
s.close(); 

私は私のシステムでこのプログラムをテストするために、単一のエミュレータを使用。そのうまく動作します。 私は2台のコンピュータと通信する必要があります。

+0

2台のコンピュータを使用している場合は、エミュレータの2つのインスタンス間でこれをテストする必要がありますか? – mcnicholls

+0

2台の異なるエミュレータ – Kamal

答えて

0

各エミュレータインスタンスが仮想ファイアウォールの背後にあるので、ホストマシンのポートがエミュレータインスタンスにリダイレクトできるようにする必要があります。

Thisページには、telnetおよびイネーブルポートリダイレクトを使用してエミュレータインスタンスに接続する方法の詳細があります。

各マシンでエミュレータを実行し、ポートリダイレクトを設定すると、ホストマシンのIPとポートを指定して、もう一方のエミュレータインスタンスに到達できるはずです。リダイレクトがあると、ホストマシンはそれを受け取ってエミュレータインスタンスにリダイレクトします。

関連する問題