I send data from a PC to Android. But I can't receive the data using UDP in Android. How to receive data by using UDP in Android? How can I see the received data on the android-display? Can anyone help me?
Code is here:
public class MainActivity extends Activity {
EditText textOut;
EditText port;
int UDP_SERVER_PORT = 8255;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
try {
int port = UDP_SERVER_PORT;
Log.d("UDP", "Connecting..");
DatagramSocket clientsocket = new DatagramSocket(port);
byte[] receivebuf = new byte[1024];
DatagramPacket receivepacket =
new DatagramPacket(receivebuf,receivebuf.length);
clientsocket.receive(receivepacket);
String modifiedSentence = new String(receivepacket.getData());
System.out.println("FROM SERVER:" + modifiedSentence);
clientsocket.close();
} catch (Exception e) {
Log.e("UDP", "C: Error", e);
}
};
}
For looking the data recevied let us suppose data is converted to string and then you need a view to set its value equal to data recevied . Does it make sense to you .
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.