我的组播程序如果设置了setLoopbackMode(true)为什么就收不到消息了?

06-07-19 fxbird
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;

}

}

说明一下,测试的时候,要先加入,然后才能发消息

fxbird
2006-07-19 15:57
这是书上的一个例子,用组播技术实现网络聊天,但我如果setLoopbackMode(true),也就是不收到自已发送的消息后,也收不到别人发的消息了,不禁用这种功能则可以。这是为什么?有2个类,一个是启动类,一个是实现类(窗体)。是用jbuilder写的,所以用jbuilder看起来更方便。

猜你喜欢