Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting socket local port throws exception
    primarykey
    data
    text
    <p>Given this:</p> <pre><code>. . . ServerSocket serverSocket = new ServerSocket(1111); while (helperSockets.size() &lt; Common.NUM_HELPERS) { Socket helperSocket = serverSocket.accept(); . . . </code></pre> <p>This throws the exception:</p> <pre><code>new Socket("localhost", 1111, InetAddress.getLocalHost(), localPort); </code></pre> <p>Exception:</p> <pre><code>java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine </code></pre> <p>I'm trying to use this constructor to set the local port, so what am I doing wrong? Source below. </p> <p>The constructor creates a thread that listens for clients via ServerSockets, while trying to connect to other clients. This client tries to connect with itself for testing. The problem is encountered upon first iteration of the for loop.</p> <pre><code>public class DisSemHelper extends Thread { private int id; private int semaphore; private Clock clock; private Vector&lt;Socket&gt; helperSockets; private int localPort; private int receivedSender; private String receivedOperation; private int receivedTimestamp; /** */ public DisSemHelper(int id) { this.id = id; this.semaphore = 0; this.clock = new Clock(); this.helperSockets = new Vector&lt;Socket&gt;(); this.receivedSender = -1; this.receivedOperation = null; this.receivedTimestamp = -1; this.localPort = Common.portMap.get(id); new ConnectionListener().start(); /* Create and store connections to all helpers */ for (int i=0; helperSockets.size() &lt; Common.NUM_HELPERS; i++) { Socket helperSocket = null; // String portKey = "STREET_" + i; /* Wait until target street socket is ready. Retry every second. */ Exception e = new ConnectException(); while (helperSocket == null) { try { Thread.sleep(1000); helperSocket = new Socket("localhost", 2222, InetAddress.getLocalHost(), localPort); } catch (ConnectException ce) { e = ce; e.printStackTrace(); } catch (UnknownHostException uhe) { uhe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } catch (InterruptedException ie) { e.printStackTrace(); } } int remotePort = helperSocket.getPort(); int connectedHelperID = Common.portMap.indexOf(remotePort); if (this.helperSockets.size() &lt;= connectedHelperID) { this.helperSockets.add(helperSocket); System.out.println("Helper " + id + " added socket from outgoing: local port: " + helperSocket.getLocalPort() + " remote port: " + helperSocket.getPort()); } } System.out.println(this.helperSockets); } private class ConnectionListener extends Thread { public void run() { try { ServerSocket serverSocket = new ServerSocket(2222); /* Listen for connections from other helpers */ while (helperSockets.size() &lt; Common.NUM_HELPERS) { Socket helperSocket = serverSocket.accept(); // TODO Will indexof int in list of Integers work? int remotePort = helperSocket.getPort(); int connectedHelperID = Common.portMap.indexOf(remotePort); // TODO Does this really work? if (connectedHelperID == -1) { helperSockets.add(helperSocket); System.out.println("Helper " + id + " added socket from incoming: local port: " + helperSocket.getLocalPort() + " remote port: " + helperSocket.getPort()); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload