为何广播程序不能真正广播?请教高手!

hl_longman 2004-06-22 12:25:35
本人用的是Win2K+Jcreator Pro:
本机IP: 10.3.8.132

client:


import java.net.*;
import java.awt.*;
import java.awt.event.*;


public class Receive extends Frame implements Runnable,ActionListener{

int port;
InetAddress group=null;
MulticastSocket socket=null;
Button start,end;
TextArea displayInfoing,displayInfoed;
Thread thread;
boolean flag=false;

//constuctor function
public Receive(){

super("Receive Information Timer");

thread=new Thread(this);
start=new Button("Strart Re");
end=new Button("End Re");
start.addActionListener(this);
end.addActionListener(this);

displayInfoing=new TextArea(10,10);
displayInfoing.setForeground(Color.blue);

displayInfoed=new TextArea(10,10);

Panel north=new Panel();
north.add(start);
north.add(end);
add(north,BorderLayout.NORTH);

Panel center=new Panel();
center.setLayout(new GridLayout(1,2));
center.add(displayInfoing);
center.add(displayInfoed);
add(center,BorderLayout.CENTER);

validate();
port=5000;

try{

group=InetAddress.getByName("10.3.0.0");

}catch(Exception e){

}

setBounds(100,50,360,380);

setVisible(true);

addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});

}


//*******************************************************************************

public void actionPerformed(ActionEvent e){

if(e.getSource()==start) {

start.setBackground(Color.blue);
end.setBackground(Color.gray);

if(!(thread.isAlive())){

thread=new Thread(this);
}

try{

thread.start();
flag=false;
}
catch(Exception ee){

}
}

if(e.getSource()==end) {

start.setBackground(Color.gray);
end.setBackground(Color.blue);
flag=false;
thread.interrupt();
}

}

//*******************************************************************************
public void run(){

while(true){

byte data[]=new byte[8192];
DatagramPacket packet=null;
packet=new DatagramPacket(data,data.length,group,port);

try{

socket.receive(packet);
String message =new String(packet.getData(),0,packet.getLength());
displayInfoing.setText("Receiving the Word is:\n"+message);
displayInfoed.setText(message+"\n");

}catch(Exception e){


}

if(flag==true){
break;
}
}

}
//*******************************************************************************

public static void main(String args[]){

new Receive();
}
}


server:

import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.Timer;
import java.net.*;


public class BroadCastWord extends Frame implements ActionListener{

int port;
MulticastSocket socket=null;
InetAddress group=null;
Timer time=null;
FileDialog open=null;
Button select,start,end;
File file=null;
FileReader in=null;
BufferedReader bufferIn=null;
String FileDir=null,fileName=null;
int token=0;
TextArea displayInfoing,displayInfoed;


public BroadCastWord(){

super("Word Broad Server") ;
select=new Button("Select File");
start=new Button("Strat BC");
end=new Button("End BC");

start.setEnabled(false);
select.addActionListener(this);
start.addActionListener(this);
end.addActionListener(this);

time=new Timer(1000,this);
open=new FileDialog(this,"Select File",FileDialog.LOAD);

displayInfoing=new TextArea(10,10);
displayInfoing.setBackground(Color.blue);
displayInfoed=new TextArea(10,10);

Panel north=new Panel();
north.add(select);
north.add(start);
north.add(end);

add(north,BorderLayout.NORTH);

Panel center=new Panel();
center.setLayout(new GridLayout(1,2));
center.add(displayInfoing);
center.add(displayInfoed);
add(center,BorderLayout.CENTER);
validate();

try{

port=5000;
group=InetAddress.getByName("10.3.0.0");
socket=new MulticastSocket(port);
socket.setTimeToLive(1);
socket.joinGroup(group);

}
catch(Exception ee){

System.out.println("Error"+ee);
}

setBounds(100,50,360,380);
setVisible(true);

addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}

public void actionPerformed(ActionEvent e){

if(e.getSource()==select) {

displayInfoed.setText(null);
open.setVisible(true);
fileName=open.getFile();
FileDir=open.getDirectory();

if(fileName!=null){
time.stop();
file=new File(FileDir,fileName);
try{
file=new File(FileDir,fileName);
in=new FileReader(file);
bufferIn=new BufferedReader(in);
start.setEnabled(true);

}
catch(Exception ex){

}
}

}
else if(e.getSource()==start) {

time.start();
}
else if(e.getSource()==time){

String s=null;
try{

if(token==-1){

file=new File(FileDir,fileName);
in=new FileReader(file);
bufferIn=new BufferedReader(in);
}

s=bufferIn.readLine();
if(s!=null) {
token=0;
displayInfoing.setText("Borasd the:\n"+s);
displayInfoed.append(s+"\n");
DatagramPacket packet=null;
byte data[]=s.getBytes();
packet=new DatagramPacket(data,data.length,group,port);
socket.send(packet);
}
else{
token=-1;
}
}
catch(IOException ioE){

}
}
else if(e.getSource()==end){

time.stop();
}
}

public static void main(String args[]){
BroadCastWord broad=new BroadCastWord();
}
}

请高手指点!

...全文
324 28 打赏 收藏 转发到动态 举报
写回复
用AI写文章
28 条回复
切换为时间正序
请发表友善的回复…
发表回复
hl_longman 2004-06-25
  • 打赏
  • 举报
回复
谢谢了,要结贴了!!
Jing77 2004-06-24
  • 打赏
  • 举报
回复
如果要单机运行,在main方法里加一行:new Broadcaster(1504,1501);就可以了^^
hl_longman 2004-06-24
  • 打赏
  • 举报
回复
我先试试看!
Jing77 2004-06-24
  • 打赏
  • 举报
回复
import javax.swing.*;

public class Receiver extends Thread
{
Broadcast listener;
JTextArea displayer;
String inMsg;

public Receiver(Broadcast listener,JTextArea displayer)
{
this.listener=listener;
this.displayer=displayer;
}

public void run()
{
while(true)
{
inMsg=listener.receiveData();
displayer.append(inMsg);
displayer.append("\n");

}
}
}
Jing77 2004-06-24
  • 打赏
  • 举报
回复
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Broadcaster extends JFrame implements ActionListener
{
Broadcast broadcast;
Receiver receiver;
JTextArea displayer;
JTextField sender;
JButton btnSend;

public Broadcaster(int a,int b)
{
broadcast=new Broadcast(a,b);
displayer=new JTextArea(10,15);
receiver=new Receiver(broadcast,displayer);
sender=new JTextField(10);
btnSend=new JButton("Send");
btnSend.addActionListener(this);
JPanel north=new JPanel();
north.add(sender);
north.add(btnSend);
JScrollPane scroll=new JScrollPane(displayer);
this.getContentPane().add(north,BorderLayout.NORTH);
this.getContentPane().add(scroll,BorderLayout.CENTER);
this.setSize(300,200);
this.setVisible(true);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
receiver.start();
}

public void actionPerformed(ActionEvent e)
{
broadcast.sendData(sender.getText());
sender.setText("");
displayer.requestFocus(true);
sender.requestFocus(true);
}

public static void main(String args[])
{
new Broadcaster(1500,1501);
}
}
Jing77 2004-06-24
  • 打赏
  • 举报
回复
实在不想慢慢看别人代码~~不好意思。
以下是我刚写的。在两台机上试过了


import java.net.*;
import java.io.*;
public class Broadcast
{
private InetAddress group;
private int serverPort,clientPort;
private DatagramSocket sender;
private MulticastSocket receiver;
public Broadcast(int serverPort,int clientPort)
{
this.serverPort=serverPort;
this.clientPort=clientPort;
try
{
group=InetAddress.getByName("230.0.0.0");
sender=new DatagramSocket(serverPort);
receiver=new MulticastSocket(clientPort);
receiver.joinGroup(group);
}
catch(Exception e)
{
e.printStackTrace();
}

}


public void sendData(String msg)
{
byte[] b=new byte[1024];
DatagramPacket packet;
try
{
b=msg.trim().getBytes();
packet=new DatagramPacket(b,b.length,group,clientPort);

sender.send(packet);
}
catch(Exception e)
{
e.printStackTrace();
}
}

public String receiveData()
{
byte[] b=new byte[1024];
DatagramPacket packet=new DatagramPacket(b,1024);
String inMsg;
try
{
receiver.receive(packet);
}
catch(IOException e)
{
e.printStackTrace();
}
b=packet.getData();
inMsg=new String(b).trim();
return inMsg;
}
}
hl_longman 2004-06-24
  • 打赏
  • 举报
回复
请哪位调一下,在自己的机子上过了再粘一下源码吧!
我试了多个方法,还是不行!
Jing77 2004-06-24
  • 打赏
  • 举报
回复
可以广播啊。
广播组的地址只是个标志而已,不需要真的有那台机器,只要在范围之内就可以
hl_longman 2004-06-24
  • 打赏
  • 举报
回复
这样的问题没有高手能搞定吗?
hl_longman 2004-06-24
  • 打赏
  • 举报
回复
A multicast group is specified by a class D IP address and by a standard UDP port number. Class D IP addresses are in the range 224.0.0.0 to 239.255.255.255, inclusive. The address 224.0.0.0 is reserved and should not be used.

这是JavaTM 2 Platform Std. Ed. v1.4.2 doc里的原文。

如果还有问题,大家再一起讨论


如果这样的话,那在LAN中就不能广播消息了,哈!以上的几个方法我都试了还是不行!
xueqs 2004-06-23
  • 打赏
  • 举报
回复
gz
Jing77 2004-06-23
  • 打赏
  • 举报
回复
A multicast group is specified by a class D IP address and by a standard UDP port number. Class D IP addresses are in the range 224.0.0.0 to 239.255.255.255, inclusive. The address 224.0.0.0 is reserved and should not be used.

这是JavaTM 2 Platform Std. Ed. v1.4.2 doc里的原文。

如果还有问题,大家再一起讨论
eclipse0016 2004-06-23
  • 打赏
  • 举报
回复
你的子网掩码是多少?如果是255.255.255.0的话,建议使用10.3.8.255试一下
老土豆T 2004-06-23
  • 打赏
  • 举报
回复
恩...有道理..
hl_longman 2004-06-22
  • 打赏
  • 举报
回复
upupupupupuupupupupupu!
hl_longman 2004-06-22
  • 打赏
  • 举报
回复
up
hl_longman 2004-06-22
  • 打赏
  • 举报
回复
UP
julyclyde 2004-06-22
  • 打赏
  • 举报
回复
网段内部广播不需要设备支持
你看看掩码是不是设置错了
北极猩猩 2004-06-22
  • 打赏
  • 举报
回复
广播需要路由器的支持。
zuxingyuan 2004-06-22
  • 打赏
  • 举报
回复
guanzhu
加载更多回复(8)

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧