Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8839315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:12:44+00:00 2026-06-14T10:12:44+00:00

While using Datagram Channel I get a PortUnreachableException . This is what my Codes

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:12:45+00:00Added an answer on June 14, 2026 at 10:12 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a java NIO Datagram Channel(in blocking mode). I want to transmit
While using a Root- and SubResource in Jersey I annotated the RootResource like this
While using this line of code, I get the error CS1040: Preprocessor directives must
While using Google chrome if I shut down it unexpectedly. like turn of pc
While using the vector why do we sometime use the operator[] like homework[mid] but
while using the following constructor $this->pdo = new PDO ($this->source, $this->username, $this->password); if I
I want to transmit an int array over a network using Datagram Channel in
While using some regex in C#, I'm facing the following problem : Consider this
While using the maven-buildnumber-plugin 1.0 beta 4, it seems that I can get the
While using boost::threads I have come across this interruption problem. When I do a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.