如何实现基于UDP的socket的通信

private static final String ServerIP = "192.168.173.1" ;
        private static final int ServerPort = 4568;
在按钮点击事件中写入new Thread( new Server()).start(); 
                                       try { 
                                      Thread.sleep(500); 
                                  } catch (InterruptedException e) { 

                                         } 
                                         new Thread( new Client()).start(); 
其中client和server的类定义如下:
public class Client implements Runnable { 
                     @Override 
                     public void run() { 
                          /*while (start == false) { 
                         }  */
                   try { 
                              Thread.sleep(500); 
                          } catch (InterruptedException e1) { 
                             // TODO Auto-generated catch block 
                             e1.printStackTrace(); 
                         } 
                         try { 
                             InetAddress serverAddr = InetAddress.getByName(ServerIP ); 
                           /*  updatetrack ("Client: Start connecting\n");  */
                             DatagramSocket socket = new DatagramSocket(); 
                             byte[] buf;
                                 buf = ( "abc").getBytes(); 
                            DatagramPacket packet = new DatagramPacket(buf, buf.length, 
                                     serverAddr, ServerPort); 
                            /* updatetrack ("Client: Sending ‘" + new String(buf ) + "’\n");  */
                              socket.send(packet); 
                            /* updatetrack ("Client: Message sent\n"); 
                              updatetrack("Client: Succeed!\n"); */
                          } catch (Exception e) { 
                             /* updatetrack ("Client: Error!\n");  */
                          } 
                      } 
                  } 
                 public class Server implements Runnable { 
                     @Override 
              public void run() { 
                         /*while (start == false) { 
                        }  */
                        try { 
                            InetAddress serverAddr = InetAddress.getByName(ServerIP ); 
                             /*updatetrack ("\nServer: Start connecting\n");  */
                             DatagramSocket socket = new DatagramSocket(ServerPort , 
                                     serverAddr); 
                            byte[] buf = new byte[17]; 
                           DatagramPacket packet = new DatagramPacket(buf, buf.length); 
                           /* updatetrack ("Server: Receiving\n");  */
                              socket.receive(packet); 
                            /* updatetrack ("Server: Message received: ‘" 
                                      + new String(packet.getData()) + "’\n"); 
                             updatetrack("Server: Succeed!\n");  */
                         } catch (Exception e) { 
                           /* updatetrack ("Server: Error!\n");  */
                         } 
                    } 
                } 

注意这里client是发送的,server是接收的 activity如何返回数据

你可能感兴趣的:(如何实现基于UDP的socket的通信)