-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathSocketClient.java
More file actions
65 lines (53 loc) · 1.2 KB
/
SocketClient.java
File metadata and controls
65 lines (53 loc) · 1.2 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
package com.fei435;
import java.io.*;
import java.net.*;
import android.util.Log;
public class SocketClient {
static Socket client = null;
public SocketClient(String site, int port) throws UnknownHostException,IOException{
//client = new Socket(site, port);
client = new Socket();
SocketAddress sa = new InetSocketAddress(site, port);
client.connect(sa, Constant.SOCKET_TIMEOUT);
Log.i("socket","Client is created! site:"+site+" port:"+port);
}
public void sendMsg(byte[] msg) {
try {
OutputStream out = client.getOutputStream();
out.write(msg);
out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void closeSocket() {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public InputStream getInputStream () {
if (client != null) {
try {
return client.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
public OutputStream getOutputStream () {
if (client != null) {
try {
return client.getOutputStream();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
// public static void main(String[] args) throws Exception {
//
// }
}