クライアント(クライアントとサーバーの両方)コードをニートビーンズで試してみましたが、すべてうまくいきました。それから私はそれをEclipse SDKで書き、エミュレーターで実行しようとしましたが、サーバーはもうメッセージを受信しませんでした...
クライアント:
package com.aaa.udep;
import com.aaa.udep.Client;
import com.aaa.udep.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Thread thread=new Thread (new Client());
thread.start();
}
});
}
@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.aaa.udep;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import android.util.Log;
public class Client implements Runnable {
Client()
{
run();
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
Log.i("MyApp", "starting");
String messageStr = "Hello Android dolwno rabotatj, nado testirovatj na teelefone!";
String serverHostname = new String ("127.0.0.1");
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName(serverHostname);
Log.i("MyApp", "Atempting to connect");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
sendData = messageStr.getBytes();
Log.d ("MyAPp","Sending data to server");
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress, 9879);
clientSocket.send(sendPacket);
Log.d ("MyAPp","Sent");
DatagramPacket receivePacket =
new DatagramPacket(receiveData, receiveData.length);
Log.d ("MyApp","Waiting for return packet");
clientSocket.setSoTimeout(10000);
try {
clientSocket.receive(receivePacket);
String modifiedSentence =
new String(receivePacket.getData());
InetAddress returnIPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
Log.d ("From server at get reply: ", serverHostname);
Log.d("Message: ", modifiedSentence );
}
catch (SocketTimeoutException ste)
{
ste.printStackTrace();
Log.e("Timeout Occurred: Packet assumed lost", ste+"");
}
clientSocket.close();
}
catch (UnknownHostException ex) {
ex.printStackTrace();
Log.e("UnknownHostException", ex+"");
}
catch (IOException ex) {
ex.printStackTrace();
Log.e("IO Expeption", ex+"");
}
}
}
manifest:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
ログ:
03-28 00:53:37.159: I/MyApp(337): starting
03-28 00:53:37.159: I/MyApp(337): Atempting to connect
03-28 00:53:37.220: D/MyAPp(337): Sending data to server
03-28 00:53:37.220: D/MyAPp(337): Sent
03-28 00:53:37.220: D/MyApp(337): Waiting for return packet
03-28 00:53:47.254: W/System.err(337): java.net.SocketTimeoutException: Connection timed out
03-28 00:53:47.860: W/System.err(337): at org.apache.harmony.luni.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:263)
03-28 00:53:47.860: W/System.err(337): at java.net.DatagramSocket.receive(DatagramSocket.java:414)
03-28 00:53:47.870: W/System.err(337): at com.aaa.udep.Client.run(Client.java:51)
03-28 00:53:47.870: W/System.err(337): at java.lang.Thread.run(Thread.java:1096)
03-28 00:53:47.870: E/Timeout Occurred: Packet assumed lost(337): java.net.SocketTimeoutException: Connection timed out