forked from GuoJinyu/AndroidUtils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUdpMulticastSocket.java
139 lines (130 loc) · 3.83 KB
/
UdpMulticastSocket.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.acker.android;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.SocketException;
import java.net.UnknownHostException;
public class UdpMulticastSocket {
private MulticastSocket mSocket;
private DatagramPacket recvPacket;
private InetAddress groupIp;
private int port;
/**
* Constructs a multicast socket, bound to the specified port on the local
* host.
*
* @param multiAddr specify the multicast group.
* @param port the port to bind on the localhost.
*/
public UdpMulticastSocket(String multiAddr, int port) {
super();
try {
this.groupIp = InetAddress.getByName(multiAddr);
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
this.port = port;
try {
mSocket = new MulticastSocket(port);
mSocket.joinGroup(groupIp);
} catch (IOException e) {
e.printStackTrace();
}
try {
mSocket.setLoopbackMode(true);
} catch (SocketException e) {
e.printStackTrace();
}
}
/**
* Constructs a multicast socket, bound to the specified port on the local
* host with socket buffer set.
*
* @param multiAddr specify the multicast group.
* @param port the port to bind on the localhost.
* @param size this socket's send and recv buffer size.
*/
public UdpMulticastSocket(String multiAddr, int port, int size) {
super();
try {
this.groupIp = InetAddress.getByName(multiAddr);
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
this.port = port;
try {
mSocket = new MulticastSocket(port);
mSocket.setSendBufferSize(size);
mSocket.setReceiveBufferSize(size);
mSocket.joinGroup(groupIp);
} catch (IOException e) {
e.printStackTrace();
}
try {
mSocket.setLoopbackMode(true);
} catch (SocketException e) {
e.printStackTrace();
}
}
/**
* Sends data over this socket.
*
* @param data data to be sent.
*/
public void udpSend(byte[] data) {
DatagramPacket packet = new DatagramPacket(data, data.length, groupIp,
port);
try {
mSocket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Wating for data receiving.
*
* @param length max packet size you want to receive from UDP socket.
* @return receive buffer.
*/
public byte[] udpRecv(int length) {
try {
recvPacket = new DatagramPacket(new byte[length], length);
mSocket.receive(recvPacket);
int len = recvPacket.getLength();
byte[] mData = new byte[len];
System.arraycopy(recvPacket.getData(), 0, mData, 0, len);
return mData;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* Gets the sender or destination IP address of this datagram packet.
*
* @return UDP packet source address.
*/
public String getUdpSrcAdd() {
if (recvPacket != null) {
String address = recvPacket.getAddress().toString();
String[] temp = address.split("/");
return temp[1];
} else {
return null;
}
}
/**
* For multicast UDP finalization.
*/
public void udpEnd() {
if (mSocket != null) {
try {
mSocket.leaveGroup(groupIp);
} catch (IOException e) {
e.printStackTrace();
}
mSocket.close();
}
}
}