1.发送
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.net.SocketException; public class UDPSender { public static void main(String[] args){ //1.创建要用来发送的本地地址对象 SocketAddress localAddr = new InetSocketAddress("localhost", 13000); //2.创建发送的Socket对象 try { DatagramSocket sender = new DatagramSocket(localAddr); String text = new String("今天星期天"); byte[] buffer = text.getBytes(); //3.发送数据的目标地址和端口 SocketAddress dstAddr = new InetSocketAddress("localhost",14000); //4.创建要发送的数据包,指定内容,指定目标地址 DatagramPacket pack = new DatagramPacket(buffer, buffer.length, dstAddr); //5.发送 sender.send(pack); System.out.println("已发送"); } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
2.接收
import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetSocketAddress; import java.net.SocketAddress; import java.net.SocketException; public class UDPReceiver { public static void main(String[] args){ //1.创建要用来发送的本地地址对象 SocketAddress localAddr = new InetSocketAddress("localhost", 14000); //2.创建接受的服务器对象 try { DatagramSocket receiver = new DatagramSocket(localAddr); //3.指定接收缓冲区,buffer太小将只能接受部分消息 byte[] buffer = new byte[100]; //4.创建接受数据包对象,指定接受大小 DatagramPacket pack = new DatagramPacket(buffer, buffer.length); System.out.println("正在等待数据"); //5.堵塞等待数据来临,如果收到数据,存入pack中 receiver.receive(pack); SocketAddress sendAddr = pack.getSocketAddress(); System.out.println("发送方的SocketAddr:"+sendAddr); String msg = new String(pack.getData()).trim(); System.out.println("接受到的数据:"+msg); } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
3.组播群聊
import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.net.DatagramPacket; import java.net.InetAddress; import java.net.MulticastSocket; import java.net.UnknownHostException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class MulticastClientFrame extends JFrame implements Runnable,ActionListener{ private static final long serialVersionUID = 1L; private int port = 9999; private String ipAddr = "230.0.0.1";//组播IP地址 private InetAddress dstAddress;//组播消息的目标地址 private MulticastSocket sender;//本机组播发送端 private JTextArea receiveArea;//接受数据显示框 private JTextField nameField;//姓名框 private JTextField inputField;//消息框 JButton sendBu;//发送 public MulticastClientFrame(){ this.setTitle("组播"); this.setSize(480, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); try { dstAddress = InetAddress.getByName(ipAddr); sender = new MulticastSocket(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } this.createUI(); this.setVisible(true); //开启线程读取数据 new Thread(this).start(); } public void createUI(){ this.setLayout(new FlowLayout(FlowLayout.LEFT)); JLabel msgLabel = new JLabel("接受的消息:"); receiveArea = new JTextArea(10,40); receiveArea.setLineWrap(true); JScrollPane recvScrollPane = new JScrollPane(receiveArea); JLabel nameLabel = new JLabel("姓名"); nameField = new JTextField(5); JLabel sendLabel = new JLabel("发送消息:"); inputField = new JTextField(20); sendBu = new JButton("发送"); sendBu.addActionListener(this); this.add(msgLabel); this.add(recvScrollPane); this.add(nameLabel); this.add(nameField); this.add(sendLabel); this.add(inputField); this.add(sendBu); } public void run(){ MulticastSocket receiver; try { receiver = new MulticastSocket(port); receiver.joinGroup(dstAddress); while(true){ byte[] buffer = new byte[100]; DatagramPacket pack = new DatagramPacket(buffer, buffer.length); receiver.receive(pack); String msg = new String(buffer).trim() + "\n"; receiveArea.append(msg); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void sendMsg(String msg){ byte[] data = msg.getBytes(); DatagramPacket pack = new DatagramPacket(data, data.length, dstAddress, port); try { sender.send(pack); } catch (IOException e) { e.printStackTrace(); } } public void actionPerformed(ActionEvent e){ if(e.getSource() == sendBu){ String msg = nameField.getText()+":"+inputField.getText(); sendMsg(msg); } } public static void main(String[] args){ new MulticastClientFrame(); } }