-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTCPSockets.java
More file actions
66 lines (51 loc) · 1.75 KB
/
TCPSockets.java
File metadata and controls
66 lines (51 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.habel.plugin.sockets;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import org.apache.cordova.api.Plugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import android.util.Log;
public class TCPSockets extends Plugin {
@Override
public PluginResult execute(String action, JSONArray args, String callbackID) {
Log.d("TCPSockets", "Plugin Called");
}
public PluginResult sendMessage(JSONArray args)
{
try {
int port = args.getInt(0);
String host = args.getString(1);
String message = args.getString(2);
String reply = "";
try {
Socket sock = new Socket(host, port);
Log.d("TCPSockets", "Socket created");
PrintWriter out = new PrintWriter(sock.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
Log.d("TCPSockets", "Created reader/writer");
out.println(message);
Log.d("TCPSockets", "Sent message");
reply = in.readLine();
Log.d("TCPSockets", "Received reply: " + reply);
out.close();
in.close();
sock.close();
return new PluginResult(PluginResult.Status.OK, reply);
} catch (UnknownHostException e) {
Log.d("TCPSockets", "Unknown Host");
return new PluginResult(PluginResult.Status.IO_EXCEPTION);
} catch (IOException e) {
Log.d("TCPSockets", "IOException");
return new PluginResult(PluginResult.Status.IO_EXCEPTION);
}
} catch (JSONException e) {
Log.d("TCPSockets", "JSONException: " + e.getMessage());
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
}
}
}