Skip to content

Commit 0eb73cc

Browse files
committed
Added blocking and non-blocking sockets
1 parent 63c094a commit 0eb73cc

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
11
# Blocking & Non-Blocking Sockets
2+
Sockets can operate in two modes namely blocking or non-blocking. When a blocking socket performs an I/O operation, the program will pause and wait until the operation is completed. But in case of non-blocking sockets the program will immediately return with an error, even if the operation is not completed. For example, during `send()` the data from user buffer is copied to kernel buffer, But if the kernel buffer is full, then in case of blocking sockets the program will gets blocked (it means the program gets paused and will be in waiting state) until space is available in the kernel buffer. In case of non-blocking sockets if kernel buffer is full and `send()` cant be performed, then it will immediately return with an error instead of waiting until space becomes available.
3+
4+
Generally all the sockets created using `socket()` system call are in blocking mode. In Unix like systems, fcntl system call can be used to set a socket to non blocking mode. After `socket()` creation add the `O_NONBLOCK` flag using `fcntl()` to make the socket non blocking.
5+
```c
6+
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
7+
int flags = fcntl(sockfd, F_GETFL, 0);
8+
if (flags == -1) {
9+
perror("fcntl F_GETFL");
10+
exit(EXIT_FAILURE);
11+
}
12+
if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) == -1) {
13+
perror("fcntl F_SETFL O_NONBLOCK");
14+
exit(EXIT_FAILURE);
15+
}
16+
```

0 commit comments

Comments
 (0)