impedindo TCP para si mesmo
This commit is contained in:
parent
947aaf00b7
commit
3efc59444f
|
|
@ -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,10 +74,13 @@ public class SocketTCP extends Thread implements PeerListener {
|
||||||
*/
|
*/
|
||||||
public void send(byte[] msg, InetAddress destinationAddress) {
|
public void send(byte[] msg, InetAddress destinationAddress) {
|
||||||
try {
|
try {
|
||||||
TcpConnection connection;
|
if (destinationAddress.equals(address)) {
|
||||||
|
throw new IllegalArgumentException("Destination address cannot be the same as the original address.");
|
||||||
|
}
|
||||||
|
TcpConnection connec tion;
|
||||||
|
|
||||||
synchronized (connections) {
|
synchronized (connections) {
|
||||||
connection= connections.get(destinationAddress);
|
connection = connections.get(destinationAddress);
|
||||||
|
|
||||||
if (connection == null || connection.isClosed()) {
|
if (connection == null || connection.isClosed()) {
|
||||||
Socket socket = new Socket(destinationAddress, main.getPort());
|
Socket socket = new Socket(destinationAddress, main.getPort());
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue