I am trying to establish a UDP send/Receive app. I send a message, the server responds back, and then the server MAY send more info over time. I created a thread to send the message, and another Async thread to continuously check in the background for new messages. However, I am not receiving the messages correctly. It works correctly if I have a constant feed of receive packets, but it doesn't work when the receive is random.
For example: Port 1: constantly sends data to my client, so I receive new packets every second. The result is fine, my app shows each and every new packet. My "Receiving update" even ticks to go like "Receiving." "Receiving.." "Receiving..."
Port 2: i receive packets randomly. it shows them only if I keep clicking my send button. My receiving progress dots only iterate when I press the send button. Sometimes a packet flashes and goes away.
I am trying to make port 2 work. I tried putting my inbackground() function to sleep for 2 seconds after I publish a progress, but that didn't help. I am really confused how to make it so I can constantly receive UDP packets. Here is my code.
EDIT: Now port 2 displays correctly, the message doesn't just flash and disappear. However, my Receiving... progress dots update really slow. My question is, in my "doInBackground" function, I have the infinite while loop, doesn't that loop execute constantly really fast? So even if there is no new message, I still publish at the end of the loop so that my "Receiving.." progress dots should move right?
EDIT AGAIN: I literally changed nothing to the source code, just some layout stuff, and now port 2 doesn't work again. It doesn't show any new receives or the receiving update bar.
If I go to port 1 with the constant feed, then everything updates perfectly...
I tried looking at other questions, couldn't decipher what to do. Thanks for your help!
import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; import android.os.AsyncTask; import android.os.Bundle; import android.os.StrictMode; import android.provider.Settings.Global; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { Button button; TextView txt1, txtH, txtE, txtER, txtUpdate; String msg; CharSequence oldMsg="a"; Integer updateCount=0; Activity mActivity; DatagramSocket socket; boolean msgSent = false; boolean errorSend = false; boolean errorReceive = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txt1 = (TextView) findViewById(R.id.textView2); txtH = (TextView) findViewById(R.id.textView1); txtE = (TextView) findViewById(R.id.textView6); txtER = (TextView) findViewById(R.id.textView8); txtUpdate = (TextView) findViewById(R.id.textView9); //I start my async class here new receiveUDP().execute(); button = (Button) findViewById(R.id.button1); //When I click this, I send a message button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Thread sendThread = new Thread(){ public void run(){ try{ byte[] data ="Some MSG".getBytes(); InetAddress address = InetAddress.getByName("Some address"); DatagramPacket pack = null; pack = new DatagramPacket(data,data.length, address, somePort); socket.send(pack); msgSent=true; } catch (Exception e){ boolean errorSend = true; } } }; sendThread.start(); try { sendThread.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (msgSent){ txtH.setText("SENT!"); } } }); if (errorSend){ txtE.setText("Error Sending Socket"); } } public class receiveUDP extends AsyncTask <Void, CharSequence, Void>{ @Override protected Void doInBackground(Void... params) { //Constantly check to see if we received a new packet. while (true){ try{ //if no socket, create a new socket if (socket == null){ socket = new DatagramSocket(somePort, InetAddress.getByName("address")); socket.setBroadcast(true); } byte[] buf = new byte[2500]; DatagramPacket packet = new DatagramPacket(buf, buf.length); socket.receive(packet); //Get the data of the packet to be a string msg = new String(packet.getData(),0,packet.getLength()); } catch (Exception e){ errorReceive=true; e.printStackTrace(); } publishProgress(msg); } } protected void onProgressUpdate(CharSequence...progress) { updateCount++; // If no errors, and if new message is different than old message // Then change the text field to show new message. if(!(errorReceive)){ if(!(oldMsg.equals(progress[0]))){ txt1.setText(progress[0]); oldMsg = progress[0]; } }else { txtER.setText("Error Receiving"); } //Progress dots... if(updateCount==1){ txtUpdate.setText("Receiving."); } else if(updateCount==2){ txtUpdate.setText("Receiving.."); } else { txtUpdate.setText("Receiving..."); updateCount=0; } } } }