/*
* Copyright (C) 2016 即时通讯网(52im.net) - 即时通讯开发者社区.
* All rights reserved.
*/
package
net.x52im.example.android.udp;
import
java.net.DatagramPacket;
import
java.net.DatagramSocket;
import
net.x52im.example.android.udp.utils.ConfigEntity;
import
android.content.Context;
import
android.util.Log;
/**
* 本地UDP端口监听和数据接收类。
*
* @author jack.jiang@52im.net, 2016-06-27
* @version 1.0
*/
public
class
LocalUDPDataReciever
{
private
static
final
String TAG = LocalUDPDataReciever.
class
.getSimpleName();
private
static
LocalUDPDataReciever instance =
null
;
private
Thread thread =
null
;
private
Context context =
null
;
public
static
LocalUDPDataReciever getInstance(Context context)
{
if
(instance ==
null
)
instance =
new
LocalUDPDataReciever(context);
return
instance;
}
private
LocalUDPDataReciever(Context context)
{
this
.context = context;
}
public
void
startup()
{
this
.thread =
new
Thread(
new
Runnable()
{
public
void
run()
{
try
{
Log.d(LocalUDPDataReciever.TAG,
"本地UDP端口侦听中,端口="
+ ConfigEntity.localUDPPort +
"..."
);
//开始侦听
LocalUDPDataReciever.
this
.udpListeningImpl();
}
catch
(Exception eee)
{
Log.w(LocalUDPDataReciever.TAG,
"本地UDP监听停止了(socket被关闭了?),"
+ eee.getMessage(), eee);
}
}
});
this
.thread.start();
}
private
void
udpListeningImpl()
throws
Exception
{
while
(
true
)
{
byte
[] data =
new
byte
[
1024
];
// 接收数据报的包
DatagramPacket packet =
new
DatagramPacket(data, data.length);
DatagramSocket localUDPSocket = LocalUDPSocketProvider.getInstance().getLocalUDPSocket();
if
((localUDPSocket ==
null
) || (localUDPSocket.isClosed()))
continue
;
// 阻塞直到收到数据
localUDPSocket.receive(packet);
// 解析服务端发过来的数据
String pFromServer =
new
String(packet.getData(),
0
, packet.getLength(),
"UTF-8"
);
Log.w(LocalUDPDataReciever.TAG,
"【NOTE】>>>>>> 收到服务端的消息:"
+pFromServer);
}
}
}