Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package fr.test.udp;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.support.design.widget.FloatingActionButton;
- import android.support.design.widget.Snackbar;
- import android.support.v4.os.AsyncTaskCompat;
- import android.support.v7.app.AppCompatActivity;
- import android.support.v7.widget.Toolbar;
- import android.util.Log;
- import android.view.View;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.Button;
- import android.widget.TextView;
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- import java.net.InetSocketAddress;
- import java.net.NetworkInterface;
- import java.net.SocketException;
- import java.net.UnknownHostException;
- import java.util.Enumeration;
- public class MainActivity extends AppCompatActivity {
- TextView debug1,debug2;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- new runUdpServer().start();
- debug1 = (TextView)findViewById(R.id.textView);
- debug2 = (TextView)findViewById(R.id.textView);
- debug1.setText("Start "+getIpAddress());
- Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
- fab.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
- .setAction("Action", null).show();
- }
- });
- Button btn = (Button) findViewById(R.id.button);
- btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- debug1.setText("Envoie");
- new runUdpClient("yooo").start();
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- private static final int UDP_SERVER_PORT = 8051;
- private static final int MAX_UDP_DATAGRAM_LEN = 1500;
- DatagramSocket ds = null;
- public class runUdpServer extends Thread {
- public void run() {
- String lText;
- byte[] lMsg = new byte[MAX_UDP_DATAGRAM_LEN];
- DatagramPacket dp = new DatagramPacket(lMsg, lMsg.length);
- DatagramSocket ds = null;
- try {
- if (ds == null) {
- try {
- ds = new DatagramSocket(null);
- } catch (SocketException e) {
- e.printStackTrace();
- }
- try {
- ds.setReuseAddress(true);
- } catch (SocketException e) {
- e.printStackTrace();
- }
- try {
- ds.setBroadcast(true);
- } catch (SocketException e) {
- e.printStackTrace();
- }
- try {
- ds.bind(new InetSocketAddress(UDP_SERVER_PORT));
- } catch (SocketException e) {
- e.printStackTrace();
- }
- }
- //ds.setSoTimeout(100000);
- ds.receive(dp);
- lText = new String(lMsg, 0, dp.getLength());
- Log.i("UDP packet received", lText);
- debug2.setText(lText);
- ds.close();
- } catch (SocketException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (ds != null) {
- ds.close();
- }
- }
- }
- }
- public class runUdpClient extends Thread {
- String Msg;
- public runUdpClient(String str)
- {
- super(str);
- Msg = str;
- debug1.setText("Start Client");
- }
- DatagramSocket ds = null;
- public void run() {
- if (ds == null) {
- try {
- ds = new DatagramSocket(null);
- } catch (SocketException e) {
- e.printStackTrace();
- }
- try {
- ds.setReuseAddress(true);
- } catch (SocketException e) {
- e.printStackTrace();
- }
- try {
- ds.setBroadcast(true);
- } catch (SocketException e) {
- e.printStackTrace();
- }
- try {
- ds.bind(new InetSocketAddress(UDP_SERVER_PORT));
- } catch (SocketException e) {
- e.printStackTrace();
- }
- }
- try {
- byte[] buf = new byte[30];
- buf = Msg.getBytes();
- InetAddress adrSrv = InetAddress.getByName("192.168.xxx.xxx");
- DatagramPacket packet = new DatagramPacket(buf, buf.length, adrSrv, UDP_SERVER_PORT);
- ds.send(packet);
- ds.close();
- } catch (SocketException e) {
- e.printStackTrace();
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (ds != null) {
- ds.close();
- }
- }
- }
- }
- private String getIpAddress() {
- String ip = "";
- try {
- Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
- .getNetworkInterfaces();
- while (enumNetworkInterfaces.hasMoreElements()) {
- NetworkInterface networkInterface = enumNetworkInterfaces
- .nextElement();
- Enumeration<InetAddress> enumInetAddress = networkInterface
- .getInetAddresses();
- while (enumInetAddress.hasMoreElements()) {
- InetAddress inetAddress = enumInetAddress.nextElement();
- if (inetAddress.isSiteLocalAddress()) {
- ip += inetAddress.getHostAddress();
- }
- }
- }
- } catch (SocketException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- ip += "Something Wrong! " + e.toString() + "\n";
- }
- return ip;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment