• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

non-blocking socket NIO

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Daniil Sosonkin
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1033
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
reply
    Bookmark Topic Watch Topic
  • New Topic