1
votes

I need to change the IP address of a host from a client. I use UDP commands and a MulticastSocket to obtain the IP address of this host (currentIp) and use this IP address to successfully establish a TCP connection. The command to change this host IP address requires a DatagramSocket since I need to first get the host device MAC address to include in the change IP address command. Once the TCP connection is made I close the MulticastSocket UDP socket so I can open the DatagramSocket but get the following error:

java.net.BindException: Cannot assign requested address: Cannot bind

Is there something I need to do besides close the MulticastSocket socket before trying to get a DatagramSocket socket with the same port number, or is am I missing something else?

DatagramSocket socket;
private boolean ChangeIpAddress(String newIp) {
    DatagramSocket socket;
    try {
        socket = new DatagramSocket(30718, InetAddress.getByName(currentIp));
    } catch (SocketException ex) {
        ...
2

2 Answers

2
votes

It appears you are using a hostname with an incorrect IP address. You need to find you etc/hosts or where ever its defines.

0
votes

In my case, I have changed the listening address to 127.0.0.1 (localhost) and the problem is fixed