saman
saman

Reputation: 311

how to manually assign port for URL in java

I have a java client application which uses URL and URLConnection objects in order to call another java servlet and get the result. while making a URLConnection, it would assign a random port number to our client, but I would like to explicitly specify a port number for it to acquire. I'm aware that this is OS responsibility to do such a thing, but I'm wondering is there any way around to make it happen?

Upvotes: 1

Views: 454

Answers (2)

K139
K139

Reputation: 3669

It's not possible with URL, but with Socket you could do that.

        Socket socket = new Socket();
        socket.bind(new InetSocketAddress("address", 6670));

        // Now you can connect to any Server you want'
        socket.connect(new InetSocketAddress("Server",80)); 

And also make sure to use the IP of your machine instead of "127.0.0.1" address.

Upvotes: 2

igreenfield
igreenfield

Reputation: 1702

It is not possible at all, as far as I know even with C code you can do that.

Upvotes: 1

Related Questions