UDP程序设计基础
UDP与TCP不同,基于UDP的信息传递更快,但是使用UDP传递数据的时候,用户无法知道数据能否正确的到达主机,也并不能确定到达的目的地的顺序是否和发送的顺序相同,但是他快啊!!
基本模式
-
将数据打包(成为数据包),然后然后将数据发往目的地。
-
接受别人发来的数据包,然后查看数据包
程序步骤 -
使用DatagramSocket()创建一个数据报套接字
-
使用DatagramPacket(byte[ ] buf,int offset , int length , InterAddress address , int port ),创建要发送的数据包
-
使用DatagramSocket类的send方法发送数据包
-
使用DatagramSocket( int port )创建数据报套接字,绑定到指定的端口
-
使用DatagramPacket(byte[ ] buf,int offset , int length)创建字节数组来接收数据包
-
使用DatagramPacket类的receive()方法接受UDP包
DatagramPacket类
用来表示数据包
DatagramPacket(byte[ ] buf , int length )//指定数据包的内存空间和大小
DatagramPacket(byte[ ] buf , int length , InterAddress address , int port)//指定数据包的内存空间和大小还制定了数据包的目的地址和端口。在发送数据时,必须指定接收方的Socket地址和端口号,因此使用这种构造函数可以创建发送数据的DataPacket对象。
DatagramSocket类
用于表示发送和接受数据包的套接字。
DatagramSocket()//构造数据报套接字并将其绑定到本地主机上任意可用的端口
DatagramSocket( int port )///创建数据报套接字并将其绑定到本地主机上指定的端口
DatagramSocket( int port , InterAddress addr)
//创建数据报套接字并将其绑定到指定的本地地址,适用于有多个网卡和多个IP地址的情况
例题
import java.net.*;
public class Weather extends Thread {
String weather = "josephous handsome";
int port = 9898;
InetAddress iaddress = null;
MulticastSocket socket = null;
Weather() {
try {
iaddress = InetAddress.getByName("224.255.10.0");
socket = new MulticastSocket(port);
socket.setTimeToLive(1);
socket.joinGroup(iaddress);
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
while (true) {
DatagramPacket packet = null;
byte data[] = weather.getBytes();
packet = new DatagramPacket(data, data.length, iaddress, port);
System.out.println(new String(data));
try {
socket.send(packet);
sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Weather w = new Weather();
w.start();
}
}
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
public class Receive extends JFrame implements Runnable, ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
int port;
InetAddress group = null;
MulticastSocket socket = null;
JButton ince = new JButton("开始接收");
JButton stop = new JButton("停止接收");
JTextArea inceAr = new JTextArea(10, 10);
JTextArea inced = new JTextArea(10, 10);
Thread thread;
boolean b = false;
public Receive() {
super("广播数据报");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
thread = new Thread(this);
ince.addActionListener(this);
stop.addActionListener(this);
inceAr.setForeground(Color.blue);
JPanel north = new JPanel();
north.add(ince);
north.add(stop);
add(north, BorderLayout.NORTH);
JPanel center = new JPanel();
center.setLayout(new GridLayout(1, 2));
center.add(inceAr);
final JScrollPane scrollPane = new JScrollPane();
center.add(scrollPane);
scrollPane.setViewportView(inced);
add(center, BorderLayout.CENTER);
validate();
port = 9898;
try {
group = InetAddress.getByName("224.255.10.0");
socket = new MulticastSocket(port);
socket.joinGroup(group);
} catch (Exception e) {
e.printStackTrace();
}
setBounds(100, 50, 360, 380);
setVisible(true);
}
public void run() {
while (true) {
byte data[] = new byte[1024];
DatagramPacket packet = null;
packet = new DatagramPacket(data, data.length, group, port);
try {
socket.receive(packet);
String message = new String(packet.getData(), 0, packet
.getLength());
inceAr.setText("正在接收的内容:\n" + message);
inced.append(message + "\n");
} catch (Exception e) {
e.printStackTrace();
}
if (b == true) {
break;
}
}
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == ince) {
ince.setBackground(Color.red);
stop.setBackground(Color.yellow);
if (!(thread.isAlive())) {
thread = new Thread(this);
}
thread.start();
b = false;
}
if (e.getSource() == stop) {
ince.setBackground(Color.blue);
stop.setBackground(Color.red);
b = true;
}
}
public static void main(String[] args) {
Receive rec = new Receive();
rec.setSize(460, 200);
}
}