我的組播程式如果設定了setLoopbackMode(true)為什麼就收不到訊息了?
package frames;
import javax.swing.UIManager;
import java.awt.*;
/**
*啟動FrmWhiteBoard的主類
*/
public class AppWhiteBoard {
boolean packFrame = false;
//Construct the application
public AppWhiteBoard() {
FrmWhiteBoard frame = new FrmWhiteBoard();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
boolean flag=true;
while (flag){
flag=frame.waitforpackets();
}
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new AppWhiteBoard();
}
}
package frames;
/**
* 這個程式可以進行網線聊天,
* 並且可以同步顯示每個人在電子白板上用滑鼠拖拽而生成的線條
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class FrmWhiteBoard extends JFrame {
JPanel contentPane;
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea content = new JTextArea();
JTextField msg = new JTextField();
JButton btnSend = new JButton();
JLabel jLabel1 = new JLabel();
JTextField user_name = new JTextField();
JButton btnJoin = new JButton();
Canvas canvas2 = new Canvas();
JButton jButton3 = new JButton();
int port;
MulticastSocket socket;
InetAddress group;
DatagramPacket packet;
String username;
boolean isMember;
Color color=new Color(255,0,0);
int startx;
int starty;
int endx;
int endy;
//Construct the frame
public FrmWhiteBoard() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(833, 504));
this.setTitle("電子白板程式");
jScrollPane1.setBounds(new Rectangle(16, 13, 340, 254));
msg.setText("");
msg.setBounds(new Rectangle(19, 294, 253, 35));
btnSend.setBounds(new Rectangle(287, 294, 98, 33));
btnSend.setText("傳送");
btnSend.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
btnSend_actionPerformed(e);
}
});
jLabel1.setText("使用者名稱");
jLabel1.setBounds(new Rectangle(17, 363, 64, 37));
user_name.setText("");
user_name.setBounds(new Rectangle(84, 360, 187, 39));
btnJoin.setText("加入");
btnJoin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
btnJoin_actionPerformed(e);
}
});
btnJoin.setBounds(new Rectangle(287, 364, 98, 33));
canvas2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(MouseEvent e) {
canvas2_mousePressed(e);
}
public void mouseReleased(MouseEvent e) {
canvas2_mouseReleased(e);
}
});
canvas2.setBackground(Color.white);
canvas2.setEnabled(false);
canvas2.setBounds(new Rectangle(393, 0, 437, 473));
jButton3.setBounds(new Rectangle(72, 422, 129, 32));
jButton3.setText("清除白板");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton3_actionPerformed(e);
}
});
content.setText("");
contentPane.setBackground(new Color(132, 192, 192));
contentPane.add(jScrollPane1, null);
jScrollPane1.getViewport().add(content, null);
contentPane.add(msg, null);
contentPane.add(jLabel1, null);
contentPane.add(user_name, null);
contentPane.add(canvas2, null);
contentPane.add(btnSend, null);
contentPane.add(btnJoin, null);
contentPane.add(jButton3, null);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void canvas2_mousePressed(MouseEvent e) {
startx=e.getX();
starty=e.getY();
}
void canvas2_mouseReleased(MouseEvent e) {
try {
endx = e.getX();
endy = e.getY();
Graphics g = canvas2.getGraphics();
g.setColor(color);
g.drawLine(startx, starty, endx, endy);
String tmp = "@" + startx + "@" + starty + "@" + endx + "@" + endy + "@";
byte[] data = tmp.getBytes();
packet = new DatagramPacket(data, data.length, group, port);
socket.send(packet);
} catch (IOException ex) {
ex.printStackTrace();
}
}
void btnJoin_actionPerformed(ActionEvent e) {
if (!isMember){//加入
try {
port = 5000;
group = InetAddress.getByName("239.0.0.0");
socket = new MulticastSocket(port);
socket.setTimeToLive(32); ;
socket.setSoTimeout(10000);
socket.joinGroup(group);
socket.setLoopbackMode(true);
username = user_name.getText();
String tmp = username + " has joined the group\n";
content.append(tmp);
byte[] data = tmp.getBytes();
packet = new DatagramPacket(data, data.length, group, port);
socket.setTimeToLive(2);
socket.send(packet);
btnJoin.setText("離開");
isMember = true;
canvas2.setEnabled(true);
} catch (UnknownHostException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
} else {//離開
try {
String tmp = username + " has left the group\n";
content.append(tmp);
byte[] data = tmp.getBytes();
packet = new DatagramPacket(data, data.length, group, port);
socket.send(packet);
socket.leaveGroup(group);
btnJoin.setText("加入");
isMember = false;
canvas2.setEnabled(false);
} catch (IOException ex1) {
ex1.printStackTrace();
}
}
}
void btnSend_actionPerformed(ActionEvent e) {//傳送一條資訊
try {
String tmp = username + " said:" + msg.getText();
byte[] data = tmp.getBytes();
packet = new DatagramPacket(data, data.length, group, port);
socket.send(packet);
msg.setText("");
content.append(tmp+"\n");
} catch (IOException ex) {
ex.printStackTrace();
}
}
void jButton3_actionPerformed(ActionEvent e) {//清除電子白板的內容
canvas2.setBackground(Color.black);
canvas2.setBackground(Color.white);
}
public boolean waitforpackets(){//接收資訊
byte[] packetdata=new byte[512];
try {
// packet.setData(packetdata);
socket.receive(packet);
} catch (Exception ex) {
// ex.printStackTrace();
return true;
}
packetdata=packet.getData();
if (packetdata[0]!='@'){//文字資訊
content.append(new String(packet.getData(),0,packet.getLength())+"\n");
} else {//圖形資訊
try {
String data = new String(packetdata);
String[] arr = data.split("@");
startx = Integer.parseInt(arr[1]);
starty = Integer.parseInt(arr[2]);
endx = Integer.parseInt(arr[3]);
endy = Integer.parseInt(arr[4]);
Graphics g = canvas2.getGraphics();
g.setColor(color);
g.drawLine(startx, starty, endx, endy);
} catch (NumberFormatException ex1) {
ex1.printStackTrace();
}
}
// packet.setLength(512);
return true;
}
}
說明一下,測試的時候,要先加入,然後才能發訊息
import javax.swing.UIManager;
import java.awt.*;
/**
*啟動FrmWhiteBoard的主類
*/
public class AppWhiteBoard {
boolean packFrame = false;
//Construct the application
public AppWhiteBoard() {
FrmWhiteBoard frame = new FrmWhiteBoard();
//Validate frames that have preset sizes
//Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
}
else {
frame.validate();
}
//Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
boolean flag=true;
while (flag){
flag=frame.waitforpackets();
}
}
//Main method
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new AppWhiteBoard();
}
}
package frames;
/**
* 這個程式可以進行網線聊天,
* 並且可以同步顯示每個人在電子白板上用滑鼠拖拽而生成的線條
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
public class FrmWhiteBoard extends JFrame {
JPanel contentPane;
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea content = new JTextArea();
JTextField msg = new JTextField();
JButton btnSend = new JButton();
JLabel jLabel1 = new JLabel();
JTextField user_name = new JTextField();
JButton btnJoin = new JButton();
Canvas canvas2 = new Canvas();
JButton jButton3 = new JButton();
int port;
MulticastSocket socket;
InetAddress group;
DatagramPacket packet;
String username;
boolean isMember;
Color color=new Color(255,0,0);
int startx;
int starty;
int endx;
int endy;
//Construct the frame
public FrmWhiteBoard() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
contentPane.setLayout(null);
this.setSize(new Dimension(833, 504));
this.setTitle("電子白板程式");
jScrollPane1.setBounds(new Rectangle(16, 13, 340, 254));
msg.setText("");
msg.setBounds(new Rectangle(19, 294, 253, 35));
btnSend.setBounds(new Rectangle(287, 294, 98, 33));
btnSend.setText("傳送");
btnSend.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
btnSend_actionPerformed(e);
}
});
jLabel1.setText("使用者名稱");
jLabel1.setBounds(new Rectangle(17, 363, 64, 37));
user_name.setText("");
user_name.setBounds(new Rectangle(84, 360, 187, 39));
btnJoin.setText("加入");
btnJoin.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
btnJoin_actionPerformed(e);
}
});
btnJoin.setBounds(new Rectangle(287, 364, 98, 33));
canvas2.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(MouseEvent e) {
canvas2_mousePressed(e);
}
public void mouseReleased(MouseEvent e) {
canvas2_mouseReleased(e);
}
});
canvas2.setBackground(Color.white);
canvas2.setEnabled(false);
canvas2.setBounds(new Rectangle(393, 0, 437, 473));
jButton3.setBounds(new Rectangle(72, 422, 129, 32));
jButton3.setText("清除白板");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
jButton3_actionPerformed(e);
}
});
content.setText("");
contentPane.setBackground(new Color(132, 192, 192));
contentPane.add(jScrollPane1, null);
jScrollPane1.getViewport().add(content, null);
contentPane.add(msg, null);
contentPane.add(jLabel1, null);
contentPane.add(user_name, null);
contentPane.add(canvas2, null);
contentPane.add(btnSend, null);
contentPane.add(btnJoin, null);
contentPane.add(jButton3, null);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void canvas2_mousePressed(MouseEvent e) {
startx=e.getX();
starty=e.getY();
}
void canvas2_mouseReleased(MouseEvent e) {
try {
endx = e.getX();
endy = e.getY();
Graphics g = canvas2.getGraphics();
g.setColor(color);
g.drawLine(startx, starty, endx, endy);
String tmp = "@" + startx + "@" + starty + "@" + endx + "@" + endy + "@";
byte[] data = tmp.getBytes();
packet = new DatagramPacket(data, data.length, group, port);
socket.send(packet);
} catch (IOException ex) {
ex.printStackTrace();
}
}
void btnJoin_actionPerformed(ActionEvent e) {
if (!isMember){//加入
try {
port = 5000;
group = InetAddress.getByName("239.0.0.0");
socket = new MulticastSocket(port);
socket.setTimeToLive(32); ;
socket.setSoTimeout(10000);
socket.joinGroup(group);
socket.setLoopbackMode(true);
username = user_name.getText();
String tmp = username + " has joined the group\n";
content.append(tmp);
byte[] data = tmp.getBytes();
packet = new DatagramPacket(data, data.length, group, port);
socket.setTimeToLive(2);
socket.send(packet);
btnJoin.setText("離開");
isMember = true;
canvas2.setEnabled(true);
} catch (UnknownHostException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
} else {//離開
try {
String tmp = username + " has left the group\n";
content.append(tmp);
byte[] data = tmp.getBytes();
packet = new DatagramPacket(data, data.length, group, port);
socket.send(packet);
socket.leaveGroup(group);
btnJoin.setText("加入");
isMember = false;
canvas2.setEnabled(false);
} catch (IOException ex1) {
ex1.printStackTrace();
}
}
}
void btnSend_actionPerformed(ActionEvent e) {//傳送一條資訊
try {
String tmp = username + " said:" + msg.getText();
byte[] data = tmp.getBytes();
packet = new DatagramPacket(data, data.length, group, port);
socket.send(packet);
msg.setText("");
content.append(tmp+"\n");
} catch (IOException ex) {
ex.printStackTrace();
}
}
void jButton3_actionPerformed(ActionEvent e) {//清除電子白板的內容
canvas2.setBackground(Color.black);
canvas2.setBackground(Color.white);
}
public boolean waitforpackets(){//接收資訊
byte[] packetdata=new byte[512];
try {
// packet.setData(packetdata);
socket.receive(packet);
} catch (Exception ex) {
// ex.printStackTrace();
return true;
}
packetdata=packet.getData();
if (packetdata[0]!='@'){//文字資訊
content.append(new String(packet.getData(),0,packet.getLength())+"\n");
} else {//圖形資訊
try {
String data = new String(packetdata);
String[] arr = data.split("@");
startx = Integer.parseInt(arr[1]);
starty = Integer.parseInt(arr[2]);
endx = Integer.parseInt(arr[3]);
endy = Integer.parseInt(arr[4]);
Graphics g = canvas2.getGraphics();
g.setColor(color);
g.drawLine(startx, starty, endx, endy);
} catch (NumberFormatException ex1) {
ex1.printStackTrace();
}
}
// packet.setLength(512);
return true;
}
}
說明一下,測試的時候,要先加入,然後才能發訊息
相關文章
- 為什麼我喜歡程式設計 程式設計充滿了樂趣程式設計
- 運維告訴我CPU飆升300%,為什麼我的程式上線就奔潰了運維
- 為什麼我們需要訊息佇列?佇列
- 一位設計師的自白:我為什麼一個月就離開了蘋果?蘋果
- 我學習的程式設計,都給我帶來了什麼?程式設計
- 為什麼Date物件被設定成過時了物件
- 為什麼 [] == ![] 為 true?
- Donald Knuth:我為什麼不用Email了?AI
- 我們工作到底為了什麼
- 小米筆記本吹成PPT了 看完這篇你就知道為什麼我要吐槽了筆記
- 我為什麼選擇了 AdonisJsJS
- 我在家門口散步了7公里,就為了搞清楚“城市智慧體”到底是什麼智慧體
- 為什麼結束了十年的程式設計生涯?程式設計
- 我去,為什麼最左字首原則失效了?
- 我為什麼放棄MySQL?選擇了MongoDBMySqlMongoDB
- 為什麼你該開始學習程式設計了?程式設計
- Service Mesh是什麼,為我們解決了什麼問題?
- Apache RocketMQ中如果一個topic堆積了非常多的訊息會導致rockemq出現什麼問題ApacheMQ
- go2.0怎麼沒訊息了Go
- 轉:我們到底為了什麼鑽研技術?
- 怎麼了?我就磕上安卓了!沒有男朋友我也要做安卓安卓
- 為什麼Oracle PULA在宣佈不久後就消失了?TLOracle
- 新專案為什麼決定用 JDK 17了JDK
- YouTube視訊推薦系統為什麼那麼強?看了這篇文章你就知道了
- 關於Jmeter執行緒組的設定,看這一篇就夠了JMeter執行緒
- springboot使用RabbitMQ的fanout廣播模式消費者死活接收不到訊息Spring BootMQ模式
- 我選單為什麼跟著滑動了?!!—— 固定定位為什麼會失效?
- 我為什麼是程式設計師?程式設計師
- 我為什麼痴迷於程式設計?程式設計
- 為什麼我還在程式設計程式設計
- 我為什麼喜歡程式設計程式設計
- 我用的是ms sql server2000 我已經安裝了sqlforjdbc了為什麼不行?SQLServerJDBC
- 入門RabbitMQ訊息佇列,看這篇文章就夠了MQ佇列
- 雲原生訊息佇列RocketMQ:為什麼我們選擇 RocketMQ佇列MQ
- 為什麼我們不用sourcemap了?hey-cli預設關閉打包配置
- [譯] 為什麼我放棄了 React 而轉向 Vue。ReactVue
- 李嘉誠:我今年90了 為什麼還要努力工作?
- 我為什麼把think in java 讀了10遍(轉載)Java