JAVA局域网广播 其他机器收不到。
本人接触JAVA 一个多月了 自己写了个 局域网聊天 传文件 和群聊的小软件。群聊不是用的服务器客户端的模式,而是直接在局域网中用 multiCastSocket进行广播。但是 自己的机器上不管能收到广播,在局域网其他的机器上就收不到。也在网上查了很久,没找到相似的情况。纠结了两天了。于是想在这里来问问大家 代码很长 我贴出个我广播自己在线,和在线用户查询的代码。
//广播自己在线的类
package lMess;
//服务端代码:不断广播--自身信息
import java.io.BufferedReader;
import java.io.File;
import
java.io.FileNotFoundException;
import java.io.FileReader;
import
java.io.IOException;
import java.net.*;
public class BroadCast extends Thread {
String message;
int port = 50611;
InetAddress group = null;
MulticastSocket socket = null;
private javax.swing.JTextField userNameArea;
String ip;
BroadCast(javax.swing.JTextField userNameArea) {
this.userNameArea = userNameArea;
File ipFile = new File("ipBroadCast.ini");
try {
if (ipFile.exists()) {
BufferedReader reader = new BufferedReader(new FileReader(
"ipBroadCast.ini"));
try {
ip = reader.readLine();
} catch (
IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (
FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
group = InetAddress.getByName(ip);
socket = new MulticastSocket(port);
socket.setTimeToLive(0);
socket.joinGroup(group);
} catch (Exception e) {
}
}
public void run() {
while (true) {
try {
DatagramPacket packet = null;
message = "USER:" + userNameArea.getText() + " @ "
+ InetAddress.getLocalHost();
byte data[] = message.getBytes();
packet = new DatagramPacket(data, data.length, group, port);
System.out.println(new String(data));
socket.send(packet);
sleep(4000);
} catch (Exception e) {
}
}
}
}
//接受广播的类
package lMess;
import java.io.BufferedReader;
import java.io.File;
import java.io.File
NotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.
UnknownHostException;
import javax.swing.JButton;
import javax.swing.JTextArea;
public class UsersFinding implements Runnable {
private javax.swing.JTextArea usersList;
int port = 50611;
InetAddress group = null;
MulticastSocket socket = null;
JButton startReceive, stopReceive;
JTextArea showArea;
Thread thread;
boolean stop = false;
String ip;
public UsersFinding(javax.swing.JTextArea usersList) {
this.usersList = usersList;
File ipFile = new File("ipBroadCast.ini");
try {
if (ipFile.exists()) {
BufferedReader reader = new BufferedReader(new FileReader(
"ipBroadCast.ini"));
try {
ip = reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public void run() {
try {
while (true) {
group = InetAddress.getByName(ip);
socket = new MulticastSocket(port);
socket.joinGroup(group);
byte[] buffer = new byte[256];
DatagramPacket dgp = new DatagramPacket(buffer, buffer.length);
socket.receive(dgp);
byte[] getMessage = new byte[dgp.getLength()];
System.arraycopy(dgp.getData(), 0, getMessage, 0,
dgp.getLength());
String get = new String(getMessage);
System.out.println("UsersFinding get the Message:\n" + get);