我的組播程式如果設定了setLoopbackMode(true)為什麼就收不到訊息了?

fxbird發表於2006-07-19
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;
}
}


說明一下,測試的時候,要先加入,然後才能發訊息

相關文章