Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Android udp client

By: godens on Mar 2nd, 2013  |  syntax: Java  |  size: 1.12 KB  |  hits: 31  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public class MainActivity extends Activity {
  2.         @Override
  3.         public void onCreate(Bundle savedInstanceState) {
  4.  
  5.                 super.onCreate(savedInstanceState);
  6.  
  7.                 setContentView(R.layout.activity_main);
  8.                 byte[] receiveData = new byte[50];
  9.                 DatagramPacket  receivePacket = new DatagramPacket(receiveData, receiveData.length);
  10.                 System.out.println(getLocalIpAddress());
  11.                 DatagramSocket serverSocket;
  12.                 try {
  13.                         serverSocket = new DatagramSocket(57111);
  14.                         serverSocket.receive(receivePacket);
  15.                          String response = new String(receivePacket.getData(), 0,
  16.                                          receivePacket.getLength(), "UTF-8");
  17.                         TextView t = (TextView) findViewById(R.id.textView1);
  18.                         t.setText(String.valueOf(new String(response)));
  19.                        
  20.                 } catch (SocketException e1) {
  21.                         // TODO Auto-generated catch block
  22.                         e1.printStackTrace();
  23.                 } catch (IOException e) {
  24.                         // TODO Auto-generated catch block
  25.                         e.printStackTrace();
  26.                 }
  27.         }
  28.                
  29.                
  30.  
  31.         @Override
  32.         public boolean onCreateOptionsMenu(Menu menu) {
  33.                 // Inflate the menu; this adds items to the action bar if it is present.
  34.                 getMenuInflater().inflate(R.menu.activity_main, menu);
  35.                 return true;
  36.         }
  37.  
  38. }