1.Server.java
- package onlyfun.gray.sever;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import java.net.*;
- import java.util.*;
- import javax.swing.*;
- public class Server extends JFrame {
- JButton startButton; //开始按钮
- JButton sendButton; //发送按钮
- JButton exitButton; //退出按钮
- JTextArea inputTa; //输入文本域
- JTextArea showTa; //显示文本域
- JTextField addrTf; //D类地址输入文本框
- //设置Frame的大小
- private int x = 300;
- private int y = 250;
- private int width = 450;
- private int height = 500;
- //构造函数
- public Server(String title) {
- this.setTitle(title);
- this.setBounds(x, y, width, height);
- launch();
- }
- //设置Frame的布局
- public void launch() {
- inputTa = new JTextArea(4, 30);
- showTa = new JTextArea(10, 30);
- addrTf = new JTextField(20);
- startButton = new JButton("Start"); //为startButton添加启动服务事件
- sendButton = new JButton("Send");
- sendButton.setEnabled(false);
- exitButton = new JButton("Exit");
- JPanel topPanel = new JPanel(); //定义top部的Jpanel
- //添加顶部的组件
- JLabel titleLabel = new JLabel("BroadCastTest");
- titleLabel.setFont(new Font(Font.MONOSPACED, Font.LAYOUT_NO_LIMIT_CONTEXT, 24));
- topPanel.add(titleLabel);
- JPanel buttonPanel = new JPanel(); //设置底部Button的Panel
- buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
- //添加buttonPanel的组件
- buttonPanel.add(startButton);
- buttonPanel.add(sendButton);
- buttonPanel.add(exitButton);
- //监听inputTa输入域
- inputTa.addKeyListener(new KeyAdapter() {
- public void keyPressed(KeyEvent e) {
- if((e.getKeyCode() == KeyEvent.VK_ENTER) && ((e.getModifiers() & KeyEvent.CTRL_MASK) == KeyEvent.CTRL_MASK) && sendButton.isEnabled()) { //判断按键是否为ctrl+enter
- showTa.append("send: " + getTime() + "/n" +inputTa.getText() + "/n");
- inputTa.setText("");
- send();
- }
- }
- });
- //监听发送按钮
- sendButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- showTa.append("send: " + getTime() + "/n" +inputTa.getText() + "/n");
- inputTa.setText("");
- send();
- }
- });
- //监听退出按钮
- exitButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- System.exit(0);
- }
- });
- //静听开始按钮
- startButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- startButton.setEnabled(false);
- sendButton.setEnabled(true);
- new Thread(new ReceiveThread()).start();
- }
- });
- JPanel bPanel = new JPanel(); //bPnael定义为底部的JPanel
- //添加底部的组件
- bPanel.setLayout(new BorderLayout());
- bPanel.add(new JScrollPane(inputTa), BorderLayout.CENTER);
- bPanel.add(buttonPanel, BorderLayout.SOUTH);
- this.setLayout(new BorderLayout(5, 5));
- this.add(topPanel, BorderLayout.NORTH);
- this.add(new JScrollPane(showTa), BorderLayout.CENTER);
- this.add(bPanel, BorderLayout.SOUTH);
- }
- /*
- * 获取当前时间
- */
- public String getTime() {
- String ret = null;
- Calendar calendar = Calendar.getInstance();
- ret = calendar.get(Calendar.HOUR) + ":" + calendar.get(Calendar.MINUTE) + ":"
- + calendar.get(Calendar.SECOND);
- return ret;
- }
- /*
- * 发送数据
- */
- public void send() {
- int port = 8888; //设置端口号为:8888;
- int level = 0; //设置多点广播套接字的发送范围为本地网络。
- String address = "239.255.8.1"; //设置一个D类地址。
- try {
- InetAddress group = InetAddress.getByName(address);
- MulticastSocket socket = new MulticastSocket(port);
- DatagramPacket data = null;
- byte[] bytes = null;
- String message = inputTa.getText().trim();
- socket.setTimeToLive(level);
- socket.joinGroup(group);
- bytes = message.getBytes();
- data = new DatagramPacket(bytes, bytes.length, group, port);
- socket.send(data);
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /*
- * 接收数据
- */
- class ReceiveThread implements Runnable {
- byte[] bytes = new byte[80];
- String host = "239.255.8.1";
- InetAddress group = null;
- DatagramPacket data = null;
- MulticastSocket socket = null;
- int port = 8888;
- public void run() {
- while(true) {
- try {
- socket = new MulticastSocket(port);
- group = InetAddress.getByName(host);
- socket.joinGroup(group);
- data = new DatagramPacket(bytes, bytes.length, group, port);
- socket.receive(data);
- String message = new String(data.getData(), 0, data.getLength());
- showTa.append("receive: " + getTime() + "/n" + message + "/n");
- showTa.setCaretPosition(showTa.getText().length());
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
2、Client.java
- package onlyfun.gray.client;
- import javax.swing.JFrame;
- import onlyfun.gray.sever.Server;
- public class Client {
- public static void main(String[] args) {
- String title = "Client";
- Server server = new Server(title);
- server.setVisible(true);
- server.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- }