0

this has been bugging me for a while and I'm not too sure how I can go about solving this issue.

The issue I am having is it will take several attempts of pushing a packet before the server will actually receive the packet

This is the code I have on the client (Server is Identical but the Send/Receive are reversed) and the Server gets the client IP from the packet to send a return packet

class MainTask extends AsyncTask<Void, Void, Void> {
ProgressDialog progress = null;
Context context = null;
  public MainTask(ProgressDialog progress, Context context) {
    this.progress = progress;
    this.context = context;
  }

public void onPreExecute() {        
    progress.show();
  }

  public Void doInBackground(Void... unused) {
      // Send UDP Packet
        String messageStr="Hello Android!";
        int server_port = 12441;
        DatagramSocket s = null;
        DatagramPacket p = null;
        InetAddress local = null;
        Log.i("EyeceBoxService", "Sending packet");
        try {
            s = new DatagramSocket();
            local = InetAddress.getByName("192.168.0.4");
            int msg_length=messageStr.length();
            byte[] message = messageStr.getBytes();
            p = new DatagramPacket(message, msg_length,local,server_port);
            s.send(p);
            Log.i("EyeceBoxService", "Packet Sent!");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          // Receive UDP Packet
        byte[] message = new byte[1500];
        try {
            //progress.setMessage("Retrieving Discovery Information...");
            p = new DatagramPacket(message, message.length);
            s = new DatagramSocket(server_port);
            Log.i("EyeceBoxService", "Waiting for packet");
            s.receive(p);
            String address = p.getAddress().toString();
            Log.i("EyeceBoxService", "Server IP address" + address);
            //progress.setMessage("Done...");
            //MainActivity.buildNotification(context, address);
            progress.dismiss();
            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;      
        }

  public void onPostExecute(Void unused) {
  }
}
adam2510
  • 563
  • 1
  • 7
  • 22

1 Answers1

0

Try to isolate whether the issue is in the server hardware, client hardware, Android framework, network, router config or your code. Try using some debugging tools like wireshark, netcat, ipsend and see what packets are actually sent, received, if packet sending works through commandline, etc.