[关闭]
@Yano 2016-01-03T15:52:08.000000Z 字数 2391 阅读 10832

Android UDP简单实现

Android


说明

测试程序分为两个部分:Android客户端、Java服务端。

Android客户端

  1. public class MainActivity extends Activity {
  2. // UDP广播IP和PORT
  3. public static final String SERVERIP = "255.255.255.255";
  4. public static final int SERVERPORT = 11000;
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. new Thread(new Client()).start();
  10. }
  11. DatagramSocket socket = null;
  12. public class Client implements Runnable {
  13. public void run() {
  14. // 向局域网UDP广播信息:Hello, World!
  15. try {
  16. InetAddress serverAddress = InetAddress.getByName(SERVERIP);
  17. System.out.println("Client: Start connecting\n");
  18. socket = new DatagramSocket(SERVERPORT);
  19. byte[] buf = "Hello, World!".getBytes();
  20. DatagramPacket packet = new DatagramPacket(buf, buf.length,
  21. serverAddress, SERVERPORT);
  22. System.out.println("Client: Sending ‘" + new String(buf)
  23. + "’\n");
  24. socket.send(packet);
  25. System.out.println("Client: Message sent\n");
  26. System.out.println("Client: Succeed!\n");
  27. } catch (UnknownHostException e) {
  28. e.printStackTrace();
  29. } catch (SocketException e) {
  30. e.printStackTrace();
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. // 接收UDP广播,有的手机不支持
  35. while (true) {
  36. byte[] recbuf = new byte[255];
  37. DatagramPacket recpacket = new DatagramPacket(recbuf,
  38. recbuf.length);
  39. try {
  40. socket.receive(recpacket);
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. System.out.println("Server: Message received: ‘"
  45. + new String(recpacket.getData()) + "’\n");
  46. System.out.println("Server: IP " + recpacket.getAddress()
  47. + "’\n");
  48. }
  49. }
  50. }
  51. }

AndroidManifest中添加Internet权限

  1. <uses-permission android:name="android.permission.INTERNET" />

Java服务端

  1. class UDPServer {
  2. public static void main(String[] args) throws IOException {
  3. DatagramSocket server = new DatagramSocket(11000);
  4. while (true) {
  5. byte[] recvBuf = new byte[100];
  6. DatagramPacket recvPacket = new DatagramPacket(recvBuf,
  7. recvBuf.length);
  8. server.receive(recvPacket);
  9. String recvStr = new String(recvPacket.getData(), 0,
  10. recvPacket.getLength());
  11. System.out.println();
  12. System.out.println("收到的内容:" + recvStr);
  13. System.out.println("收到的ip:" + recvPacket.getAddress());
  14. // 发送UDP消息,JSON格式数据
  15. // {"name":"123","ID":"Robot007","BATTERY":"2.7f"}
  16. int port = recvPacket.getPort();
  17. InetAddress addr = recvPacket.getAddress();
  18. JSONObject json = new JSONObject();
  19. json.put("name", "Computer7");
  20. json.put("ID", "Robot007");
  21. json.put("BATTERY", "2.7f");
  22. byte[] sendBuf = json.toString().getBytes();
  23. DatagramPacket sendPacket = new DatagramPacket(sendBuf,
  24. sendBuf.length, addr, port);
  25. server.send(sendPacket);
  26. }
  27. }
  28. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注