我目前正在探索 Java 中的 UDP 数据包传输,以在 Android 上创建多人游戏。我通过使用通常的“127.0.0.1”成功地在我的 Nexus 4 中交换了数据包,并且我还成功地在我的 PC 服务器和本地网络中的 Android 客户端之间交换了数据包。但是由于我希望我的游戏可以在 Internet 上玩,所以我希望我的 Android 客户端能够在我的 PC 服务器不在同一个本地网络上时交换数据包。这就是我挣扎的地方。
我的设置:一台 PC 服务器与我的家庭 Internet 连接和一台 Nexus 4 与 3G 网络连接。
首先,我的 PC 服务器开始监听 10000 端口,我的 Android 客户端打开一个套接字以在端口 10001 上接收服务器的数据包。然后,Android 客户端向 PC 服务器发送一个数据包到其当前公共地址“173.246.12.125”上的端口10000。PC服务器收到数据包并在10001端口向发送者发送响应。但Android客户端从未收到响应。
这是我的 PC 服务器代码:
public class UDPServer {
private final static int SERVER_PORT = 10000;
private final static int CLIENT_PORT = 10001;
public static void main(String[] args) {
InetAddress clientAddr = null;
DatagramSocket socket = null;
try {
//Initializing the UDP server
System.out.println(String.format("Connecting on %s...", SERVER_PORT));
socket = new DatagramSocket(SERVER_PORT);
System.out.println("Connected.");
System.out.println("====================");
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
}
while(true){
try {
//Listening
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
System.out.println("Listening...");
socket.receive(packet);
//Getting client address from the packet we received
clientAddr = packet.getAddress();
System.out.println("Received: '" + new String(packet.getData()).trim() + "' from "+clientAddr.toString());
//Sending response
byte[] message = ("Hello Android").getBytes();
DatagramPacket response = new DatagramPacket(message, message.length, clientAddr, CLIENT_PORT);
DatagramSocket clientSocket = new DatagramSocket();
System.out.println("Sending: '" + new String(message) + "'");
clientSocket.send(response);
System.out.println("Response sent.");
System.out.println("--------------------");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
这是我的 Android 客户端类:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(new Receiver()).start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(new Client()).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.activity_main, menu);
return true;
}
}
public class Receiver implements Runnable {
private final static int LISTENING_PORT = 10001;
@Override
public void run() {
try {
//Opening listening socket
Log.d("UDP Receiver", "Opening listening socket on port "+LISTENING_PORT+"...");
DatagramSocket socket = new DatagramSocket(LISTENING_PORT);
socket.setBroadcast(true);
socket.setReuseAddress(true);
while(true){
//Listening on socket
Log.d("UDP Receiver", "Listening...");
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
Log.d("UDP", "Received: '" + new String(packet.getData()).trim() + "'");
}
} catch (Exception e) {
Log.e("UDP", "Receiver error", e);
}
}
}
public class Client implements Runnable {
private final static String SERVER_ADDRESS = "173.246.12.125";//public ip of my server
private final static int SERVER_PORT = 10000;
@Override
public void run() {
try {
//Preparing the socket
InetAddress serverAddr = InetAddress.getByName(SERVER_ADDRESS);
DatagramSocket socket = new DatagramSocket();
//Preparing the packet
byte[] buf = ("Hello computer").getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, SERVER_PORT);
//Sending the packet
Log.d("UDP", String.format("Sending: '%s' to %s:%s", new String(buf), SERVER_ADDRESS, SERVER_PORT));
socket.send(packet);
Log.d("UDP", "Packet sent.");
} catch (Exception e) {
Log.e("UDP", "Client error", e);
}
}
}
服务器的控制台显示客户端的 IP:
Connecting on 192.168.1.126:10000...
Connected.
====================
Listening...
Received: 'Hello computer' from /204.48.72.68
Sending: 'Hello Android'
Response sent.
--------------------
Listening...
数据包似乎来自地址 204.48.72.68,但如果我在我的 Android 上访问 whatismyip.com,它会显示 96.22.246.97...我不知道 204.48.72.68 来自哪里...
我不确定问题是我的 Android 客户端上的侦听套接字不好,还是 PC 服务器没有将响应发送到正确的地址。有人可以指出我做错了什么吗?
谢谢