gpt4 book ai didi

android - 无法获取我设备的 IP 地址

转载 作者:行者123 更新时间:2023-11-30 03:07:03 24 4
gpt4 key购买 nike

我在获取设备的 IP 地址时遇到问题。

我想将它用作服务器并将它连接到另一台将充当客户端的 Android 设备。

这是我为服务器应用程序编写的代码。

public class ServerActivity extends Activity {

private TextView serverStatus;

// DEFAULT IP
public static String SERVERIP = "10.0.2.15";

// DESIGNATE A PORT
public static final int SERVERPORT = 8080;

private Handler handler = new Handler();

private ServerSocket serverSocket;

private EditText serverMessage;

String str="";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
serverStatus = (TextView) findViewById(R.id.TextView01);
serverMessage = (EditText) findViewById(R.id.TextView02);

SERVERIP = getLocalIpAddress();

Thread fst = new Thread(new ServerThread());
fst.start();
}


public class ServerThread implements Runnable {

public void run() {
try {
if (SERVERIP != null) {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Listening on IP: " + SERVERIP);
}
});
serverSocket = new ServerSocket(SERVERPORT);
while (true) {
Socket client = serverSocket.accept();
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Connected.");
}
});

try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String line = null;

while ((line = in.readLine()) != null) {
str = line;
Log.d("ServerActivity", line);
handler.post(new Runnable() {
@Override
public void run() {
serverMessage.setText(str);
}
});
}
break;
} catch (Exception e) {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones.");
}
});
e.printStackTrace();
}
}
} else {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Couldn't detect internet connection.");
}
});
}
} catch (Exception e) {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Error");
}
});
e.printStackTrace();
}
}
}

private String getLocalIpAddress()
{
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
return "ERROR Obtaining IP";
}
return "No IP Available";
}

@Override
protected void onStop() {
super.onStop();
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}

当我启动我的应用程序时,它显示在 TextView 中:

"Listening on IP: fe80::2254:76ff:fe85:f2bd%wlan0".  

但是,我通过客户端连接了另一个 IP 地址 192.168.1.2(我在设备的设置中找到了它)。

我的问题是:如何在我的应用中获取第二个 IP 地址?

最佳答案

我希望您正在寻找 IPv4 地址;尝试以下操作:

               boolean isIP4Address = inetAddress instanceof Inet4Address;
if (!inetAddress.isLoopbackAddress() && isIP4Address) {
String Ipv4 = inetAddress.getHostAddress();
//Ipv4 = 192.168.1.2
}

这是 Ipv6 LinkedLocal wifi IP fe80::2254:76ff:fe85:f2bd%wlan0

关于android - 无法获取我设备的 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21626911/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com