【发布时间】:2016-01-27 23:04:43
【问题描述】:
我创建了一个服务器,它将在我的桌面上运行以发送和接收来自客户端的消息。客户端应用程序将在 android 上运行。到目前为止,两个程序都启动了,客户端能够将初始连接发送到服务器。我的桌面通过路由器连接到互联网,我已从路由器转发端口以接收信息。
服务器能够接收客户端发送的所有消息。但是,服务器无法响应。我对收到的数据包(服务器端)进行了快速检查,它说数据包的 ipaddress 是我的路由器的 IP。这是否意味着我将无法发送响应,因为我所有的数据包都会将其 IP 更改为路由器的本地地址(因为端口转发)?
我将在下面发布一些代码,尽管我不确定它是否会有所帮助。非常感谢您的 cmets。
服务器:
public class QuoteServerThread extends Thread {
protected DatagramSocket socket = null;
DatagramPacket dgp = null;
protected boolean running = true;
ArrayList<ConnectedUser> users = new ArrayList<>();
String state;
int port;
int userId;
public QuoteServerThread(String name, String state) throws IOException {
super(name);
port = 4445;
userId = 1;
socket = new DatagramSocket(port);
this.state = state;
this.state = "listening";
System.out.println("Server Socket is now listening on port: " + port);
}
public void run() {
while (running) {
try {
byte[] buf = new byte[256];
// receive request
final DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
if(packet.getData() == null){
System.out.println(packet.getData());
return;
}
else{
System.out.println(packet.getData());
}
dgp = packet;
String message = new String(packet.getData(), 0, packet.getLength());
if(state.equals("listening")) {
if(message.contains("newUser")){
ConnectedUser newUser = new ConnectedUser(userId, socket);
users.add(newUser);
System.out.println("connected users: " + users.size());
String msg = "ID/" + userId;
InetAddress IPAddress = packet.getAddress();
int _port = packet.getPort();
System.out.println(IPAddress + "," + _port);
DatagramPacket out = new DatagramPacket(msg.getBytes(), msg.length(), IPAddress, _port);
socket.send(out);
userId++;
}
else if(message.contains("ID/")){
String receivedMessage[] = message.split("/");
System.out.println(receivedMessage[0] + ", " + receivedMessage[1] + ", " + receivedMessage[2]);
}
else{
System.out.println(message);
}
}
} catch (IOException e) {
e.printStackTrace();
running = false;
}
}
socket.close();
}
}
客户:
public class QuoteServerThread extends Thread {
protected DatagramSocket socket = null;
protected boolean running = true;
TextView chatWindow;
TextView notification;
EditText chatBox;
MainActivity ma;
ScrollView sv;
public QuoteServerThread(MainActivity ma, TextView chatWindow, EditText chatBox, ScrollView sv, TextView notification) throws IOException {
this("QuoteServerThread");
this.ma = ma;
this.chatWindow = chatWindow;
this.sv = sv;
this.chatBox = chatBox;
this.notification = notification;
}
public QuoteServerThread(String name) throws IOException {
super(name);
socket = new DatagramSocket();
}
public void run() {
while (running) {
try {
byte[] buf = new byte[256];
// receive request
final DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
if(packet.getData() == null){
System.out.println("data was null!");
return;
}
String message = new String(packet.getData(), 0, packet.getLength());
final String notif = message;
notification.post(new Runnable() {
public void run() {
notification.setText("server " + "__msg: " + notif);
}
});
if(ma.state.equals("connected")) {
final String chatText = chatWindow.getText().toString() + "\n" + "Sender: " + message;
chatWindow.post(new Runnable() {
public void run() {
chatWindow.setText(chatText);
sv.post(new Runnable() {
public void run() {
sv.fullScroll(View.FOCUS_DOWN);
chatBox.post(new Runnable() {
public void run() {
chatBox.requestFocus();
}
});
}
});
}
});
}
else if(ma.state.equals("waitingconnection")){
String msgSplit[];
if(message.contains("ID/")){
msgSplit = message.split("/");
final String id = msgSplit[1];
ma.clientID = Integer.getInteger(id);
notification.post(new Runnable() {
public void run() {
notification.setText("connected to server " + "__msg: " + id);
ma.state = "connected";
}
});
}
}
} catch (IOException e) {
e.printStackTrace();
running = false;
}
}
socket.close();
}
}
这里唯一的问题是客户端没有收到响应。再次说明问题,我相信我的路由器上的端口转发正在将数据包地址更改为其本地 IP 地址(我的手机是从移动网络发送的)。关于如何解决此问题的任何想法?
【问题讨论】:
-
我可能在无数次测试中发现了这个错误。如果我明天弄清楚,我会用答案更新这篇文章。
标签: java android sockets network-programming datagram