4

While using Datagram Channel I get a PortUnreachableException. This is what my Codes look like : This is the sender side

//Open a non-blocking socket to send data to Receiver
DatagramChannel channel = DatagramChannel.open();
channel.configureBlocking(false);
channel.socket().bind(new InetSocketAddress(10000));
channel.connect(new InetSocketAddress(host,UDPort));

It is this code that gives me : java.net.PortUnreachableException. The parameter "host" is set as:

String host = new String("192.168.1.3");

The Receiver side is this

//Open a Socket to listen for incoming data
DatagramChannel channel = DatagramChannel.open();
channel.connect(new InetSocketAddress(UDPort));
channel.configureBlocking(false);
ByteBuffer buffer =   ByteBuffer.allocate((recvpkt[0].length)*4);
System.out.println("Waiting for packet");
channel.receive(buffer);
System.out.println("Received packet");

I cannot understand why I am getting this exception. I have looked up examples on net and this is how they all suggest the code should be.

UPDATE 1:

As pointed out in the comment by shazin, the binding needs to be done at Receiver and connection at Sender. The updated code for Sender is:

DatagramChannel channel = DatagramChannel.open();
channel.configureBlocking(false);
channel.connect(new InetSocketAddress(host,UDPort));

For the Receiver:

DatagramChannel channel = DatagramChannel.open();
channel.configureBlocking(false);
channel.socket().bind(new InetSocketAddress(host,UDPort));

Now the problem is that if "host" is set as "localhost" the program works but if we pass an IP say 10.132.0.30 as "host" the java.net.PortUnreachableException occurs. While the channel.isConnected() option return "true" the channel.write(buffer) command gives an Exception.

UPDATE 2:

The PortUnreachableException is now gone. The only difference in the code now is that I am using selectors to accept connections on the Receiver side. I still do not understand why the error came when selectors were not being used. If anyone stumbles on this question and knows, do post your answer.

Aditya
  • 195
  • 3
  • 4
  • 12
  • 2
    What is the value for UDPort? And why are you binding the sender? You should bind the reciever and connect from the sender isn't it? – shazin Nov 14 '12 at 06:05
  • Thanks for pointing it out. I removed the binding from the sender side and carried it out on the receiver side. The connection now occurs but there is a caveat. It connects if "host" is set as "localhost" not "192.168.1.3". Why does an IP address not connect. Does IP address need to be passed in a format other than String ? – Aditya Nov 14 '12 at 06:13
  • Seems like you are testing both sender and receiver in the same machine. Are you sure the about the IP of the machine? – shazin Nov 14 '12 at 06:21
  • Yeah currently testing on same machine. Found IP using ifconfig. So should be correct. – Aditya Nov 14 '12 at 06:26

2 Answers2

1

The code is correct but the port really is unreachable. It's a network connectivity problem, or a no-listener problem, not a coding problem.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thnx for the reply. I just tried out setting the host string as "localhost" instead of "192.168.1.3". In that case, the connection occurs. Any thoughts on why an IP address fails but a name works ? Because when I do connect to a remote machine, I will need to pass in the IP address of that machine. – Aditya Nov 14 '12 at 06:09
  • 'The connection occurs' but does the data get through? Fundamentally either the IP address is unreachable - check with `ping(1)` - or nothing is listening at that port. – user207421 Nov 14 '12 at 09:06
  • The data goes through correctly in case "localhost" is passed as "host" instead of the IP address. But when an actual IP address is used, I get the java.net.PortUnreachbaleException. By the way, there is no need to use selectors right, as I have only one Sender and only one Receiver. Any idea about why I get an exception ? channel.isConnected() returns "true" in case of an IP address but channel.write(buffer) or channel.send(buffer,address) give this exception. – Aditya Nov 15 '12 at 09:47
  • @Aditya UDP connect does nothing on the network, so it can never actually fail, unless the arguments are invalid, e.g. 0.0.0.0. Remove the host parameter when binding: just bind to the port. That binds you to 0.0.0.0, which means all interfaces. – user207421 Nov 18 '12 at 22:07
1

Try using the following to get the Ip Address

channel.connect(new InetSocketAddress(InetAddress.getByName(host),UDPort));

UDPort must be equal to the port you are using to Bind in Receiver.

shazin
  • 21,379
  • 3
  • 54
  • 71