0
我試圖我的客戶端(兩個客戶端&服務器)在整潔的代碼,一切工作正常。然後我寫了它在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
上面清清楚楚地寫着連接timeout..Make確保您的網絡連接是可用的,也是你的服務器響應請求 – Pragnani 2013-03-27 22:59:51