Question

I am writing a program with TCP sockets connection between client and server. When the server starts I want to display the IP and port that clients need to use to connect, and when client connects I want the server to show what IP did the client connect from. I am getting confused which command should i use to each of those:

getInetAdress()

getLocalAdress()

getRemoteSocketAdress()

edit

I earlier used int port = 1234 and String IP = "localhost" to test and it worked, but I only used it on one PC, so I think localhost will not work if i start server and client on different computers.

This is server side:

int port = 1234;

...

public void start() {
        keepRunning = true;
        // create socket
        try {
            ServerSocket server = new ServerSocket(port);
            while (keepRunning) {
                display("Waiting for client connections on "
                        + server.getInetAddress().getLocalHost()
                                .getHostAddress() + ":" + port);
                Socket conn = server.accept();
                if (!keepRunning)
                    break;

                ClientThread t = new ClientThread(conn);
                cList.add(t);
                t.start();

And this is client:

int port = 1234;
String IP = "localhost";
//these variables can be changed from Client GUI before making connection

...


public boolean start() {
    try {
        socket = new Socket(IP, port);
    } catch (Exception e) {
        display("Error connectiong to server:" + e);
        return false;
    }
    try {
        sInput = new ObjectInputStream(socket.getInputStream());
        sOutput = new ObjectOutputStream(socket.getOutputStream());
    } catch (IOException e) {
        display("Exception creating new Input/output Streams: " + e);
        return false;
    }

When i start the server,

display("Waiting for client connections on " + server.getInetAddress().getLocalHost().getHostAddress() + ":" + port);

return this:

Waiting for client connections on 192.168.1.104:1234

which is kind of what i want, but I still cant get it to show me the port. 1234 is a fixed value i used, but I want to use ServerSocket server = new ServerSocket(0); to asign port dynamically, then when i start the client i just put in the values that i got from server and connect.

I tried to use server.getLocalPort() in the display line in server and it returned 55410 or something like that, but when i put this port in client to make connection, it doesn't work. I get Error connectiong to server:java.net.ConnectException: Connection refused: connect. from client

Was it helpful?

Solution

To get the current port that the ServerSocket is listening to, use getLocalPort();

http://download.java.net/jdk7/archive/b123/docs/api/java/net/ServerSocket.html#getLocalPort%28%29

getLocalPort

public int getLocalPort()

Returns the port number on which this socket is listening.

If the socket was bound prior to being closed, then this method will continue to return the port number after the socket is closed.

Edit: Just saw your edit. Are you trying to connect by explicitly referencing the IP and Port? If so, and it's still failing, your server machine might be running a firewall. I'd check for that first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top