LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   blocking socket Vs non-blocking socket (https://www.linuxquestions.org/questions/linux-software-2/blocking-socket-vs-non-blocking-socket-799070/)

barunparichha 03-31-2010 04:06 AM

blocking socket Vs non-blocking socket
 
My question is quite simple:
What are type of applications where socket in blocking mode can be used ?
And where non-blocking mode to be used ??


TCP is blocking by default.

pantalone 04-02-2010 07:23 AM

"Blocking" simply means that when you call a function, it does not return until it has completed its job. "Non-blocking" means that the function returns right away, even if it was not able to complete its mission. There are good reasons to prefer one mode or another, depending on the requirements of your program.

For example, if your program is an FTP client, and you are uploading a file to an FTP server, you might use blocking mode. When you call the send() function to transfer a block of data, you want to know that when the function returns, all of that data is on its way to the server. Because your program has nothing else to do except upload the file, you don't care if it takes some amount of time before the function returns.

On the other hand, if your program is an FTP server, you might have to deal with dozens of client connections, all trying to transfer files at the same time. In that case, you might use non-blocking mode. When you call the recv() function to receive data from client A, you don't necessarily want to wait until all the data arrives, because you have other clients that need your attention. With non-blocking mode, the call to recv() will return right away, even if there was no data waiting to be received, and you can try to receive data from a different client. You can use the select() function to check each connection to see which ones have data that is ready to be received.

In short, you would probably use non-blocking mode in cases where you need to do some kind of multi-tasking and you don't want to wait if the hardware or the remote host isn't ready yet.

Aquarius_Girl 04-02-2010 07:38 AM

pantalone

Thanks for replying, I had the same question !


All times are GMT -5. The time now is 02:23 AM.