What are Async Sockets?

What are Async Sockets? How are they different from normal sockets (Blocking and Non-Blocking)? Any pointers in that direction or any links to tutorials will be helpful. Thanks.

asked Feb 25, 2010 at 5:35 24.7k 25 25 gold badges 97 97 silver badges 142 142 bronze badges

3 Answers 3

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.

1,657 1 1 gold badge 15 15 silver badges 33 33 bronze badges answered Feb 25, 2010 at 7:15 34.7k 14 14 gold badges 57 57 silver badges 90 90 bronze badges

There are also, likely, other platform specific ways to use sockets asynchronously such as using Overlapped I/O and I/O completion ports on Windows platforms.

Commented Feb 25, 2010 at 8:29

@Len Sure. Even these are not cross-platform, as fcntl() may have different options on different platform, and ioctl() isn't standard and very much platform-dependent.

Commented Feb 25, 2010 at 12:42 Is there a platform independent option? Commented Mar 7, 2013 at 10:47

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

answered Feb 25, 2010 at 10:49 1,045 2 2 gold badges 9 9 silver badges 14 14 bronze badges

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

answered Feb 25, 2010 at 5:41 7,375 1 1 gold badge 33 33 silver badges 51 51 bronze badges

Linked

Related

Hot Network Questions

Subscribe to RSS

Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2024 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2024.9.4.14806