Android作为客户端,PC端作为服务器端:实现服务器端(PC)发送广播消息,Android作为客户端,不断监听服务器端指定的端口发来的消息,从而实现对Android客户端应用的控制。下面对代码说明如下,服务器端:
1、SendIP.java
public class SendIP {
public static void main(String args[]) {
// new SendIP().lanchApp();
new Thread() {
public void run() {
while (true) {
try {
DatagramSocket dgSocket = new DatagramSocket();
System.out.println("请输入您要发送的信息:");
Scanner mScanner=new Scanner(System.in);
String mString=mScanner.nextLine();
byte b[] = mString.getBytes();
DatagramPacket dgPacket = new DatagramPacket(b,
b.length,
InetAddress.getByName("192.168.48.255"), 12345);
dgSocket.send(dgPacket);
dgSocket.close();
System.out.println("send message is ok.");
} catch (Exception e) {
e.printStackTrace();
}
}
};
}.start();
}
}
上述代码主要是在Java主函数main函数中开启了一个子线程,并在该子线程中通过不断循环的方式从而可以实现随时发送广播信息;
2、关于UDP广播代码的说明:
- 通过DatagramSocket类创建socket:DatagramSocket dgSocket = new DatagramSocket();
- 创建字节数组用于存放要发送的内容:byte b[] = mString.getBytes();
- 将数据封装到要发送的数据包中,同时制定广播地址及端口DatagramPacket dgPacket = new DatagramPacket(b, b.length,InetAddress.getByName(“192.168.48.255”), 12345);
- 然后调用socket的send方法将数据包发送出去:dgSocket.send(dgPacket);
3、接收端Android端:首先在MainActivity中打开service,之后service运行在后台不断监听来自服务器端的消息,收到消息后执行相应控制操作:
MainActivity.java
public class MainActivity extends Activity {
//private GetStreamBroadcaseReceiver mGetStreamBroadcaseReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
Intent intent=new Intent(this,UdpReceiverService.class);
Log.v("WANGRUI", "准备启动service...............");
startService(intent);
Log.v("WANGRUI", "开启service...............");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.v("WANGRUI", "stopservice 被调用。。。。。。。。。");
stopService(new Intent("com.reciver1.rc"));
}
}
UdpReceiverService.java
public class UdpReceiverService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread() {
public void run() {
while (true) {
/*try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
try {
DatagramSocket dgSocket =null;
int port = 12345;
if(dgSocket==null){
dgSocket = new DatagramSocket(null);
dgSocket.setReuseAddress(true);
dgSocket.bind(new InetSocketAddress(port));
}
byte[] by = new byte[1024];
DatagramPacket packet = new DatagramPacket(by,by.length);
dgSocket.receive(packet);
String str = new String(packet.getData(), 0,packet.getLength());
System.out.println("接收到的数据为:" + str);
Log.v("WANGRUI", "已获取服务器端发过来的数据。。。。。"+str);
}.start();
return super.onStartCommand(intent, flags, startId);
//return 0;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
对于接收端这里需要注意一下几点:
- 创建socket时需做判断,否则会报未bind异常:
DatagramSocket dgSocket =null;
int port = 12345;
if(dgSocket==null){
dgSocket = new DatagramSocket(null);
dgSocket.setReuseAddress(true);
dgSocket.bind(new InetSocketAddress(port));
} - 创建字节数组用于接收服务器端发来的消息:byte[] by = new byte[1024];
DatagramPacket packet = new DatagramPacket(by,by.length); - 开始数据接收:dgSocket.receive(packet);
- 对于接收服务器端的广播信息而言,一般并不需要指定IP地址,指定相应的服务器端的广播端口即可;