服务器不接收来自客户端的消息(android)

时间:2013-03-27 22:57:47

标签: android client

我在neatbeans中尝试了我的客户端(客户端和服务器)代码,一切正常。然后我在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

1 个答案:

答案 0 :(得分:1)

Android模拟器与您的PC不共享相同的IP地址,这意味着您无法使用环回ip 127.0.0.1 并期望它连接到您的PC。 Android模拟器实例在具有自己的防火墙和子网的虚拟LAN上运行,并且可以通过该子网上的IP与您的PC通信。

是:10.0.2.2

有关模拟器网络的更多信息,请访问this page

希望有所帮助