开发者

What are Async Sockets?

开发者 https://www.devze.com 2022-12-21 08:59 出处:网络
What are Async Sockets? How are the开发者_开发问答y different from normal sockets (Blocking and Non-Blocking)?

What are Async Sockets? How are the开发者_开发问答y different from normal sockets (Blocking and Non-Blocking)?

Any pointers in that direction or any links to tutorials will be helpful.

Thanks.


There are three ways to communicate with sockets in async way:

  1. Open regular socket, but do not read from it (because read() blocks) until you know there it something to be read. You can use select() or poll() to check whether there are data to read from socket(s), and if there is something, read it, as read() won't block.

  2. Switch socket to non-blocking I/O, by setting O_NONBLOCK flag with fcntl() function. In this case read() won't block.

  3. Set socket's O_ASYNC flag using FIOASYNC option of ioctl() (see man 7 socket for details). In this case you will receive SIGIO signal when there is something to read from socket.

Third approach is async socket.


Comparison of the following five different models for I/O in UNIX Network Programming: The sockets networking API would be helpful:

Blocking

Nonblocking

I/O multiplexing

Signal-driven I/O

Asynchronous I/O


If a server uses a synchronous socket, while it is waiting for data from the client, its main thread is blocked, so the server won't be doing anything... that is bad if you have multiple clients connecting. In an asynchronous socket, you CAN do other stuff while waiting for the client to send data to you, so now you CAN have multiple clients connecting to you

Synchronous uses a function like receive() which blocks until it gets a message

Asynchronous has beginReceive() endReceive() or similar functions. It uses callbacks, when a message is received, the callback is invoked

0

精彩评论

暂无评论...
验证码 换一张
取 消