-
Notifications
You must be signed in to change notification settings - Fork 0
/
TCPClient.java
83 lines (59 loc) · 1.86 KB
/
TCPClient.java
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package ronidea.viewonphone;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
* TCP client to send data to a TCP server running
* on the related server.
*/
class TCPClient {
private static Socket client;
private static PrintWriter out;
private static BufferedReader in;
private static final int PORT = 7492; // no reason, just a random number
TCPClient() {}
static void send(final String link) {
new Thread(new Runnable() {
@Override
public void run() {
String ip = Prefs.getString(Prefs.KEY_IP_ADR);
// connect client with server
try {
client = null;
client = new Socket(ip, PORT);
} catch (IOException e) {
e.printStackTrace();
return;
}
// send link to server
try {
out = new PrintWriter(client.getOutputStream(), true);
} catch (IOException e) {
e.printStackTrace();
}
out.println(link);
out.close();
Log.d("TCPClient, send: ", link);
}
}).start();
Log.d("tcpclient", "gesendet");
}
static String getInputLine() {
if (client == null) {
return null;
}
in = null;
String inputLine = "nothing";
try {
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
inputLine = in.readLine();
} catch (IOException | NullPointerException e) {
e.printStackTrace();
return null;
}
return inputLine;
}
}