用DatagramSocket与DatagramPacket实现UDP通信.UDP比TCP相对来说简单一点,不需要等待连接,且处理数据不需要用输出输出流,只需要DatagramPacket.严格来说,UDP没有服务端和客户端之分,只有发送端与接收端之分.
发送端首先建立DatagramSocket与DatagramPacket,设置好DatagramPacket的端口与ip,再通过DatagramSocket发送.
DatagramSocket socket = new DatagramSocket(55555); 复制代码
这里的55555端口是指数据通过这个端口发送.
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()的四个参数分别为
这里端口不能与上面的一致,不然的话会提示端口被占用.
socket.send(packet); 复制代码
接收端只需指定端口进行接收,这里的端口在DatagramSocket中的构造方法中指定,与发送端的发送的packet中设置的端口一致.
DatagramSocket socket = new DatagramSocket(12345); 复制代码
这里创建的DatagramPacket只需要指定存储数据的字节数组与该字节数组的长度.
byte [] message = new byte[2048]; DatagramPacket packet = new DatagramPacket(message, message.length); 复制代码
socket.receive(packet); 复制代码
这个方法会阻塞,直到接收到packet.
接收到packet后可以使用getData()获取数据,返回字节数组,可以传递给String的构造方法创建String.
String str = new String(packet.getData()); 复制代码
这里人为地设定了客户端与服务端,可以互相发送数据,服务端输入后发送到服务端,服务端返回固定数据, "客户端"与"服务端"轮流扮演了发送端与接收端的角色.配合了swing使用.
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(); } } } 复制代码
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(); } }); } } 复制代码