gpt4 book ai didi

java - Android 套接字 : listening doesn't work through 4G

转载 作者:太空宇宙 更新时间:2023-11-04 11:20:38 25 4
gpt4 key购买 nike

我正在自学 Android 和 Java 编程。为了练习,我开发了几个应用程序,现在我正在尝试编写一个类似聊天的应用程序。基本上,我想要 WhatsApp 之类的东西。我的电脑上运行着一个我自己编写的服务器程序,Android系统中的聊天就是用它来进行通信的。所以基本上,所有内容都发送到服务器,服务器执行传递消息所需的操作。

这就是我所拥有的:

  1. 首先,我的应用向我的服务器(已知 IP 和端口)发送“Hello”
  2. 服务器存储每个用户使用的IP和端口(也是用户名)
  3. 当服务器被要求向用户传递消息时,它会将消息发送到存储的 IP 和端口

我使用一个套接字来发送“Hello”,但我不能使用同一个套接字来发送回消息(因为我对服务器进行编程的方式)。但这不是问题。当服务器收到“Hello”时,它就会监听相应的端口。但是,当服务器必须启动连接时,应用程序就必须启动连接。为了做到这一点,应用程序必须知道它必须监听哪个端口(由于 NAT,它不知道),我告诉它通过用于“Hello”消息的第一个套接字发回所使用的端口。尽管如此,我的应用程序仍无法接收消息并停留在接收调用状态。

这里有代码:

服务器:

    DatagramSocket client = new DatagramSocket(2048); //Predefined port
DatagramPacket receivePacket;
byte[] receiveData = new byte[1024];
receivePacket = new DatagramPacket(receiveData,
receiveData.length);
System.out.println("Ready to receive");
client.receive(receivePacket);
System.out.println("Received successfuly");
String receivedPort = String.valueOf(receivePacket.getPort());
byte[] sendData = receivedPort.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, receivePacket.getAddress(), receivePacket.getPort());
client.send(sendPacket); //Send back the port used by the app
System.out.println("Sent successfuly");
client.close();
Thread.sleep(1000); //To make sure the app is ready to listen
DatagramSocket socket = new DatagramSocket();
byte[] sendData2 = "Working like a charm".getBytes();
InetAddress ip = receivePacket.getAddress();
int port = receivePacket.getPort();
System.out.println(ip + " " + port);
DatagramPacket sendPacket2 = new DatagramPacket(sendData2, sendData2.length, ip, port);
socket.send(sendPacket);
System.out.println("Sent successfuly 2nd time");
socket.close();

客户端(Android 应用程序):

EditText eT = (EditText) findViewById(R.id.messageText);
String message = eT.getText().toString();
DatagramSocket socket = new DatagramSocket();
byte[] sendData = message.getBytes();
InetAddress ip = InetAddress.getByName(MY_COMPUTER'S_IP);
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, ip, 2048);
socket.send(sendPacket);
byte[] receiveData = new byte[1024];
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
socket.receive(receivePacket); //Receive the port used (and that must be opened and redirecting the traffic to this device due to NAT)
socket.close();
String receivedMessage = new String(receivePacket.getData());
String receivedData = new String(receivePacket.getData());
String actualData = receivedData.substring(0, receivePacket.getLength());
Log.d("Port", actualData);
int port = Integer.parseInt(actualData);
DatagramSocket socket2 = new DatagramSocket(port);
DatagramPacket receivePacket2;
byte[] receiveData2 = new byte[1024];
receivePacket2 = new DatagramPacket(receiveData2,
receiveData2.length);
socket2.receive(receivePacket2); //Here is where it gets blocked
String data = new String(receivePacket2.getData());
Log.d("Finally received", data.substring(receivePacket2.getLength()));
socket2.close();
有谁知道为什么我的消息无法通过第二个套接字返回?我已经检查过,IP和端口都是正确的。

预先感谢您的回答,并对我的格式表示歉意。

最佳答案

许多移动电话公司目前为 3G-4G 连接设备分配专用 IP。

就好像这些设备位于 LAN 内一样。您无权访问该 LAN 的网关路由器,并且如果没有 NAT,则无法从外部访问您设备的监听端口。

要了解您的 IP 是公共(public) IP 还是私有(private) IP,请将设备的 IP 与 showmyip.com 等网站的 IP 进行比较,如果不同,则您的设备位于电话公司 LAN 中。

要获取设备 IP,一些应用程序(例如 PingTools)可以提供帮助。

关于java - Android 套接字 : listening doesn't work through 4G,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44946502/

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