Android 我的应用程序无法将消息从我的真实设备发送到emulator!出了什么问题?需要添加更多设置吗?
我是android新手。我已经开始使用一个简单的聊天应用程序。我通过Android 我的应用程序无法将消息从我的真实设备发送到emulator!出了什么问题?需要添加更多设置吗?,android,client-server,emulation,Android,Client Server,Emulation,我是android新手。我已经开始使用一个简单的聊天应用程序。我通过UDPsocket编写了一个简短的代码,一个用于客户端,一个用于服务器。现在,我在连接android real设备时遇到了一些问题,该设备上安装了哪个客户端应用,哪个服务器应用运行在模拟器设备上。 客户端应用程序是一个简单的代码,它必须设置连接到emulator的IP,并且还有一个用于发送消息的EditText。(此时我设置了10.0.2.2或10.0.2.15。但是我的应用程序无法将消息从我的真实设备发送到emulator!出
UDP
socket编写了一个简短的代码,一个用于客户端,一个用于服务器。现在,我在连接android real设备时遇到了一些问题,该设备上安装了哪个客户端应用,哪个服务器应用运行在模拟器设备上。
客户端应用程序是一个简单的代码,它必须设置连接到emulator的IP,并且还有一个用于发送消息的EditText。(此时我设置了10.0.2.2或10.0.2.15。但是我的应用程序无法将消息从我的真实设备发送到emulator!出了什么问题?需要更多设置吗?)
服务器应用程序只有一个文本视图
,用于获取和显示收到的消息。
以下是客户端代码:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class ClienttextchatActivity extends Activity
{
private String udpMsg=null;
Handler hand=new Handler();
EditText edtSetIp=null , edtText=null;
Button btnSetIp=null , btnSend=null;
static String ip=null;
static final String LOG_TAG = "UdpStream";
static final int PORT = 8888;
static final int BUF_SIZE=4096;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edtText=(EditText)findViewById(R.id.edtText);
edtSetIp=(EditText)findViewById(R.id.edtSetIp);
btnSetIp=(Button)findViewById(R.id.btnSetIp);
btnSend=(Button)findViewById(R.id.btnSend);
btnSetIp.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
ip=edtSetIp.getText().toString();
}
});
btnSend.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Thread mythread=new Thread(new th1() );
mythread.start();
}
});
}
//#############################################
public class th1 implements Runnable
{
public void run()
{
udpMsg =edtText.getText().toString();
Log.d(LOG_TAG,udpMsg);
DatagramSocket ds = null;
try
{
ds = new DatagramSocket();
InetAddress serverAddr = InetAddress.getByName(ip);
Log.d(LOG_TAG,"address server address created.");
DatagramPacket dp;
dp = new DatagramPacket(udpMsg.getBytes(), udpMsg.length(), serverAddr, PORT);
ds.send(dp);
Log.d(LOG_TAG,"packet send.");
}
catch (SocketException e)
{
e.printStackTrace();
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (ds != null)
{
ds.close();
}
}
}//end run
}//end class th
}
确保emulator已启动,然后在ADB shell中键入此命令,它将显示emulator的ip地址
adb shell
ifconfig etho