-
Nonblocking I/O
Hi
I am trying to scan a network and i read that it is better to use nonblocking I/O for this (from the fyodor's nmap.txt), since linear socket I/O may take lots of time! With nonblocking I/O, I am able to use multiple socket at the same time.
What is the main difference between a linear and nonblocking I/O? In action and in coding point of views...
Thanks in advice...
-
As far as basic I/O theory goes a blocking operation will return (either with some data or after some data has been received and handled) only once the event has occured (i.e write,read,etc). So if it expecting to read something in, it will halt until it has read something.
This is good in that you can handle your data synchronously, but bad in the sense that it will stop the rest of the process from executing (and possibly hang your program).
If you were to use a non blocking call, then you would simply send a message to the interface that you expect a response from, and hope it gets back to you. This asynchronous behaviour will stop an I/O call from holding up the rest of the process. So your process can continue on through the code doing whatever. I guess what makes it a little more complicated is that you need a mechanism to acknowledge this response once you have sent off the request (This would be done either automatically for you, or you may have to implement a method of handling this response).
Ray
-
Your web browser, for instance, uses non-blocking I/O. If it used standard (blocking) I/O then the whole browser interface would freeze up while it was trying to connect to a website and get the data. There'd be no use for a Stop button because you'd never be able to use it.
-
I think BeeJ guide speaks comprehensibly about the topic - http://beej.us/guide/bgnet/output/html/
-
Thanks for all advice...
I read beej's guide and realize that i have to concentrate on select(2) system call. I read the man page and see that there is a very useful simple example about usage of select(2).
So, the example in the man page make the concept more clear for me, I do love Linux :)