[英]Server not Receiving UDP Packet from Android
I looked at some other articles that seemed similar, however I was unable to find any that were relevant and answered the question. 我看了看似相似的其他文章,但是找不到任何相关的文章并回答了问题。
I am trying to send a UDP packet from an android application to an external server. 我正在尝试将UDP数据包从android应用程序发送到外部服务器。
As of right now, the packet sends successfully (code is completed with no errors) yet the server is not receiving the packet. 截至目前,数据包已成功发送(代码已正确完成,没有错误),但服务器尚未收到该数据包。
I believe I have the necessary permissions (no permissions errors), however if I don't please let me know. 我相信我拥有必要的权限(没有权限错误),但是如果我不知道,请告诉我。 All current permissions are as follows: 当前所有权限如下:
Android Client: Android客户端:
@Override
protected void onResume() {
super.onResume();
// TODO: following is done onResume only because it does not save its data yet
// check for internet first
ConnectivityManager connMgr = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
// download and display plant data
new downloadPlantDataTask().execute("169.234.79.98");
} else {
// display error
TextView viewErrors = (TextView) findViewById(R.id.view_errors);
viewErrors.setText("No internet connection...");
}
}
public String[] downloadPlantData(String ip) throws IOException {
Vector<String> plantsNShit = new Vector<String>();
TextView viewErrors = (TextView) findViewById(R.id.view_errors); // TODO: temp(?)
// get datagram socket
DatagramSocket socket = new DatagramSocket();
// setup info for request
byte[] buf = new byte[256];
buf[0] = 1;
InetAddress address = InetAddress.getByName(ip);
DatagramPacket packet;
while (true) {
// send request
for (int i = 0; i < 5; i++) {
viewErrors.setText("sending packet"); // TODO: temp
packet = new DatagramPacket(buf, buf.length, address, 60000);
try {
socket.send(packet);
Log.i("Packet message", "packet sent"); // TODO: temp
} catch (Exception e) {
Log.e("Packet error", e.getMessage());
}
}
// get response
packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
viewErrors.setText("packet received"); // TODO: temp
// store response
String received = new String(packet.getData(), 0, packet.getLength());
if (received == "DONE")
break;
plantsNShit.add(received); // TODO: temporarily storing in vector
}
viewErrors.setText("finished");
socket.close();
return plantsNShit.toArray(new String[plantsNShit.size()]);
}
Server Code 服务器代码
import java.io.*;
import java.net.*;
public class DatagramServerThread extends Thread{
protected DatagramSocket socket = null;
protected BufferedReader in = null;
protected boolean moreLines = true;
public DatagramServerThread() throws IOException {
this("DatagramServer");
}
public DatagramServerThread(String name) throws IOException {
super(name);
socket = new DatagramSocket(60000);
try {
in = new BufferedReader(new FileReader("pingas.txt"));
} catch (FileNotFoundException e) {
System.err.println("Couldn't open file. You dun messed up son.");
}
}
public void run() {
while (moreLines) {
try {
byte[] buf = new byte[256];
// receive request
System.out.println("waiting for packet");
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
System.out.println("packet received");
// figure out response
String dString = null;
if (in == null)
dString = "DONE";
else
dString = getNextLine();
buf = dString.getBytes();
// send the response
InetAddress address = packet.getAddress();
int port = packet.getPort();
packet = new DatagramPacket(buf, buf.length, address, port);
socket.send(packet);
System.out.println("packet sent");
} catch (IOException e) {
e.printStackTrace();
moreLines = false;
}
}
socket.close();
}
protected String getNextLine() {
String returnValue = null;
try {
if ((returnValue = in.readLine()) == null) {
in.close();
moreLines = false;
returnValue = "DONE";
}
} catch (IOException e) {
returnValue = "IOException occurred in server.";
}
return returnValue;
}
}
*Also, if I am showing too much or too little code, or violating any other rules and stuff please let me know so I can get better. *此外,如果我显示的代码太多或太少,或者违反了其他任何规则和规定,请告诉我,以便我可以做得更好。 Thank you. 谢谢。
Two items: 两项:
Assuming when your code did work, both the client and server were on the same machine, what's most likely happening is that your packets are being blocked by your machine's firewall, since your client (your phone) is now making requests from outside the machine. 假设您的代码正常工作时,客户端和服务器都在同一台计算机上,则最有可能发生的事情是您的数据包被计算机的防火墙阻止,因为您的客户端(您的电话)现在正在从计算机外部发出请求。
Not sure what OS you are using (Windows, OS X, Linux?), but make sure you go into the firewall settings, and open the corresponding port 5673. 不确定使用的是哪种操作系统(Windows,OS X,Linux?),但请确保进入防火墙设置并打开相应的端口5673。
In your client code, I don't see it specifically sending to the same port that your server is listening on. 在您的客户端代码中,我看不到它专门发送到服务器正在侦听的端口。 This is also important. 这也很重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.