Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cant create a connection keep getting a NotYetConnectedException

 
Tony Evans
Ranch Hand
Posts: 598
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this code is the problem



I have used "localHost" the hostName and the IP address (IPv4 address)



I just wonder if its the windows 7 ops sustem thats causing this problem
 
Tony Evans
Ranch Hand
Posts: 598
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Also fails
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're using non-blocking I/O (in line 10 you're setting it to non-blocking). When you do that, most methods, such as connect() in line 11, return immediately, before the actual connection is made. Setting up the connection happens in a background thread. It's very well possible that when your code reaches line 14, the connection has not yet been set up, so at that point you see that it's not yet connected.

Non-blocking I/O is a specialized and complicated subject. Read the API documentation of methods such as SocketChannel.connect() very carefully - it explains you exactly how those methods behave in case of blocking and non-blocking mode.

I'd advise you to first make a program work with blocking I/O, which is easier to understand. Comment out line 10, or change it to:

If you really want to use non-blocking I/O, you'll have to study how that works exactly first.
 
Tony Evans
Ranch Hand
Posts: 598
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper de Jong will read up a little more on non blocking, its a pity that all the non blocking examples are written the way i coded.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic