Java UDP Socket

来源:互联网 发布:数据库漏洞扫描系统 编辑:程序博客网 时间:2024/05/19 13:44

本文转载地址:
       Java学习路上的收获:http://blog.csdn.net/qinpeng100423/article/details/8980423


[java] view plaincopyprint?
  1.   

2. UDP客户端程序


[java] view plaincopyprint?
  1. import java.io.*;    
  2. import java.net.*;    
  3.     
  4. /**  
  5.  * Copyright 2007 GuangZhou Cotel Co. Ltd.  
  6.  * All right reserved.      
  7.  * UDP客户端程序,用于对服务端发送数据,并接收服务端的回应信息.  
  8.  * @author QPING 
  9.  */    
  10. public class UdpClientSocket {    
  11.     private byte[] buffer = new byte[1024];    
  12.     
  13.     private DatagramSocket ds = null;    
  14.     
  15.     /**  
  16.      * 构造函数,创建UDP客户端  
  17.      * @throws Exception  
  18.      */    
  19.     public UdpClientSocket() throws Exception {    
  20.         ds = new DatagramSocket();    
  21.     }    
  22.         
  23.     /**  
  24.      * 设置超时时间,该方法必须在bind方法之后使用.  
  25.      * @param timeout 超时时间  
  26.      * @throws Exception  
  27.      */    
  28.     public final void setSoTimeout(final int timeout) throws Exception {    
  29.         ds.setSoTimeout(timeout);    
  30.     }    
  31.     
  32.     /**  
  33.      * 获得超时时间.  
  34.      * @return 返回超时时间  
  35.      * @throws Exception  
  36.      */    
  37.     public final int getSoTimeout() throws Exception {    
  38.         return ds.getSoTimeout();    
  39.     }    
  40.     
  41.     public final DatagramSocket getSocket() {    
  42.         return ds;    
  43.     }    
  44.     
  45.     /**  
  46.      * 向指定的服务端发送数据信息.  
  47.      * @param host 服务器主机地址  
  48.      * @param port 服务端端口  
  49.      * @param bytes 发送的数据信息  
  50.      * @return 返回构造后俄数据报  
  51.      * @throws IOException  
  52.      */    
  53.     public final DatagramPacket send(final String host, final int port,    
  54.             final byte[] bytes) throws IOException {    
  55.         DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress    
  56.                 .getByName(host), port);    
  57.         ds.send(dp);    
  58.         return dp;    
  59.     }    
  60.     
  61.     /**  
  62.      * 接收从指定的服务端发回的数据.  
  63.      * @param lhost 服务端主机  
  64.      * @param lport 服务端端口  
  65.      * @return 返回从指定的服务端发回的数据.  
  66.      * @throws Exception  
  67.      */    
  68.     public final String receive(final String lhost, final int lport)    
  69.             throws Exception {    
  70.         DatagramPacket dp = new DatagramPacket(buffer, buffer.length);    
  71.         ds.receive(dp);    
  72.         String info = new String(dp.getData(), 0, dp.getLength());    
  73.         return info;    
  74.     }    
  75.     
  76.     /**  
  77.      * 关闭udp连接.  
  78.      */    
  79.     public final void close() {    
  80.         try {    
  81.             ds.close();    
  82.         } catch (Exception ex) {    
  83.             ex.printStackTrace();    
  84.         }    
  85.     }    
  86.     
  87.     /**  
  88.      * 测试客户端发包和接收回应信息的方法.  
  89.      * @param args  
  90.      * @throws Exception  
  91.      */    
  92.     public static void main(String[] args) throws Exception {    
  93.         UdpClientSocket client = new UdpClientSocket();    
  94.         String serverHost = "127.0.0.1";    
  95.         int serverPort = 3344;    
  96.         client.send(serverHost, serverPort, ("你好,阿蜜果!").getBytes());    
  97.         String info = client.receive(serverHost, serverPort);    
  98.         System.out.println("服务端回应数据:" + info);    
  99.     }    
  100. }   

原文连接:http://sishuok.com/forum/blogPost/list/466.html


0 0
原创粉丝点击