impedindo TCP para si mesmo

This commit is contained in:
GustavoHMDS 2026-08-18 12:53:30 -03:00
parent 947aaf00b7
commit 3efc59444f
1 changed files with 7 additions and 2 deletions

View File

@ -35,6 +35,7 @@ public class SocketTCP extends Thread implements PeerListener {
private final ExecutorService connectionExecutor = Executors.newCachedThreadPool(); private final ExecutorService connectionExecutor = Executors.newCachedThreadPool();
private final Map<InetAddress, TcpConnection> connections; private final Map<InetAddress, TcpConnection> connections;
private ServerSocket serverSocket; private ServerSocket serverSocket;
private InetAddress address;
private final MainWindow main; private final MainWindow main;
@ -54,6 +55,7 @@ public class SocketTCP extends Thread implements PeerListener {
connections = new ConcurrentHashMap<>(); connections = new ConcurrentHashMap<>();
try { try {
serverSocket = new ServerSocket(main.getPort()); serverSocket = new ServerSocket(main.getPort());
address = InetAddress.getByName(InetAddress.getLocalHost().getHostAddress());
} catch (IOException ex) { } catch (IOException ex) {
System.out.println("There is no socket connection. Sorry."); System.out.println("There is no socket connection. Sorry.");
System.out.println(ex); System.out.println(ex);
@ -72,6 +74,9 @@ public class SocketTCP extends Thread implements PeerListener {
*/ */
public void send(byte[] msg, InetAddress destinationAddress) { public void send(byte[] msg, InetAddress destinationAddress) {
try { try {
if (destinationAddress.equals(address)) {
throw new IllegalArgumentException("Destination address cannot be the same as the original address.");
}
TcpConnection connec tion; TcpConnection connec tion;
synchronized (connections) { synchronized (connections) {