---
title: "Java数据报(UDP)编程"
author: "mmdev"
date: "2006-10-13T15:40:00Z"
tags: []
slug: "java-udp"
draft: false
lastmod: "2012-03-28T22:24:03Z"
id: 1467901
url: "https://mmdev.iteye.com/blog/1467901"
---

**一般说明**

在TCP/IP协议族中,UDP和TCP同样位于传输层,用户数据报是UDP协议中的概念.

UDP协议提供面向事务的简单不可靠信息传送服务,它不提供对 IP 协议的可靠机制、流控制以及错误恢复功能.

UDP 协议基本上是IP 协议与上层协议的接口,从整个用户数据在各层的包装看,UDP报文格式相当简单:

**16** **32bit** Source port源端口 Destination port目标端口 Length 报文长度(单位是字节,包括首部和用户数据区) Checksum(校验和) Data

由于校验和的原因,UDP还引入了伪首部,这导致了UDP和IP层的关系过于密切,破坏了分层原则.

**Java数据报支持**

包java.net中提供了两个类DatagramSocket和DatagramPacket用来支持数据报通信，DatagramSocket用于在程序之间建立传送数据报的通信连接， DatagramPacket则用来表示一个数据报。

DatagramSocket代表发送和接收数据报的套接字,一个数据报套接字是为包递送服务的发送和接收点,在一个数据报套接字上,每个被发送和接收的包都被独立的寻址和路由,从一台机器到另一台机器上发送的多个包有不同的路由,任意的抵达顺序.

对于DatagramSocket,UDP广播发送总是使能的(那是缺省设置).为了接收广播包这个类实例应该绑定到通用地址(wildcard address).在某些实现中,当被绑定到更多特定地址上的时候广播包也可以接收.

例如:

DatagramSocket s = new DatagramSocket(null);  
s.bind(new InetSocketAddress(8888));  
这等同于:  
DatagramSocket s = new DatagramSocket(8888);  
两种情况都会创建一个能在端口8888上接收广播的DatagramSocket实例.

&lt;!-- ======== NESTED CLASS SUMMARY ======== --&gt;&lt;!-- =========== FIELD SUMMARY =========== --&gt;&lt;!-- ======== CONSTRUCTOR SUMMARY ======== --&gt;[&lt;!-- --&gt;]()

**Constructor Summary** `DatagramSocket()`  
Constructs a datagram socket and binds it to any available port on the local host machine. `protected` `DatagramSocket(DatagramSocketImplimpl)`  
Creates an unbound datagram socket with the specified DatagramSocketImpl. `DatagramSocket(intport)`  
Constructs a datagram socket and binds it to the specified port on the local host machine. `DatagramSocket(intport, InetAddressladdr)`  
Creates a datagram socket, bound to the specified local address. `DatagramSocket(SocketAddressbindaddr)`  
Creates a datagram socket, bound to the specified local socket address.

其中，port指明socket所使用的端口号，如果未指明端口号，则把socket连接到本地主机上一个可用的端口。laddr指明一个可用的本地地址。给出端口号时要保证不发生端口冲突，否则会生成SocketException类例外。

用数据报方式编写通信程序时，通信双方，首先都要建立一个DatagramSocket对象，用来接收或发送数据报，然后使用DatagramPacket类对象作为传输数据的载体。下面看一下DatagramPacket的构造方法 ：

**Constructor Summary** `DatagramPacket(byte[]buf, intlength)`  
Constructs a `DatagramPacket` for receiving packets of length `length`. `DatagramPacket(byte[]buf, intlength, InetAddressaddress, intport)`  
Constructs a datagram packet for sending packets of length `length` to the specified port number on the specified host. `DatagramPacket(byte[]buf, intoffset, intlength)`  
Constructs a `DatagramPacket` for receiving packets of length `length`, specifying an offset into the buffer. `DatagramPacket(byte[]buf, intoffset, intlength, InetAddressaddress, intport)`  
Constructs a datagram packet for sending packets of length `length` with offset `ioffset`to the specified port number on the specified host. `DatagramPacket(byte[]buf, intoffset, intlength, SocketAddressaddress)`  
Constructs a datagram packet for sending packets of length `length` with offset `ioffset`to the specified port number on the specified host. `DatagramPacket(byte[]buf, intlength, SocketAddressaddress)`  
Constructs a datagram packet for sending packets of length `length` to the specified port number on the specified host.

可以看出,有两个供接收的构造器和四个供发送的构造器.其中，buf中存放数据报数据，length为数据报中数据的长度，address和port旨明目的地址，offset指明了数据报的位移量。

**Java组播支持**

MulticastSocket 多播数据报套接字。这个组播套接字对于收发IP多播数据包是很有用的，它扩展了DatagramSocket，在其上附加了加入internet上多播组的方法。一个多播组由D类IP地址和标准UDP端口指定，D类IP范围是224.0.0.0 to `239.255.255.255，其中224.0.0.0被保留不为它用。`

`它有三个构造器：`

**Constructor Summary** `MulticastSocket()`  
Create a multicast socket. `MulticastSocket(intport)`  
Create a multicast socket and bind it to a specific port. `MulticastSocket(SocketAddressbindaddr)`  
Create a MulticastSocket bound to the specified socket address.

基本上，没有指定端口，只为发送，指定端口可收发，多址主机会用套接字地址。

一，DatagramSocket类；DatagramPacket类；InetAddress类

构造函数 public DatagramSocket();  
public DatagramSocket(int port);  
public DatagramSocket(InetAddress laddr);

close()方法

send(DatagramPacket p)

receive(DatagramPacket p)

接受方的**DatagramPacket** public **DatagramPacket**(byte\[]buf, intlength)  
发送方的**DatagramPacket**public **DatagramPacket**(byte\[]buf, intlength, InetAddressaddress, intport

最简单的UDP程序:

发送程序：UdpSend.java

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.net.\*;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)publicclassUdpSend  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticvoidmain(String\[]args)throwsException  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)DatagramSocketds=newDatagramSocket();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Stringstr="helloworld";  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)DatagramPacketdp=newDatagramPacket(str.getBytes(),str.length(),  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)InetAddress.getByName("192.168.0.25"),3000);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ds.send(dp);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ds.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

接收程序:UdpRecv.java

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)importjava.net.\*;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)publicclassUdpRecv  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)publicstaticvoidmain(String\[]args)throwsException  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)DatagramSocketds=newDatagramSocket(3000);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)byte\[]buf=newbyte\[1024];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)DatagramPacketdp=newDatagramPacket(buf,1024);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ds.receive(dp);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)StringstrRecv=newString(dp.getData(),0,dp.getLength())+  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)"from"+dp.getAddress().getHostAddress()+":"+dp.getPort();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)System.out.println(strRecv);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ds.close();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}