Reputation: 253
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
Reputation: 21419
Good practice: - Never specify a source port if there is no need to do so.
Upvotes: 0
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
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
Reputation: 6368
Use 0 for your scrPort
. This will allow the OS to choose an open port when you bind the socket.
Upvotes: 0