how to Receive UDP packets, when it comes from server? for android java
11,676
This is a working snippet I am using to receive and parse UDP packets.
try {
int port = 11000;
DatagramSocket dsocket = new DatagramSocket(port);
byte[] buffer = new byte[2048];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (true) {
dsocket.receive(packet);
lText = new String(buffer, 0, packet.getLength());
Log.i("UDP packet received", lText);
data.setText(lText);
packet.setLength(buffer.length);
}
} catch (Exception e) {
System.err.println(e);
e.printStackTrace();
}
Author by
Dinesh Anton Raja
Updated on June 05, 2022Comments
-
Dinesh Anton Raja almost 2 years
I have used a thread for UDP receive packet. When I am sending a packet to that particular IP, where the UDP receive program runs. The application will be stopped unfortunately. Then if I remove the thread called
new Thread(new Runnable())
andpublic void run
the application will run good, but only one data has received. My intention is to receive data at the receiver end continuously, when data comes. please acknowledge me.udpserver.java:
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class UdpServer extends Activity { /** Called when the activity is first created. */ private TextView data; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); data = (TextView) findViewById(R.id.textView); runUdpServer(); } private static final int UDP_SERVER_PORT = 11111; private static final int MAX_UDP_DATAGRAM_LEN = 1500; private void runUdpServer() { new Thread(new Runnable() { public void run() { String lText; byte[] lMsg = new byte[MAX_UDP_DATAGRAM_LEN]; DatagramPacket dp = new DatagramPacket(lMsg, lMsg.length); DatagramSocket ds=null; try { ds = new DatagramSocket(UDP_SERVER_PORT); //disable timeout for testing //ds.setSoTimeout(100000); ds.receive(dp); lText = new String(dp.getData()); Log.i("UDP packet received", lText); data.setText(lText); } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (ds != null) { ds.close(); } } } }).start(); }
-
user207421 over 8 yearsYou're ignoring the length in the datagram.
-
user207421 over 8 yearsYou're ignoring the length in the datagram.
-
jobcrazy over 8 years@EJP Yes, more details about buffer size or even timeout issue should be considered, but this answer only give a work sample to fix the continuous read issue.
-
Dinesh Anton Raja over 8 yearsthe above solutions are not sufficient..only one time the packet has received..but i need the solution that, whenever the packet comes, it has to receive the packet
-
Dinesh Anton Raja over 8 yearsplease acknowledge me for continuos packet reception..if once socket is closed, then it will not be re-assigned.after closing of socket only i can see the incoming data..
-
jobcrazy over 8 years@DineshAntonRaja you mean you want to recreate the socket and continue to receive packet when exception throws?
-
Dinesh Anton Raja over 8 years@jobcrazy i need to read the packet whenever it comes. But now only one time the packet has received. i think no exception throws...i have attached the code in the following website. (www.pdaraja.weebly.com).... i have tired the while loop(true). my intention is the program should always ready to receive the packet. please acknowledge me
-
Dinesh Anton Raja over 8 yearspdaraja.weebly.com/uploads/1/8/2/3/18237947/udpserver.txt this is the code i have used..
-
jobcrazy over 8 yearsYou should not close it until exception throws, just remove ds.close();
-
Dinesh Anton Raja over 8 yearscan you please send me the code. because if i remove ds.close() it will not show the output in textview. then i am using while(true), so how can i code. please acknowledge me..
-
Dinesh Anton Raja over 8 years@jobcrazy can you please send me the code. because if i remove ds.close() it will not show the output in textview. then i am using while(true), so how can i code. please acknowledge me.
-
jobcrazy over 8 years@DineshAntonRaja check your code carefully plz, use !ds.isClosed() not ds.isClosed() in your while loop