use datagram sockets
[UdpRespond.git] / src / com / example / udpresponder / UdpRespondActivity.java
1 package com.example.udpresponder;
2
3 import java.net.InetAddress;
4 import java.net.InetSocketAddress;
5 import java.net.DatagramPacket;
6 import java.net.DatagramSocket;
7 import android.app.Activity;
8 import android.os.Bundle;
9 import android.os.Handler;
10 import android.os.Message;
11 import android.os.SystemClock;
12 import android.widget.TextView;
13 import android.widget.EditText;
14 import android.widget.Button;
15 import android.view.View;
16 import android.view.View.OnClickListener;
17
18 public class UdpRespondActivity extends Activity {
19
20         private TextView tv;
21         private EditText et;
22
23         private Handler mHandler = new Handler() {
24                 public void handleMessage(Message msg) {
25                         Bundle bdl = msg.getData();
26                         tv.append(bdl.getString("ln"));
27                 }
28         };
29
30         private OnClickListener mStartListener = new OnClickListener() {
31                 public void onClick(View v) {
32                         CharSequence cs = et.getText();
33                         tv.append("Start pressed, data: " +
34                                 cs.toString() +
35                                 " (unimplemented)\n");
36                 }
37         };
38
39         private OnClickListener mStopListener = new OnClickListener() {
40                 public void onClick(View v) {
41                         tv.append("Stop pressed, exiting\n");
42                         finish();
43                 }
44         };
45
46         @Override
47         public void onCreate(Bundle savedInstanceState) {
48                 super.onCreate(savedInstanceState);
49                 setContentView(R.layout.main);
50                 tv = (TextView)findViewById(R.id.tv);
51                 tv.append(" Starting\n");
52                 et = (EditText)findViewById(R.id.text_entry);
53                 Button btn_start = (Button)findViewById(R.id.btn_start);
54                 btn_start.setOnClickListener(mStartListener);
55                 Button btn_stop = (Button)findViewById(R.id.btn_stop);
56                 btn_stop.setOnClickListener(mStopListener);
57                 startBgThread();
58         }
59
60         private void println(String str) {
61                 Bundle bdl = new Bundle();
62                 bdl.putString("ln", str + "\n");
63                 Message msg = Message.obtain(mHandler);
64                 msg.setData(bdl);
65                 msg.sendToTarget();
66         }
67
68         protected void startBgThread () {
69                 Thread t = new Thread() {
70                         public void run() {
71                                 bgTask();
72                         }
73                 };
74                 t.start();
75         }
76
77         private void bgTask() {
78                 DatagramSocket s = null;
79                 try {
80                         s = new DatagramSocket(9999);
81                 } catch (java.net.SocketException e) {
82                         println("socket: " + e);
83                         return;
84                 }
85 //              InetSocketAddress sa = new InetSocketAddress(9999);
86 //              try {
87 //                      s.bind(sa);
88 //              } catch (IllegalArgumentException e) {
89 //                      println("bind: " + e);
90 //                      return;
91 //              } catch (java.net.SocketException e) {
92 //                      println("bind: " + e);
93 //                      return;
94 //              }
95                 println("Bound to port " + s.getLocalPort());
96                 while (true) {
97                         byte[] data = new byte[256];
98                         DatagramPacket pkt = new DatagramPacket(data, 256);
99                         try {
100                                 s.receive(pkt);
101                         } catch (java.io.IOException e) {
102                                 println("receive: " + e);
103                         }
104                         InetAddress ia = pkt.getAddress();
105                         int ip = pkt.getPort();
106                         byte[] id = pkt.getData();
107                         println(ia.toString() + ":" +
108                                 ip + " " +
109                                 id.toString());
110                 }
111         }
112 }