10
10
import android .app .Activity ;
11
11
import android .os .Bundle ;
12
12
import android .util .Log ;
13
+ import android .widget .EditText ;
13
14
import android .widget .TextView ;
14
15
15
16
import com .googlecode .androidannotations .annotations .Background ;
22
23
public class AndroidUDPActivity extends Activity {
23
24
24
25
@ ViewById
25
- TextView hello ;
26
+ TextView output ;
27
+
28
+ @ ViewById
29
+ EditText hostname ;
30
+
31
+ @ ViewById
32
+ EditText port ;
26
33
27
- int PORT = 12345 ;
28
34
String data = "Wake up, you!" ;
29
- String host = "10.0.1.129" ;
30
35
31
- @ Click (R .id .button1 )
32
- void startForkableComputation () {
33
- doSomeStuffInBackground ();
36
+ @ Click (R .id .sendPacketButton )
37
+ void clickSendPacketButton () {
38
+ DatagramSocket socket ;
39
+ try {
40
+ int portInt = Integer .parseInt (port .getText ().toString ());
41
+ if (portInt < 1024 ) {
42
+ printOutput ("Can't send to a port below 1024" );
43
+ port .setText ("1024" );
44
+ }
45
+ socket = openSocket (portInt );
46
+ sendPacket (socket , portInt );
47
+ receiveResponse (socket );
48
+ } catch (NumberFormatException e ) {
49
+ printOutput ("Port must be a number!" );
50
+ }
51
+ }
52
+
53
+ DatagramSocket openSocket (int port ) {
54
+ try {
55
+ return new DatagramSocket (port );
56
+ } catch (SocketException e ) {
57
+ // TODO Auto-generated catch block
58
+ e .printStackTrace ();
59
+ }
60
+ return null ;
34
61
}
35
62
36
63
@ Background
37
- void doSomeStuffInBackground ( ) {
64
+ void sendPacket ( DatagramSocket socket , int port ) {
38
65
try {
39
- InetAddress address = InetAddress .getByName (host );
40
- DatagramSocket socket = new DatagramSocket (PORT );
66
+ InetAddress address = InetAddress .getByName (hostname .getText ()
67
+ .toString ());
68
+
41
69
DatagramPacket packet = new DatagramPacket (data .getBytes (),
42
- data .length (), address , PORT );
70
+ data .length (), address , port );
43
71
socket .send (packet );
44
72
Log .d ("AndroidUDP" , "Sent: " + packet );
45
- publishProgress ("Packet sent" );
46
- byte [] buf = new byte [1024 ];
47
- DatagramPacket inputPacket = new DatagramPacket (buf , buf .length );
48
- socket .receive (inputPacket );
49
- String s = new String (packet .getData (), 0 , packet .getLength ());
50
- Log .d ("AndroidUDP" , "Received: " + s );
51
- publishProgress (s );
73
+ printOutput ("Packet sent, waiting for response" );
74
+
52
75
} catch (UnknownHostException e ) {
53
76
// TODO Auto-generated catch block
54
77
e .printStackTrace ();
@@ -61,9 +84,31 @@ void doSomeStuffInBackground() {
61
84
}
62
85
}
63
86
87
+ @ Background
88
+ void receiveResponse (DatagramSocket socket ) {
89
+ try {
90
+ byte [] buf = new byte [1024 ];
91
+ DatagramPacket packet = new DatagramPacket (buf , buf .length );
92
+ socket .setSoTimeout (3 );
93
+ socket .receive (packet );
94
+ if (packet .getData ().length < 1 ) {
95
+ printOutput ("Timed out with no response" );
96
+ } else {
97
+ String s = new String (packet .getData (), 0 , packet .getLength ());
98
+ Log .d ("AndroidUDP" , "Received: " + s );
99
+ printOutput (s );
100
+ }
101
+ socket .close ();
102
+ } catch (SocketException sex ) {
103
+ printOutput ("Socket exception! " + sex .getMessage ());
104
+ } catch (IOException ioex ) {
105
+ printOutput ("IOException! " + ioex .getMessage ());
106
+ }
107
+ }
108
+
64
109
@ UiThread
65
- void publishProgress (String update ) {
66
- hello . setText ( update );
110
+ void printOutput (String update ) {
111
+ output . append ( " \n " + update );
67
112
}
68
113
69
114
}
0 commit comments