Back to Opening Connections (hopen)

Interprocess Communication (IPC) · Opening Connections (hopen) · Practice Question

What Value Should a Handle Have, and Why?

Sign in to track your progress

Question

What value should h have after a successful hopen, and why?

Solution

A positive integer, greater than 2 — for example 4i or 5i.

Explanation

A connection handle is the operating system's file descriptor for the socket. The first three descriptors are always taken by the process's standard streams, which q also exposes as handles:

  • 0 — the console (standard input)
  • 1 — standard output
  • 2 — standard error

So a handle returned by hopen is always greater than 2 — in practice usually a little higher still, since the process may already have other files or sockets open (such as its own listening port). hopen never returns 0, 1 or 2 to signal failure: if the connection can't be made, it signals an error instead.