Skip to content

Commit 99f3d76

Browse files
committed
Adds the worlds ugliest UI, separates send and receive threads
Now tested with sockettest[1]; this is indeed sending "Wake up" payload to the UDP socket specified. Now, if only there was an existing user interface definition that we could copy-paste this code into ... [1] http://sockettest.sourceforge.net/
1 parent c9c1e66 commit 99f3d76

File tree

3 files changed

+85
-25
lines changed

3 files changed

+85
-25
lines changed

Android UDP/res/layout/main.xml

+20-6
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,30 @@
44
android:layout_height="fill_parent"
55
android:orientation="vertical" >
66

7-
<TextView
8-
android:id="@+id/hello"
9-
android:layout_width="fill_parent"
7+
<EditText
8+
android:id="@+id/hostname"
9+
android:layout_width="match_parent"
10+
android:layout_height="wrap_content" >
11+
12+
<requestFocus />
13+
</EditText>
14+
15+
<EditText
16+
android:id="@+id/port"
17+
android:layout_width="match_parent"
1018
android:layout_height="wrap_content"
11-
android:text="@string/hello" />
19+
android:inputType="number" />
1220

1321
<Button
14-
android:id="@+id/button1"
22+
android:id="@+id/sendPacketButton"
1523
android:layout_width="wrap_content"
1624
android:layout_height="wrap_content"
17-
android:text="Button" />
25+
android:text="@string/sendLabel" />
26+
27+
<TextView
28+
android:id="@+id/output"
29+
android:layout_width="fill_parent"
30+
android:layout_height="wrap_content"
31+
android:text="@string/hello" />
1832

1933
</LinearLayout>

Android UDP/res/values/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33

44
<string name="hello">Hello World, AndroidUDPActivity!</string>
55
<string name="app_name">Android UDP</string>
6+
<string name="sendLabel">Send Packet</string>
67

78
</resources>

Android UDP/src/org/glennji/devcamp/AndroidUDPActivity.java

+64-19
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.app.Activity;
1111
import android.os.Bundle;
1212
import android.util.Log;
13+
import android.widget.EditText;
1314
import android.widget.TextView;
1415

1516
import com.googlecode.androidannotations.annotations.Background;
@@ -22,33 +23,55 @@
2223
public class AndroidUDPActivity extends Activity {
2324

2425
@ViewById
25-
TextView hello;
26+
TextView output;
27+
28+
@ViewById
29+
EditText hostname;
30+
31+
@ViewById
32+
EditText port;
2633

27-
int PORT = 12345;
2834
String data = "Wake up, you!";
29-
String host = "10.0.1.129";
3035

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;
3461
}
3562

3663
@Background
37-
void doSomeStuffInBackground() {
64+
void sendPacket(DatagramSocket socket, int port) {
3865
try {
39-
InetAddress address = InetAddress.getByName(host);
40-
DatagramSocket socket = new DatagramSocket(PORT);
66+
InetAddress address = InetAddress.getByName(hostname.getText()
67+
.toString());
68+
4169
DatagramPacket packet = new DatagramPacket(data.getBytes(),
42-
data.length(), address, PORT);
70+
data.length(), address, port);
4371
socket.send(packet);
4472
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+
5275
} catch (UnknownHostException e) {
5376
// TODO Auto-generated catch block
5477
e.printStackTrace();
@@ -61,9 +84,31 @@ void doSomeStuffInBackground() {
6184
}
6285
}
6386

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+
64109
@UiThread
65-
void publishProgress(String update) {
66-
hello.setText(update);
110+
void printOutput(String update) {
111+
output.append("\n" + update);
67112
}
68113

69114
}

0 commit comments

Comments
 (0)