aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes non-blocking socket NIO Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "non-blocking socket NIO" Watch "non-blocking socket NIO" New topic
Author

non-blocking socket NIO

Daniil Sosonkin
Ranch Hand

Joined: Jan 15, 2004
Posts: 76
Hi,

I'm wondering if anyone can point out some of the tutorials on the net about using NIO for non-blocking sockets. So far I've been out of luck and this stuff is quite different from regular sockets. Also, have anyone had any experience with NIO? I need to write this application which will send out hundreds of messages every second to dozens of clients. Hoping that NIO would do the trick.

Thanks in advance
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Daniil Sosonkin:
Hi,

I'm wondering if anyone can point out some of the tutorials on the net about using NIO for non-blocking sockets. So far I've been out of luck and this stuff is quite different from regular sockets. Also, have anyone had any experience with NIO? I need to write this application which will send out hundreds of messages every second to dozens of clients. Hoping that NIO would do the trick.

Thanks in advance


There is a thread on the Sun Java forums that provides a lot of information on non-blocking NIO. I started that thread last year but may others have contributed.

Taming the NIO Circus

You might also look at Ron Hitchings Java NIOpublished by O'Reilly.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
pk wooster Welcome to the Ranch! I hope you find it a nice place.

--------------------------------------------------------------

Daniil please take a look at this thread

and

NIO Examples in the SDK Documention.
[ June 15, 2004: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
Daniil Sosonkin
Ranch Hand

Joined: Jan 15, 2004
Posts: 76
Thank you guys, those are wonderful pages. One more question, maybe you can point out on technque of making sure all my data is being sent out and received? Say if I need to send out: (int)(byte)(byte)(double)(int). And at the other end I need to receive them in the same sequence. How would a receiving end know when its all received? Or how a sending end would know when its all sent out. Come to think of it, what happens if I send data too fast?

Thanx
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Daniil Sosonkin:
Thank you guys, those are wonderful pages. One more question, maybe you can point out on technque of making sure all my data is being sent out and received? Say if I need to send out: (int)(byte)(byte)(double)(int). And at the other end I need to receive them in the same sequence. How would a receiving end know when its all received? Or how a sending end would know when its all sent out. Come to think of it, what happens if I send data too fast?

Thanx
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Daniil Sosonkin:
Thank you guys, those are wonderful pages. One more question, maybe you can point out on technque of making sure all my data is being sent out and received? Say if I need to send out: (int)(byte)(byte)(double)(int). And at the other end I need to receive them in the same sequence. How would a receiving end know when its all received? Or how a sending end would know when its all sent out. Come to think of it, what happens if I send data too fast?

Thanx


Sorry, I'm still new to this forum, here's the actual reply.

I've posted a new set of NIO examples on the SUN forums. These provide a NIO Server and a NIO Client that implement a simple 1 to many message switch, a bit like chat. I also provided multithreaded examples.
peter wooster
Ranch Hand

Joined: Jun 13, 2004
Posts: 1033
Originally posted by Daniil Sosonkin:
One more question, maybe you can point out on technque of making sure all my data is being sent out and received? Say if I need to send out: (int)(byte)(byte)(double)(int). And at the other end I need to receive them in the same sequence. How would a receiving end know when its all received? Or how a sending end would know when its all sent out. Come to think of it, what happens if I send data too fast?

Thanx

The NIO client and server that I refered you to in my previous post uses the shutdownOutput method to ensure that the client has received all data before closing the connection. This is discussed in those topics. The only sure way to know that your partner has received your data is to implement some kind of application level acknowledgement. The standard way is to send a message requiring an ack when you are done sending data and then wait for the ack to return. In this respect NIO is no different from the blocking stream based I/O.

When it comes to detecting problems caused by sending too fast, NIO will not send all that you requested if its buffers are full. The byte count returned by write will be less than requested and the buffer will still have hasRemaining true. When this happens you must add OP_WRITE to the interstOps and not do another write until it is selected again. See those examples and the NIO Circus for working versions of this.
Kris Adams
Greenhorn

Joined: Jul 02, 2004
Posts: 17
Hey Dan
I used nio in my project and it works well. See the examples in this link. It shud help..

http://javaalmanac.com/egs/java.nio/pkg.html

SJCP
Kris Adams
Greenhorn

Joined: Jul 02, 2004
Posts: 17
This might work. Write the data length and then the data to the stream. When reading at the other end, read the length and then length num of bytes. If that much data is not ready, either block or timeout depending on your need. I did this in my stuff as i was sending varying length messages each time.

~krishna
SJCP
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: non-blocking socket NIO