1

Given this:

. . .
ServerSocket serverSocket = new ServerSocket(1111);

while (helperSockets.size() < Common.NUM_HELPERS) {
    Socket helperSocket = serverSocket.accept();
. . .

This throws the exception:

new Socket("localhost", 1111, InetAddress.getLocalHost(), localPort);

Exception:

java.net.ConnectException: connect: Address is invalid on local machine, or port is not valid on remote machine

I'm trying to use this constructor to set the local port, so what am I doing wrong? Source below.

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.

public class DisSemHelper extends Thread {
private int id;
private int semaphore;
private Clock clock;
private Vector<Socket> 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<Socket>();
    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() < 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() <= 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() < 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();
        }
    }
}

3 Answers 3

1

This is a WAG, but I'd try the other constructors in Socket.

Try new Socket(INetAddress.getLocalHost(), "111");

See:

http://ashishmyles.com/tutorials/tcpchat/index.html

Sign up to request clarification or add additional context in comments.

3 Comments

Yea my bad. The exception is added to my question above. Forgot to print stack trace.
Yes, that one works actually. But I still need to set local port.
Not sure what you mean about setting the local port. Sorry I couldn't be more helpful. \
1

Can you make a simple test-case and post the source? That'll help us troubleshoot this better.

new Socket("localhost", 1111, InetAddress.getLocalHost(), localPort);

What exactly are you trying to do here? Why do you need to define what local host/port pair to use locally? A client should normally just specify the remote host/port and let the OS pick the local port. This is perhaps the problem.

4 Comments

I'm doing it this way because I have clients connecting to each other, keeping track of who they're connected to (using port numbers). The server socket is on localhost for testing; it could later be on a different machine.
Thats fine. But my question still remains valid. Why do you have to pick the 'localPort'? Clients can connect to each other without you specifying the localPort to use to connect to the server (in your case it'll probably is another client). If you show us your entire code or a test-case as I mentioned we can debug easier. Also, you probably would get that exception if you are reusing the 'localPort' in a short period of time.
You can still track the remote host/port combo in a hash. Take a look at my answer on your other post hopefully that'll clarify my thoughts better.
I'm having problems along these lines. Please see stackoverflow.com/questions/8207260/…
0

Not a true solution, but I ended up working around setting the local port (because nobody seems to like that idea).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.