UDP示例

android学习笔记18--------------UDP示例

分类: android

参考别人一幅原理图。

 

 

  1. import java.io.IOException;  
  2. import java.net.DatagramPacket;  
  3. import java.net.DatagramSocket;  
  4. import java.net.InetAddress;  
  5.   
  6. public class UDPClient  
  7. {  
  8.     private DatagramSocket client=null;  
  9.     private DatagramPacket packet=null;  
  10.     private InetAddress host=null;  
  11.     public static int portSend=8888;  
  12.     public static String name="192.168.142.199";  
  13.       
  14.       
  15.     public UDPClient()  
  16.     {         
  17.         try  
  18.         {  
  19.             host = InetAddress.getByName(name);  
  20.             client=new DatagramSocket();  
  21.                                           
  22.             System.out.println("客户端准备就绪");  
  23.         } catch (Exception e)  
  24.         {  
  25.             e.printStackTrace();  
  26.         }                             
  27.     }  
  28.       
  29.     public void UDPClientRun(byte[] buffer) throws IOException    
  30.     {                                 
  31.         host = InetAddress.getByName(name);  
  32.         packet=new DatagramPacket(buffer,buffer.length, host, portSend);  
  33.         client.send(packet);      
  34.         System.out.println("客户端发送消息");  
  35.     }  
  36.       
  37.     public void UDPClientStop()  
  38.     {  
  39.         client.close();  
  40.     }  
  41. }  


 

  1. import java.io.IOException;  
  2. import java.net.DatagramPacket;  
  3. import java.net.DatagramSocket;  
  4.   
  5. public class UDPService  
  6. {  
  7.     private DatagramSocket server =null;  
  8.     private DatagramPacket dPacket=null;  
  9.     private byte[] buffer = new byte[640];    
  10.     public static int portServer=8888;  
  11.       
  12.     public UDPService ()  
  13.     {  
  14.         try  
  15.         {  
  16.             server  = new DatagramSocket(portServer);     
  17.             dPacket = new DatagramPacket(buffer, buffer.length);  
  18.         } catch (Exception e)  
  19.         {  
  20.             e.printStackTrace();  
  21.         }             
  22.     }  
  23.       
  24.     public byte[] UDPServiceRun()  
  25.     {                                                                                             
  26.         try  
  27.         {  
  28.             server.receive(dPacket);  
  29.             System.out.println("服务端接收到消息");  
  30.         } catch (IOException e)  
  31.         {  
  32.             e.printStackTrace();  
  33.             System.out.println("服务端未收到消息");  
  34.         }                             
  35.         return buffer;                        
  36.     }  
  37.       
  38.     public void UDPSeverStop()  
  39.     {  
  40.         server.close();  
  41.     }  
  42.       
  43.     public String getClientName()  
  44.     {  
  45.         String name="";  
  46.         name=dPacket.getAddress().toString();  
  47.         return name;  
  48.     }  
  49.       
  50. }  

posted on 2015-10-28 16:53  善思善学  阅读(177)  评论(0编辑  收藏  举报

导航