UDP通信

UDP通信,发送端发送数据,接受端接收数据,发送端发送数据不需要知道客户端接受与否的方式,比TCP通信的方式简单很多,下面是我的写的一个小例子

UDP的发送端:

/**
 * UdpServer.java	  V1.0   2012-5-9 下午4:58:54
 *
 * Copyright Lizhenbin Co. ,Ltd. All rights reserved.
 *
 * Modification history(By    Time    Reason):
 * 
 * Description:
 */

package com.lzb.common.socket;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

public class UdpSender {
	
	private static UdpSender instance = null;
	public UdpSender() {};
	public static synchronized UdpSender getInstance() {
		if(instance == null)
			instance = new UdpSender();
		return instance;
	}
	
	/**
	 * 
	 * 功能描述:UDP发送数据报文,这里只发送字符串的形式,每次发送不超过1024个字节
	 *
	 * @author  lizhenbin
	 * <p>创建日期 :2012-5-9 下午5:26:17</p>
	 *
	 * @param msg 发送的内容
	 * @param iphost 发送的目标ip地址
	 * @param port 发送的端口
	 * @return 成功:ture, 失败:false;
	 *
	 * <p>修改历史 :(修改人,修改时间,修改原因/内容)</p>
	 */
	public boolean send(String msg, String iphost, String port) {
		
		if(msg==null || msg.equals("")) {
			System.out.println("Usage: UDP Sender Message is Null.");
			return false;
		}
		if(iphost==null || iphost.equals("")) {
			System.out.println("Usage: UDP Sender IP host is Null.");
			return false;
		}
		if(port==null || port.equals("")) {
			System.out.println("Usage: UDP Sender Listener Port is Null.");
			return false;
		}
		
		DatagramSocket dsocket = null;
		try {
			
			dsocket = new DatagramSocket();
			
			byte[] bytes = new byte[1024];
			bytes = msg.getBytes();
			int msgLength = msg.getBytes().length;
			InetAddress senderIp = InetAddress.getByName(iphost);
			DatagramPacket dpacket = new DatagramPacket(bytes, msgLength, senderIp, Integer.valueOf(port));
			
			dsocket.send(dpacket);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			dsocket.close();
		}
	}
//	public static void main(String[] args) {
//		
//		UdpSender sender = UdpSender.getInstance();
//		String msg = "Hello World.";
//		String iphost = "127.0.0.1";
//		String port = "3000";
//		sender.send(msg, iphost, port);
//	}
}

 UDP的接收端:

/**
 * UdpReceiver.java	  V1.0   2012-5-9 下午5:24:02
 *
 * Copyright Lizhenbin Co. ,Ltd. All rights reserved.
 *
 * Modification history(By    Time    Reason):
 * 
 * Description:
 */

package com.lzb.common.socket;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class UdpReceiver {

	private static UdpReceiver instance = null;
	public UdpReceiver() {};
	public static synchronized UdpReceiver getInstance() {
		if(instance == null)
			instance = new UdpReceiver();
		return instance;
	}
	
	/**
	 * 
	 * 功能描述:UDP接收端
	 *
	 * @author  lizhenbin
	 * <p>创建日期 :2012-5-9 下午5:38:16</p>
	 *
	 * @param port 端口号
	 * @return 数据包信息(DatagramPacket)
	 *
	 * <p>修改历史 :(修改人,修改时间,修改原因/内容)</p>
	 */
	public DatagramPacket receive(String port) {
		
		if(port==null || port.equals("")) {
			System.out.println("Usage: UDP Receiver Listener Port is Null.");
			return null;
		}
		
		DatagramSocket dsocket = null;
		try {
			
			dsocket = new DatagramSocket(Integer.valueOf(port));
			// 设置每次接受的最大字节数
			byte[] msgBytes = new byte[1024];
			DatagramPacket dpacket = new DatagramPacket(msgBytes, 1024);
			
			dsocket.receive(dpacket);
			return dpacket;
		} catch (Exception e) {
			
			e.printStackTrace();
			return null;
		} finally {
			dsocket.close();
		}
	}
	
//	public static void main(String[] args) {
//		
//		UdpReceiver receiver = UdpReceiver.getInstance();
//		DatagramPacket dpacket = receiver.receive("3000");
//		
//		String msg = new String(dpacket.getData(), 0, dpacket.getLength());
//		String ipHost = dpacket.getAddress().getHostName();
//		String rport = String.valueOf(dpacket.getPort());
//		System.out.println("--->Receive Msg: " + msg + " From IP Host: " + ipHost + " Port: " + rport);
//
//	}
}

 

 

你可能感兴趣的:(UDP)