局域网聊天室
局域網聊天室需要完成以下幾個功能:
? ? ? ? * 1.局域網中各主機都能參與到聊天室中聊天,即可以接發消息,其中接受可以接受每臺主機發送的消息,發送的消息每臺主機都能收到
? ? ? ? * 2.簡陋界面
?
?
?
實現思想:
?????? 1.先實現界面的設計。創建自己的J711ChatRoom()類來繼承JFrame()類。即可隨意編寫界面。
?????? 2.發送消息采用多播數據通道MulticastSocket,并且利用joinGroup()方法給其指定一個多播地址groupIp。每個主機都用DatagramPacket來打包自己的消息,然后將數據包發送到多播地址,改數據包得從指定的端口發送,然后MulticastSocket就可以調用send方法把主機發送的數據包給廣播出去。
?????? 3.接收消息采用線程來實現,因為接受消息是實時進行的,所以要實現接受的同時還能發送消息,則可以利用線程的阻塞來完成。首先在指定端口中開啟多播通道MulticastSocket,然后利用joinGroup()方法給其指定一個多播地址groupIp。然后利用receive()方法從多播通道中獲取數據包。此處獲取數據包的代碼處于死循環狀態,使其保持一直接受消息的狀態。然后將接受到的消息append到文本域中。
??????
?
?
?
一些細節:
setLayout(null);?????????? ?? ?// 設置布局方式,為null則代表控件可以根據自身設置的位置任意擺放
setLocationRelativeTo(null); // 參數為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); // 讓發出去的消息與邊框隔開10個單位public J711ChatRoom(){setTitle("聊天室");setSize(500, 500);setLocationRelativeTo(null); // 參數為null此窗口置于屏幕的中央setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setResizable(false);// 設置布局方式,為null則代表控件可以根據自身設置的位置任意擺放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("發送");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);}/**************發送消息***************/public void Send(String msg){try {//創建多播數據報通道 (發數據) DatagramPacketMulticastSocket ms = new MulticastSocket();//客戶端要接到消息都必須通過下面這個地址InetAddress groupIp = InetAddress.getByName("226.81.9.8");ms.joinGroup(groupIp);//將socket通道加入多播地址//將需要發送的數據打包為數據報包String content = msg;//廣播內容//數據包DatagramPacket dp = new DatagramPacket(content.getBytes(), content.getBytes().length);//先把消息發到多播地址dp.setAddress(groupIp);dp.setPort(2426);//發送包裹ms.send(dp);} catch (IOException e) {e.printStackTrace();}}/*************接收消息**************/public class Receiver extends Thread{@Overridepublic void run() {try {int count = 0;//占據指定的端口開啟多播通道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];//聲明數據報包,用于接收廣播信息DatagramPacket dp = new DatagramPacket(b, b.length);while(true){count++;if (count % 8 == 0) area1.setText("");date = new Date();// 獲取當前時間即消息發送的時間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);//設置控件的邊界 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;} }?
?
?
?
?
?
總結
- 上一篇: 计算机维护工具盘,HB520WZ计算机维
- 下一篇: android 亮度 对比度,Andro