1.概述
用DatagramSocket与DatagramPacket实现UDP通信.UDP比TCP相对来说简单一点,不需要等待连接,且处理数据不需要用输出输出流,只需要DatagramPacket.严格来说,UDP没有服务端和客户端之分,只有发送端与接收端之分.
2.发送端
发送端首先建立DatagramSocket与DatagramPacket,设置好DatagramPacket的端口与ip,再通过DatagramSocket发送.
(1)创建DatagramSocket与DatagramPacket
DatagramSocket socket = new DatagramSocket(55555);
这里的55555端口是指数据通过这个端口发送.
(2)创建DatagramPacket
InetAddress ip = InetAddress.getByName("127.0.0.1");
int port = 12345;
String message = "message";
DatagramPacket packet = new DatagramPacket(message.getBytes(),message.getBytes().length,ip,port);
构造方法DatagramPacket()的四个参数分别为
- 发送的字节数组
- 字节数组长度
- ip
- 端口
这里端口不能与上面的一致,不然的话会提示端口被占用.
(3)发送
socket.send(packet);
3.接收端
接收端只需指定端口进行接收,这里的端口在DatagramSocket中的构造方法中指定,与发送端的发送的packet中设置的端口一致.
(1)创建DatagramSocket
DatagramSocket socket = new DatagramSocket(12345);
(2)创建DatagramPacket
这里创建的DatagramPacket只需要指定存储数据的字节数组与该字节数组的长度.
byte [] message = new byte[2048];
DatagramPacket packet = new DatagramPacket(message, message.length);
(3)接收
socket.receive(packet);
这个方法会阻塞,直到接收到packet.
(4)获取数据
接收到packet后可以使用getData()获取数据,返回字节数组,可以传递给String的构造方法创建String.
String str = new String(packet.getData());
4.完整代码
这里人为地设定了客户端与服务端,可以互相发送数据,服务端输入后发送到服务端,服务端返回固定数据,
"客户端"与"服务端"轮流扮演了发送端与接收端的角色.配合了swing使用.
(1)Server.java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.*;
public class Server
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Server");
JTextArea text = new JTextArea();
frame.add(text);
frame.setSize(600, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try
{
byte [] message = new byte[2048];
DatagramSocket socket = new DatagramSocket(12345);
DatagramPacket packet = new DatagramPacket(message, message.length);
socket.receive(packet);
InetAddress ip = packet.getAddress();
int port = packet.getPort();
text.setText("ip : "+ip.toString()+"\n");
text.append("port : "+port+"\n");
text.append("message : "+new String(packet.getData()));
String messageFromServer = "Message from server";
packet = new DatagramPacket(messageFromServer.getBytes(),messageFromServer.getBytes().length,ip,port);
socket.send(packet);
socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
(2)Client.java
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import javax.swing.*;
import java.awt.GridLayout;
public class Client
{
public static void main(String[] args) {
JFrame frame = new JFrame();
JTextArea text = new JTextArea();
JButton send = new JButton("send");
GridLayout layout = new GridLayout(2,1,1,1);
frame.setLayout(layout);
frame.setTitle("Client");
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setSize(600, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(text);
frame.add(send);
send.addActionListener( v->
{
try
{
InetAddress ip = InetAddress.getByName("127.0.0.1");
int port = 12345;
DatagramSocket socket = new DatagramSocket(55555);
String message = text.getText();
DatagramPacket packet = new DatagramPacket(message.getBytes(),message.getBytes().length,ip,port);
socket.send(packet);
byte [] messageFromServer = new byte[2048];
packet = new DatagramPacket(messageFromServer, messageFromServer.length);
socket.receive(packet);
text.setText(new String(packet.getData()));
socket.close();
}
catch (IOException e)
{
e.printStackTrace();
}
});
}
}