0

As a network client I would like to follow the input from two TCP connections. Both servers send a few packets per second. Each packet is small in size when compared to the available bandwidth.

Ideally we'd like to read both sockets from a single thread for the best latency. My initial plan was to use FIONREAD from ioctl(2) on each connection in a loop, and only read when data is available. Are there other options? CPU consumption is less relevant than low latency.

10
  • 2
    Use select() and non-blocking mode. Commented Mar 17, 2024 at 21:17
  • 2
    Or use poll (if you are on a Posix system). Commented Mar 17, 2024 at 21:51
  • 1
    @ikegami You do actually. This is a common misconception. It appears as though you don't, but in practice numerous subtle errors are possible if you use select() with blocking-mode sockets. Commented Mar 18, 2024 at 4:14
  • 1
    @ikegami accept() can return null. read() and write() can return -1/EAGAIN/EWOULDBLOCK. How many do you need? Commented Mar 18, 2024 at 7:03
  • 1
    @user207421 Re "accept() can return null.", So? /// Re "read() and write() can return -1/EAGAIN/EWOULDBLOCK.", well yeah, which is why you use select to let you know when it wouldn't return that. Commented Mar 18, 2024 at 13:09

1 Answer 1

1

You want to use one of these I/O multiplexing primitives:

  • Non-blocking sockets which would give you similar behavior as your ioctl(2). Usually, it's combined with one of the options below to avoid a busy loop.
  • select(); "On Linux [...] it may be safer to use O_NONBLOCK on sockets that should not block"
  • poll()
  • epoll() (Linux)
  • POSIX aio_ family of functions. Not sure it's used much and Linux the library spawns threads.
  • uring (Linux)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.