Search code examples
javaniotftp

TFTP client based on java NIO


I am trying to implement TFTP client using Java NIO. But it leads to error:

network error: Address already in use: bind

Code snippet is shared here.

Selector selector = Selector.open();
DatagramChannel channel = DatagramChannel.open();
InetSocketAddress isa = new InetSocketAddress("10.86.4.250",69);
channel.socket().bind(isa);
channel.configureBlocking(false);

As I am new to this networking concept, I couldn't understand the cause. Any help in resolving this issue is highly appreciated.


Solution

  • If you're developing a client, you should .connect() to a socket, rather than bind()'ing (that's for the server), eg.:

    DatagramChannel channel = DatagramChannel.open();
    channel.connect( new InetSocketAddress( "10.86.4.250" , 69 ) );
    ...
    

    Cheers,