mirror of
https://github.com/binlaab/nanofiles.git
synced 2026-07-01 12:17:21 +02:00
cosas varias
This commit is contained in:
@@ -5,9 +5,11 @@ import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.util.Map;
|
||||
import java.util.Arrays;
|
||||
import java.util.Enumeration;
|
||||
import java.util.LinkedHashMap;
|
||||
import es.um.redes.nanoFiles.tcp.client.NFConnector;
|
||||
|
||||
@@ -198,6 +200,26 @@ public class DirectoryConnector {
|
||||
public String getDirectoryHostname() {
|
||||
return directoryHostname;
|
||||
}
|
||||
|
||||
public String getClientAddress() {
|
||||
try {
|
||||
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
|
||||
|
||||
while (interfaces.hasMoreElements()) {
|
||||
NetworkInterface ni = interfaces.nextElement();
|
||||
if (ni.isUp() && !ni.isVirtual() && !ni.isLoopback()) {
|
||||
Enumeration<InetAddress> direccionesInterfaz = ni.getInetAddresses();
|
||||
|
||||
while (direccionesInterfaz.hasMoreElements()) {
|
||||
InetAddress addr = direccionesInterfaz.nextElement();
|
||||
if (addr instanceof java.net.Inet4Address) return addr.getHostAddress();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) { e.printStackTrace(); }
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Método para "hacer ping" al directorio, comprobar que está operativo y que
|
||||
@@ -293,14 +315,9 @@ public class DirectoryConnector {
|
||||
|
||||
// este trozo hay que cambiarlo porque qué cojones
|
||||
// encima tiene algo mal
|
||||
String ip = null;
|
||||
try {
|
||||
socket.connect(directoryAddress);
|
||||
ip = socket.getLocalAddress().getHostAddress();
|
||||
socket.disconnect();
|
||||
} catch (Exception e) {}
|
||||
|
||||
DirMessage serve = new DirMessage(DirMessageOps.OPERATION_SERVE, NanoFiles.peerNickname, ip, serverPort);
|
||||
|
||||
DirMessage serve = new DirMessage(DirMessageOps.OPERATION_SERVE, NanoFiles.peerNickname, this.getClientAddress(), serverPort);
|
||||
byte[] serveBytes = serve.toString().getBytes();
|
||||
byte[] response = sendAndReceiveDatagrams(serveBytes);
|
||||
String respStr = new String(response, 0, response.length);
|
||||
|
||||
Reference in New Issue
Block a user