Androidでソケット通信を学んでいます。私が考えた最も単純なデモとして、すぐにリッスンを開始し、ユーザーがボタンを押すと、EditText 内のテキストを自分自身に送信し、受信したメッセージを LogCat に表示するアプリを作成しました。私の理解ServerSocket serverSocket = new ServerSocket(port);
では、接続を受信するまでブロックします。以下のコードはヒットLog.i("Listen()", "accepted.");
しないので、接続が確立されていないと思います。btnSendMessage をクリックするLog.i("Send()", "Message sent.");
と実行されますが、他に何も起こらないようです。Socket socket = new Socket(address, port);
それがServerSocketを継続させるものだと想像していました。
誰かが次のことに何か問題があると思いますか?
package com.example.socketdemo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity
{
// UI elements
Button btnSendMessage = null;
TextView tvReceivedMessage = null;
EditText textMessage = null;
int port = 9000;
@Override
protected void onCreate(Bundle savedInstanceState)
{
Log.i("onCreate()", "onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSendMessage = (Button) findViewById(R.id.btnSendMessage);
btnSendMessage.setOnClickListener(btnSendMessage_OnClickListener);
textMessage = (EditText) findViewById(R.id.textMessage);
tvReceivedMessage = (TextView) findViewById(R.id.tvReceivedMessage);
Listen();
}
Thread thread = null;
private void Listen()
{
Log.i("Listen()", "Listen()");
thread = new Thread(new Runnable()
{
public void run()
{
try
{
ServerSocket serverSocket = new ServerSocket(port);
Socket clientSocket = serverSocket.accept();
Log.i("Listen()", "accepted.");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
while (true)
{
Thread.sleep(500);
String receivedLine = bufferedReader.readLine();
Log.i("Listen()", receivedLine);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
thread.start();
}
private void Send(final String address, final String message)
{
Thread thread = new Thread(new Runnable()
{
public void run()
{
try
{
Socket socket = new Socket(address, port);
// Attempt #1
// DataOutputStream dataOutputStream = new
// DataOutputStream(socket.getOutputStream());
// dataOutputStream.writeUTF(message);
// Attempt #2 (what is different about this than attempt
// #1?)
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),
true);
out.println(message);
socket.close();
Log.i("Send()", "Message sent.");
}
catch (IOException e)
{
e.printStackTrace();
}
}
});
thread.start();
}
private OnClickListener btnSendMessage_OnClickListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
// Get the address to send to from the UI
EditText textAddress = (EditText) findViewById(R.id.textAddress);
String destinationAddress = textAddress.getText().toString();
textMessage = (EditText) findViewById(R.id.textMessage);
String message = textMessage.getText().toString();
Send(destinationAddress, message);
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
- - - - - - - - 編集 - - - - - - - -
これは実際に可能であることがわかります。何が間違っていたのか正確にはわかりませんが、このコードは期待どおりに機能します。
package com.example.socketdemo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity
{
// UI elements
Button btnSendMessage = null;
Button btnListen = null;
TextView tvReceivedMessage = null;
EditText textMessage = null;
int port = 9000;
@Override
protected void onCreate(Bundle savedInstanceState)
{
Log.i("onCreate()", "onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSendMessage = (Button) findViewById(R.id.btnSendMessage);
btnSendMessage.setOnClickListener(btnSendMessage_OnClickListener);
btnListen = (Button) findViewById(R.id.btnListen);
btnListen.setOnClickListener(btnListen_OnClickListener);
textMessage = (EditText) findViewById(R.id.textMessage);
tvReceivedMessage = (TextView) findViewById(R.id.tvReceivedMessage);
}
Thread thread = null;
private void Listen()
{
Log.i("Listen()", "Listen()");
thread = new Thread(new Runnable()
{
public void run()
{
try
{
ServerSocket serverSocket = new ServerSocket(port);
Socket clientSocket = serverSocket.accept();
Log.i("Listen()", "accepted.");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
// Listen until we receive a message or the client disconnects
boolean stopListening = false;
while (!stopListening)
{
Thread.sleep(500);
String receivedLine = bufferedReader.readLine();
if(receivedLine != null)
{
Log.i("Listen()", "Received message: " + receivedLine);
stopListening = true;
}
// Check if the client has disconnected by attempting to write to him (this is the recommended technique)
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
if (out.checkError())
{
stopListening = true;
Log.i("Listen()", "Client disconnected.");
}
}
clientSocket.close();
}
catch (Exception e)
{
Log.i("Listen()", "Exception caught!");
e.printStackTrace();
}
}
});
thread.start();
}
private void Send(final String address, final String message)
{
Thread thread = new Thread(new Runnable()
{
public void run()
{
Socket mSendSocket = null;
try
{
if(mSendSocket == null)
{
Log.i("Send::run()", "Creating socket to " + address);
mSendSocket = new Socket(address, port);
}
// Send ascii data
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(mSendSocket.getOutputStream())),
true);
out.println(message);
Log.i("Send()", "Message sent.");
}
catch (IOException e)
{
if(e instanceof UnknownHostException)
{
Log.i("Send()", "Unknown host.");
}
else
{
Log.i("Send()", "Connection failed.");
}
}
}
});
thread.start();
}
private OnClickListener btnSendMessage_OnClickListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
// Get the address to send to from the UI
EditText textAddress = (EditText) findViewById(R.id.textAddress);
String destinationAddress = textAddress.getText().toString();
textMessage = (EditText) findViewById(R.id.textMessage);
String message = textMessage.getText().toString();
Send(destinationAddress, message);
}
};
private OnClickListener btnListen_OnClickListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
Listen();
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}