Skip to content

Commit

Permalink
修复局域网发现bug
Browse files Browse the repository at this point in the history
  • Loading branch information
croakfang committed Oct 26, 2021
1 parent 69d9e18 commit 145b908
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 46 deletions.
23 changes: 14 additions & 9 deletions src/CustomConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.io.*;
import java.lang.reflect.Field;
import java.util.ArrayList;

public class CustomConfig {
@SerializedName("本地接收连接所用的端口")
Expand Down Expand Up @@ -65,15 +66,19 @@ public void GetConfig() {
}
}

public void ShowPort() {
String str =
"-----------端口信息----------" +
"\n等待连接端口:" + serverPort +
"\n文件发送端口:" + fileSendPort +
"\n网络发现端口:" + findPort +
"\n网络发现组播地址:" + findAddr +
"\n网络发现启用:" + findEnable +
"\n---------------------------";
public void ShowPort(MySocket socket) {
StringBuilder str = new StringBuilder();
str.append("-----------端口信息----------");
str.append("\n等待连接端口:").append(serverPort);
str.append("\n文件发送端口:").append(fileSendPort);
str.append("\n网络发现端口:").append(findPort);
str.append("\n网络发现组播地址:").append(findAddr);
str.append("\n网络发现启用:").append(findEnable);
str.append("\n本地网卡地址:");
ArrayList<String> LIPList = socket.GetLocalIP();
for(String s:LIPList)
str.append("\n").append(s);
str.append("\n---------------------------");
System.out.println(str);
}

Expand Down
2 changes: 1 addition & 1 deletion src/HelpInfo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
public class HelpInfo {
static String Version = "V0.6";
static String Version = "V0.7";
static String Title =
"|-----------------------------------------------------------|\n"+
"| __ _ ______ __ __ |\n"+
Expand Down
4 changes: 1 addition & 3 deletions src/MsgProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static void GetInput(MySocket mySocket) {
if (inputNext.matches("^##H")) {
HelpInfo.ShowHelp();
} else if (inputNext.matches("^##P")) {
mySocket.config.ShowPort();
mySocket.config.ShowPort(mySocket);
} else if (inputNext.matches("^##Q")) {
mySocket.CloseConnect();
} else if (inputNext.matches("^##R")) {
Expand Down Expand Up @@ -152,8 +152,6 @@ public static Date GetCurTime() {
return new Date(System.currentTimeMillis());
}



public static void CheckChatRecord(MySocket msk) {
msk.chatRecord = ChatRecord.GetChatRecord(msk.CurSocket.getInetAddress().getHostAddress());
Date tempLastDate = null;
Expand Down
88 changes: 55 additions & 33 deletions src/MySocket.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import com.sun.corba.se.spi.activation.Server;

import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -33,47 +35,62 @@ public void Initialization() {

public void NetFind() {
if (!config.findEnable) return;
ArrayList<String> findList = GetLocalIP();
try {
InetAddress findIP = InetAddress.getByName(config.findAddr);
MulticastSocket socket = new MulticastSocket(config.findPort);
socket.setTimeToLive(128);
socket.joinGroup(findIP);
socket.setLoopbackMode(false);
new Thread(() -> {
try {
ArrayList<String> findList = GetLocalIP();
byte[] arb = new byte[12];
DatagramPacket pak = new DatagramPacket(arb, arb.length,findIP,config.findPort);
while (CurSocket == null) {
socket.receive(pak);
String tmp = pak.getAddress().getHostAddress()+ new String(pak.getData()).trim();
if(!findList.contains(tmp)){
findList.add(tmp);
System.out.println("[发现局域网用户:"+tmp+"]");
}
}
socket.leaveGroup(findIP);
socket.close();
} catch (IOException e) {
socket.close();
}
}).start();

new Thread(() -> NetBeFind(findIP)).start();
while (CurSocket == null) {
String data = "##N" + config.serverPort;
try {
String data = ":" + config.serverPort;
DatagramPacket pak = new DatagramPacket(data.getBytes(StandardCharsets.UTF_8), data.length(),findIP,config.findPort);
socket.send(pak);
InetAddress.getByName(config.findAddr);
for (String s : findList) {
InetAddress tmp = InetAddress.getByName(s);
DatagramSocket datagramSocket = new DatagramSocket(config.findPort, tmp);
DatagramPacket pak = new DatagramPacket(data.getBytes(StandardCharsets.UTF_8), data.length(), findIP, config.findPort);
datagramSocket.send(pak);
datagramSocket.close();
}
Thread.sleep(3000);
}
catch (Exception ignored){
} catch (Exception ignored) {
}
}

} catch (Exception ignored) {
}

}

public void NetBeFind(InetAddress findIP){
MulticastSocket socket = null;
try {
socket = new MulticastSocket(config.findPort);
socket.setTimeToLive(128);
socket.joinGroup(findIP);
socket.setLoopbackMode(false);
ArrayList<String> findList = GetLocalIP();
for (int i = 0; i < findList.size(); i++)
findList.set(i, findList.get(i) + ":" + config.serverPort);
byte[] arb = new byte[12];
DatagramPacket pak = new DatagramPacket(arb, arb.length);
while (CurSocket == null) {
socket.receive(pak);
if (new String(pak.getData()).contains("##N")) {
String tmp = pak.getAddress().getHostAddress() + ":" +
new String(pak.getData()).substring(3).trim();
if (!findList.contains(tmp)) {
findList.add(tmp);
System.out.println("[发现局域用户:" + tmp + "]");
}
}
}
socket.leaveGroup(findIP);
socket.close();
} catch (IOException e) {
e.printStackTrace();
if(socket!=null)socket.close();
}
}

public void CloseConnect() {
try {
if (CurSocket != null) {
Expand Down Expand Up @@ -123,7 +140,7 @@ public void Connect() {

int port = strArr.length > 1 ? Integer.parseInt(strArr[1]) : config.serverPort;
ArrayList<String> findList = GetLocalIP();
if(findList.contains(strArr[0] + ":" + port)){
if (findList.contains(strArr[0] + ":" + port)) {
System.out.println("地址或端口无效,请重新输入");
continue;
}
Expand Down Expand Up @@ -310,10 +327,15 @@ private void FileRecv(String name) {
} else System.out.println("已有文件发送/接收中");
}

private ArrayList<String> GetLocalIP() throws SocketException {
public ArrayList<String> GetLocalIP() {
ArrayList<String> out = new ArrayList<>();
Enumeration<NetworkInterface> allNetworkInterfaces =
NetworkInterface.getNetworkInterfaces();
null;
try {
allNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
return new ArrayList<>();
}
NetworkInterface networkInterface = null;

while (allNetworkInterfaces.hasMoreElements()) {
Expand All @@ -326,7 +348,7 @@ private ArrayList<String> GetLocalIP() throws SocketException {
while (allInetAddress.hasMoreElements()) {
ipAddress = allInetAddress.nextElement();
if (ipAddress instanceof Inet4Address) {
out.add(ipAddress.getHostAddress()+":"+config.serverPort);
out.add(ipAddress.getHostAddress());
}
}
}
Expand Down

0 comments on commit 145b908

Please sign in to comment.