Java networking

Associate
Joined
18 Feb 2010
Posts
940
Hi,

I am fairly new to the networking side of java, and I am stuck with something very straight forward. I can make a connection within my local network, but want to get my IP address so that a connection can be made externally.

I have the lines:
Code:
ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(0);
            System.out.println("My IP is: " + ServerSocket.getInetAddress().getHostAddress());
            System.out.println("Listening on port: " + serverSocket.getLocalPort());
            
        } catch (IOException e) {
            System.err.println("Could not listen on port: PORT NAME");
            System.exit(1);
        }
        }

I am trying to get it so that I have a port and IP to be able to connect to this using telnet. At the moment at this very basic level all I require is to be able to send some commands over this connection.

The result of my code is:
( it has to listen on a random port because of networking issues we have here, that's no problem, I can manually enter that into telnet until that is fixed - Uni stuff ).

My IP is: 0.0.0.0
Listening on port: 52516

This is for part of my coursework, but I have tried, I do try, and I am a good student. It's only a tiny tiny part of a coursework. All I really need to work out is how to get my IP?
I can getLocalHost which will return my local IP, but of course that is no use outside of the network here.

Thanks!!
 
You want a client to connect to a remote server?

Wouldn't something like this work in the client main args?

Socket soc=new Socket("0.0.0.0",52516);

although have a proper IP for the server.
 
@ Lemonade, yes, that would work.. it's getting the IP for the server which this bit of code is meant to be doing. This part of code is part of the server.

@Deiwos, very close! Nottingham uni :).
 
Back
Top Bottom