Bind Socket to specific ip adress

I'm trying to bind a Socket to a specific interface (IP-Adress) but with a port number

chosen by the underlying operating system... Unfortunately, the Socket constructor

only accepts a local ip address in combination with a specific port...

Chosing a random port does not help since the port might already be in use by another program.

Using the same port over and over again also does not work, since my program runs only a short time and then closes the socket and exits. If I restart my program, I get the Exception "Cannot assign requested address for fd: n" and the port may not be used for about half a minute.

Is there a way to construct a socket bound to a specific ip address but letting the OS select

a port number?

[783 byte] By [mMa] at [2007-9-26 3:37:40]
# 1
Do you use java.net.Socket?I use this in my code to connect to a specific IP address, which is not a local IP. The port that I use is always the same port. In my code I build a new Socket, perform a task and then close the Socket.I hope this helps.Robert
EsoralTrebor at 2007-6-29 12:10:19 > top of Java-index,Archived Forums,Java Programming...
# 2

> Do you use java.net.Socket?

>

Yes, I do...

The point is that I want to connect to a system that only accepts connections

from a specific IP address , therefore I have to bind to this address.

Let's assume that my client machine has two IP-adresses (192.168.0.1,192.168.0.2)

and the server has the address 192.168.0.3. The server only accepts connections

from 192.168.0.2. This means, that the client Socket must be bound to 192.168.0.2,

otherwise the server doesn't accept the connection.

mMa at 2007-6-29 12:10:20 > top of Java-index,Archived Forums,Java Programming...
# 3
Okay, I get what you mean now. Sorry, but I have no idea how to do that. Have you searched the forum for previous postings that have to do with binding? Good luck.
EsoralTrebor at 2007-6-29 12:10:20 > top of Java-index,Archived Forums,Java Programming...
# 4

Hey,

I'm working on a program, that has some of the same ideas as your yours, but I'm only using 2 ports on the same address ( download, upload). For my situation I create a new object each time there is a new user

" asking for that port ".

If this sounds like what you need, reply with a bit more info on what you need, and I would help ya out!!

paynepro at 2007-6-29 12:10:20 > top of Java-index,Archived Forums,Java Programming...