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.FileNotFoundException;
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);
if (get.indexOf("USER") == 0) {// 说明是用户信息 获取
if (this.usersList.getText().indexOf(get) < 0) {
this.usersList.append(get + "\n");
}
}
if (get.indexOf("LEAVE") == 0) {
String date = get
.substring("LEAVE:".length(), get.length());
String list = this.usersList.getText();
this.usersList
.setText(list.substring(0, list.indexOf(date))
+ list.substring(
list.indexOf(date) + date.length(),
list.length()));
}
if (get.indexOf("CHANGE") == 0) {// 删除操作 同上
String date = get.substring("CHANGE:".length(),
get.length());
String list = this.usersList.getText();
this.usersList
.setText(list.substring(0, list.indexOf(date))
+ list.substring(
list.indexOf(date) + date.length(),
list.length()));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
大家谁能帮我看看。新手 很多东西不是很懂。
第一次用CSDN 也不知道积分是什么东西。也不知道自己有没有积分,默认值是40 就设个40.希望好心人帮我解答一下问题。积分我有一定给。谢谢大家了。
[解决办法]
楼主,可能是你的ttl有问题吧
socket.setTimeToLive(0);