局域网聊天室
局域網(wǎng)聊天室需要完成以下幾個功能:
? ? ? ? * 1.局域網(wǎng)中各主機都能參與到聊天室中聊天,即可以接發(fā)消息,其中接受可以接受每臺主機發(fā)送的消息,發(fā)送的消息每臺主機都能收到
? ? ? ? * 2.簡陋界面
?
?
?
實現(xiàn)思想:
?????? 1.先實現(xiàn)界面的設(shè)計。創(chuàng)建自己的J711ChatRoom()類來繼承JFrame()類。即可隨意編寫界面。
?????? 2.發(fā)送消息采用多播數(shù)據(jù)通道MulticastSocket,并且利用joinGroup()方法給其指定一個多播地址groupIp。每個主機都用DatagramPacket來打包自己的消息,然后將數(shù)據(jù)包發(fā)送到多播地址,改數(shù)據(jù)包得從指定的端口發(fā)送,然后MulticastSocket就可以調(diào)用send方法把主機發(fā)送的數(shù)據(jù)包給廣播出去。
?????? 3.接收消息采用線程來實現(xiàn),因為接受消息是實時進行的,所以要實現(xiàn)接受的同時還能發(fā)送消息,則可以利用線程的阻塞來完成。首先在指定端口中開啟多播通道MulticastSocket,然后利用joinGroup()方法給其指定一個多播地址groupIp。然后利用receive()方法從多播通道中獲取數(shù)據(jù)包。此處獲取數(shù)據(jù)包的代碼處于死循環(huán)狀態(tài),使其保持一直接受消息的狀態(tài)。然后將接受到的消息append到文本域中。
??????
?
?
?
一些細節(jié):
setLayout(null);?????????? ?? ?// 設(shè)置布局方式,為null則代表控件可以根據(jù)自身設(shè)置的位置任意擺放
setLocationRelativeTo(null); // 參數(shù)為null此窗口置于屏幕的中央
ms.joinGroup(“/226.81.9.8");// 地址前要加一個反斜杠?
?
?
?
?
?
以下是運行效果:
?
?
?
?
?
import java.awt.Color; import java.awt.Font; import java.awt.Insets; 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.text.SimpleDateFormat; import java.util.Date;import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.border.LineBorder;public class J711ChatRoom extends JFrame{public static void main(String[] args) {new J711ChatRoom();}private JLabel label1;private JTextArea area1;// 文本域private JTextArea area2;// 文本域private JButton button;private Insets margin = new Insets(10, 10, 10, 10); // 讓發(fā)出去的消息與邊框隔開10個單位public J711ChatRoom(){setTitle("聊天室");setSize(500, 500);setLocationRelativeTo(null); // 參數(shù)為null此窗口置于屏幕的中央setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setResizable(false);// 設(shè)置布局方式,為null則代表控件可以根據(jù)自身設(shè)置的位置任意擺放setLayout(null);initUI(); setVisible(true);}private void initUI() {label1 = createJLabel("J711聊天室", 180, 20, 200, 30);area1 = createJTextArea(50, 60, 395, 230);area1.setMargin(margin);// 前面空兩格area2 = createJTextArea(50, 300, 395, 100);button = new JButton("發(fā)送");button.setBounds(345, 410, 100, 30);new Receiver().start();button.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {Send(area2.getText());area2.setText("");} });add(label1);add(area1);add(area2);add(button);}/**************發(fā)送消息***************/public void Send(String msg){try {//創(chuàng)建多播數(shù)據(jù)報通道 (發(fā)數(shù)據(jù)) DatagramPacketMulticastSocket ms = new MulticastSocket();//客戶端要接到消息都必須通過下面這個地址InetAddress groupIp = InetAddress.getByName("226.81.9.8");ms.joinGroup(groupIp);//將socket通道加入多播地址//將需要發(fā)送的數(shù)據(jù)打包為數(shù)據(jù)報包String content = msg;//廣播內(nèi)容//數(shù)據(jù)包DatagramPacket dp = new DatagramPacket(content.getBytes(), content.getBytes().length);//先把消息發(fā)到多播地址dp.setAddress(groupIp);dp.setPort(2426);//發(fā)送包裹ms.send(dp);} catch (IOException e) {e.printStackTrace();}}/*************接收消息**************/public class Receiver extends Thread{@Overridepublic void run() {try {int count = 0;//占據(jù)指定的端口開啟多播通道MulticastSocket ms = new MulticastSocket(2426);InetAddress groupIp = InetAddress.getByName("226.81.9.8");ms.joinGroup(groupIp);String s = "";Date date = null;SimpleDateFormat sdf;byte[] b = new byte[1024];//聲明數(shù)據(jù)報包,用于接收廣播信息DatagramPacket dp = new DatagramPacket(b, b.length);while(true){count++;if (count % 8 == 0) area1.setText("");date = new Date();// 獲取當前時間即消息發(fā)送的時間sdf = new SimpleDateFormat("HH:mm:ss");dp = new DatagramPacket(b, b.length);ms.receive(dp);s = new String(dp.getData(), dp.getOffset(), dp.getLength()); s = dp.getAddress() + " "+ sdf.format(date) + "\n" + s + "\n";area1.append(s);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public JTextArea createJTextArea(int x, int y, int w, int h){JTextArea area = new JTextArea();area.setBounds(x, y, w, h);area.setLineWrap(true); // 自動換行area.setBorder(new LineBorder(new Color(129, 152, 48)));return area;}public JLabel createJLabel(String name, int x, int y, int w, int h){JLabel label = new JLabel(name);//設(shè)置控件的邊界 x y 寬 高label.setBounds(x, y, w, h);label.setHorizontalTextPosition(JLabel.CENTER); label.setFont(new Font("楷體", Font.BOLD, 20));label.setForeground(Color.RED);return label;} }?
?
?
?
?
?
總結(jié)
- 上一篇: 计算机维护工具盘,HB520WZ计算机维
- 下一篇: 内网终端安全管理