aspose file tools*
The moose likes Java in General and the fly likes cant create a connection keep getting a NotYetConnectedException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "cant create a connection keep getting a NotYetConnectedException" Watch "cant create a connection keep getting a NotYetConnectedException" New topic
Author

cant create a connection keep getting a NotYetConnectedException

Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 582
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

Joined: Jun 29, 2002
Posts: 582


Also fails
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14420
    
  23

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 582
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: cant create a connection keep getting a NotYetConnectedException