一个简易的基于局域网的聊天室程序

//主程序package com.softeem.liaotianshi;import java.awt.Color;import java.awt.Font;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 java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Map;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JTextArea;import javax.swing.JTextField;import javax.swing.border.LineBorder;public class MyFrame extends JFrame implements Runnable{private static Map p = new HashMap();static{p.put("192.168.46.25", "本大爷");p.put("192.168.46.38", "卖女孩的小火柴");p.put("192.168.46.13", "蔡为");p.put("192.168.46.254", "mrchar");p.put("192.168.46.37", "余子杨");}private JLabel title;private JTextField input;private JButton button;private JTextArea textArea;public MyFrame() {getContentPane().setBackground(Color.YELLOW);setTitle(Config.title);setSize(Config.WIDTH, Config.HEIGHT);setLocationRelativeTo(null);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setResizable(false);setLayout(null);initUI();setVisible(Config.SHOW);}private void initUI() {title = new JLabel("聊天室");title.setBounds(180, 10, 200, 30);title.setForeground(Color.RED);title.setFont(new Font("宋体", Font.BOLD, 25));textArea = new JTextArea();textArea.setBounds(20, 60, 450, 300);textArea.setBorder(new LineBorder(new Color(0, 0, 0)));textArea.setEditable(false);input = new JTextField();input.setBounds(20, 430, 350, 30);input.setBorder(new LineBorder(new Color(0,0,0)));button = new JButton("发送");button.setBounds(400,430,60,40);add(button);add(input);add(textArea);add(title);button.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent arg0) {String text = input.getText().trim();if("".equals(text)){showMsgDialog("发送内容不能为空!");}else{try {sender(text);input.setText("");} catch (IOException e) {e.printStackTrace();}}}});}public void sender(String text) throws IOException{MulticastSocket ms = new MulticastSocket();InetAddress groupIp = InetAddress.getByName("226.6.6.6");ms.joinGroup(groupIp);DatagramPacket dp = new DatagramPacket(text.getBytes(),text.getBytes().length);dp.setAddress(groupIp);dp.setPort(9999);ms.send(dp);}public void showMsgDialog(String msg){JOptionPane.showMessageDialog(this, msg,"注意",JOptionPane.INFORMATION_MESSAGE);}@Overridepublic void run() {try {SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");MulticastSocket ms = new MulticastSocket(10000);InetAddress groupIp = InetAddress.getByName("228.8.8.9");ms.joinGroup(groupIp);String s = "";byte[] b = new byte[1024];String sd = "";DatagramPacket dp = new DatagramPacket(b, b.length);while(true){sd = sdf.format(new Date());ms.receive(dp);s = new String(dp.getData(),dp.getOffset(),dp.getLength());if(textArea.getLineCount() > 16){textArea.setText("");}textArea.append((p.get(dp.getAddress().getHostAddress()) + sd + "\n" +s + "\n"));}} catch (UnknownHostException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void main(String[] args) {MyFrame m = new MyFrame();new Thread(m).start();}}//config类,用于存储必须要定义的常量package com.softeem.liaotianshi;public class Config {public static final String title = "聊天室";public static final int WIDTH = 500;public static final int HEIGHT = 600;public static final boolean HIDE = false;public static final boolean SHOW = true;}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值