xtrem06
xtrem06

Reputation: 253

UDP bind failure

I'm trying to connect to a remote server, but i get a BindException when i instanciate the DatagramSocket with a remote server address. It works on localhost.

dstAddress = new InetSocketAddress(server, servPort);
srcAddress = new InetSocketAddress(srcPort);

try{
sock = new DatagramSocket();
sock.setReuseAddress(true);
} catch (SocketException ex) {

}

public void connect() {
sock.bind(srcAddress);
sock.connect(dstAddress);
}

Upvotes: 0

Views: 1240

Answers (5)

GETah
GETah

Reputation: 21419

Good practice: - Never specify a source port if there is no need to do so.

Upvotes: 0

Adam Zalcman
Adam Zalcman

Reputation: 27233

You don't need to call bind() or connect(). The source port is chosen in the DatagramSocket constructor (as you have already done) and the destination address and port are set in each DatagramPacket you send.

See this for example.

Upvotes: 0

Erik Ekman
Erik Ekman

Reputation: 2066

You don't have to call bind() at all. A port was already chosen for you when the DatagramSocket was created. connect() is also not required, you can choose to set the destination address in each DatagramPacket instead.

Upvotes: 1

philipvr
philipvr

Reputation: 6368

Use 0 for your scrPort. This will allow the OS to choose an open port when you bind the socket.

Upvotes: 0

user207421
user207421

Reputation: 310909

The source port is already in use.

Upvotes: 0

Related Questions