LocalSocket浅析

在使用MediaRecorder录制视频流的时候,突然发现有个LocalSocket类,兴趣使然就研究了下,为了避免遗忘,整理成博文记录下来。

概述

其实LocalSocket和Socket一样,只是Android对其进行了一定的封装,底层还是通过JNI调用C的socket函数。

认识几个常用的函数:
客户端:
LocalSocket客户端使用,创建套接字
LocalSocketAddress 套接字地址,其实就是文件描述符(主要是服务器的地址,当然也可以客户端自个绑定地址)
setSoTimeout设置超时
connect客户端主动向服务端请求连接
setReceiveBufferSize 设置接收缓存区大小
setSendBufferSize 设置发送缓存区大小
服务端:
LocalServerSocket服务端使用,创建套接字同时指定文件描述符
accept等待客户端连接(阻塞)
共同:
getInputStream获取套接字输入流
getOutputStream获取套接字输出流
close关闭套接字,客户服务都需要

Server与Client数据互发

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
public class LocalSocketActivity extends TitleBaseActivity {

private static final String TAG = "LocalSocketActivity";
private static final String SOCKET_NAME="H264";
private static final int BUFFER_SIZE = 500000;
private LocalSocket client;
private LocalSocket server;
private LocalServerSocket lss;

private boolean running ;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_localsocket);
initSocket();
}

private void initSocket() {

try {
client = new LocalSocket();
lss = new LocalServerSocket(SOCKET_NAME);

//LocalServerSocket和LocalSocketAddress的name要相同
client.connect(new LocalSocketAddress(SOCKET_NAME));
client.setReceiveBufferSize(BUFFER_SIZE);
client.setSendBufferSize(BUFFER_SIZE);

server = lss.accept();
Credentials cre= server.getPeerCredentials();
Log.e(TAG,cre.getUid()+"");
server.setReceiveBufferSize(BUFFER_SIZE);
server.setSendBufferSize(BUFFER_SIZE);
} catch (Exception ex) {
ex.printStackTrace();
}

}


public void startServer(View v) {
v.setEnabled(false);
new Thread(new Runnable() {
@Override
public void run() {
OutputStream os;
InputStream is;
int i=0;
try {
is = server.getInputStream();
os = server.getOutputStream();
int len;
byte[] data;
while (true) {
data = new byte[1024];
len=is.read(data);
Log.e(TAG, "server:receive-size--" + len + " --- " + new String(data,0,len) + " ---");

byte[] writeBytes = ("Hello Client:" + i).getBytes();
os.write(writeBytes);
Log.e(TAG, "server:send-size--" + writeBytes.length + " --- " + new String(writeBytes) + " ---");
os.flush();
i++;
}

//不能关闭流,流关闭则套接字也关闭
// os.close();
// server.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();

}
public void clientSend(View v) {

if(running) return;
running=true;

new Thread(new Runnable() {
@Override
public void run() {
InputStream is;
OutputStream os;
int i=0;
try {
is = client.getInputStream();
os= client.getOutputStream();
byte[] data;
int len;

while (running) {
byte[] writeBytes=("LocalSocket:" + i).getBytes();
os.write(writeBytes);
Log.e(TAG, "client:send-size--" + writeBytes.length + " --- " + new String(writeBytes) + " ---");
os.flush();

i++;
data = new byte[1024];
len=is.read(data);
Log.e(TAG, "client:receive-size--" + len + " --- " + new String(data,0,len) + " ---");
Thread.sleep(1000);

}

// is.close();
// client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}

public void stopClient(View v) {
running = false;

}


}


参考资料

http://blog.csdn.net/xbalien29/article/details/8765235
http://blog.csdn.net/lizzy115/article/details/9108819