Guest User

android-udp-send-receive-error

a guest
Mar 2nd, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.15 KB | None | 0 0
  1. package fr.test.udp;
  2.  
  3. import android.os.AsyncTask;
  4. import android.os.Bundle;
  5. import android.support.design.widget.FloatingActionButton;
  6. import android.support.design.widget.Snackbar;
  7. import android.support.v4.os.AsyncTaskCompat;
  8. import android.support.v7.app.AppCompatActivity;
  9. import android.support.v7.widget.Toolbar;
  10. import android.util.Log;
  11. import android.view.View;
  12. import android.view.Menu;
  13. import android.view.MenuItem;
  14. import android.widget.Button;
  15. import android.widget.TextView;
  16.  
  17. import java.io.IOException;
  18. import java.net.DatagramPacket;
  19. import java.net.DatagramSocket;
  20. import java.net.InetAddress;
  21. import java.net.InetSocketAddress;
  22. import java.net.NetworkInterface;
  23. import java.net.SocketException;
  24. import java.net.UnknownHostException;
  25. import java.util.Enumeration;
  26.  
  27. public class MainActivity extends AppCompatActivity {
  28.     TextView debug1,debug2;
  29.  
  30.     @Override
  31.     protected void onCreate(Bundle savedInstanceState) {
  32.         super.onCreate(savedInstanceState);
  33.         setContentView(R.layout.activity_main);
  34.         new runUdpServer().start();
  35.  
  36.         debug1 = (TextView)findViewById(R.id.textView);
  37.         debug2 = (TextView)findViewById(R.id.textView);
  38.  
  39.         debug1.setText("Start "+getIpAddress());
  40.         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  41.         setSupportActionBar(toolbar);
  42.  
  43.         FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
  44.         fab.setOnClickListener(new View.OnClickListener() {
  45.             @Override
  46.             public void onClick(View view) {
  47.                 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
  48.                         .setAction("Action", null).show();
  49.             }
  50.         });
  51.  
  52.         Button btn = (Button) findViewById(R.id.button);
  53.         btn.setOnClickListener(new View.OnClickListener() {
  54.             @Override
  55.             public void onClick(View v) {
  56.                 debug1.setText("Envoie");
  57.                 new runUdpClient("yooo").start();
  58.             }
  59.         });
  60.     }
  61.  
  62.     @Override
  63.     public boolean onCreateOptionsMenu(Menu menu) {
  64.         getMenuInflater().inflate(R.menu.menu_main, menu);
  65.         return true;
  66.     }
  67.  
  68.     @Override
  69.     public boolean onOptionsItemSelected(MenuItem item) {
  70.         int id = item.getItemId();
  71.         if (id == R.id.action_settings) {
  72.             return true;
  73.         }
  74.         return super.onOptionsItemSelected(item);
  75.     }
  76.     private static final int UDP_SERVER_PORT = 8051;
  77.  
  78.     private static final int MAX_UDP_DATAGRAM_LEN = 1500;
  79.     DatagramSocket ds = null;
  80.     public class runUdpServer extends Thread {
  81.         public void run() {
  82.             String lText;
  83.             byte[] lMsg = new byte[MAX_UDP_DATAGRAM_LEN];
  84.             DatagramPacket dp = new DatagramPacket(lMsg, lMsg.length);
  85.             DatagramSocket ds = null;
  86.             try {
  87.                 if (ds == null) {
  88.                     try {
  89.                         ds = new DatagramSocket(null);
  90.                     } catch (SocketException e) {
  91.                         e.printStackTrace();
  92.                     }
  93.                     try {
  94.                         ds.setReuseAddress(true);
  95.                     } catch (SocketException e) {
  96.                         e.printStackTrace();
  97.                     }
  98.                     try {
  99.                         ds.setBroadcast(true);
  100.                     } catch (SocketException e) {
  101.                         e.printStackTrace();
  102.                     }
  103.                     try {
  104.                         ds.bind(new InetSocketAddress(UDP_SERVER_PORT));
  105.                     } catch (SocketException e) {
  106.                         e.printStackTrace();
  107.                     }
  108.                 }
  109.                 //ds.setSoTimeout(100000);
  110.                 ds.receive(dp);
  111.                 lText = new String(lMsg, 0, dp.getLength());
  112.                 Log.i("UDP packet received", lText);
  113.                 debug2.setText(lText);
  114.                 ds.close();
  115.             } catch (SocketException e) {
  116.                 e.printStackTrace();
  117.             } catch (IOException e) {
  118.                 e.printStackTrace();
  119.             } finally {
  120.                 if (ds != null) {
  121.                     ds.close();
  122.                 }
  123.             }
  124.         }
  125.     }
  126.  
  127.  
  128.     public class runUdpClient extends Thread  {
  129.         String Msg;
  130.         public runUdpClient(String str)
  131.         {
  132.             super(str);
  133.             Msg = str;
  134.             debug1.setText("Start Client");
  135.         }
  136.  
  137.         DatagramSocket ds = null;
  138.  
  139.         public void run() {
  140.             if (ds == null) {
  141.                 try {
  142.                     ds = new DatagramSocket(null);
  143.                 } catch (SocketException e) {
  144.                     e.printStackTrace();
  145.                 }
  146.                 try {
  147.                     ds.setReuseAddress(true);
  148.                 } catch (SocketException e) {
  149.                     e.printStackTrace();
  150.                 }
  151.                 try {
  152.                     ds.setBroadcast(true);
  153.                 } catch (SocketException e) {
  154.                     e.printStackTrace();
  155.                 }
  156.                 try {
  157.                     ds.bind(new InetSocketAddress(UDP_SERVER_PORT));
  158.                 } catch (SocketException e) {
  159.                     e.printStackTrace();
  160.                 }
  161.             }
  162.             try {
  163.                 byte[] buf = new byte[30];
  164.                 buf = Msg.getBytes();
  165.                InetAddress adrSrv = InetAddress.getByName("192.168.xxx.xxx");
  166.                 DatagramPacket packet = new DatagramPacket(buf, buf.length, adrSrv, UDP_SERVER_PORT);
  167.                 ds.send(packet);
  168.                 ds.close();
  169.             } catch (SocketException e) {
  170.                 e.printStackTrace();
  171.             } catch (UnknownHostException e) {
  172.                 e.printStackTrace();
  173.             } catch (IOException e) {
  174.                 e.printStackTrace();
  175.             } catch (Exception e) {
  176.                 e.printStackTrace();
  177.             } finally {
  178.                 if (ds != null) {
  179.                     ds.close();
  180.                 }
  181.             }
  182.         }
  183.     }
  184.  
  185.     private String getIpAddress() {
  186.         String ip = "";
  187.         try {
  188.             Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
  189.                     .getNetworkInterfaces();
  190.             while (enumNetworkInterfaces.hasMoreElements()) {
  191.                 NetworkInterface networkInterface = enumNetworkInterfaces
  192.                         .nextElement();
  193.                 Enumeration<InetAddress> enumInetAddress = networkInterface
  194.                         .getInetAddresses();
  195.                 while (enumInetAddress.hasMoreElements()) {
  196.                     InetAddress inetAddress = enumInetAddress.nextElement();
  197.  
  198.                     if (inetAddress.isSiteLocalAddress()) {
  199.                         ip += inetAddress.getHostAddress();
  200.                     }
  201.  
  202.                 }
  203.  
  204.             }
  205.  
  206.         } catch (SocketException e) {
  207.             // TODO Auto-generated catch block
  208.             e.printStackTrace();
  209.             ip += "Something Wrong! " + e.toString() + "\n";
  210.         }
  211.  
  212.         return ip;
  213.     }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment