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.
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:
@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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.