Back to Message Handlers (.z Namespace)

Interprocess Communication (IPC) · Message Handlers (.z Namespace) · Practice Question

Capture a Connecting User's IP Address and Username

Sign in to track your progress

Question

How can a server capture and record a connecting user's IP address and username at the moment they connect?

Solution

Override .z.po on the server:

/ Server
\p 1234
.z.po:{ show `handle`username`ipaddr!(.z.w;.z.u;.z.a) }
/ Client
h:hopen `:localhost:1234:jeremy:password

When the client connects, the server prints something like:

handle  | 4i
username| `jeremy
ipaddr  | 2130706433i

Explanation

.z.po fires once per connection, immediately after authentication succeeds. At that point the three .z variables are populated with the connecting client's details:

  • .z.w — the handle assigned to this connection
  • .z.u — the username the client supplied
  • .z.a — the client's IP address as an int (the 4-byte IPv4 address packed into a single 32-bit number)

.z.a is an integer, but can be converted to a readable symbol in dotted form:

`$"." sv string 256 vs .z.a
/ `127.0.0.1

256 vs x converts an integer to its base-256 representation — which is exactly the four byte-sized octets that make up an IPv4 address. string turns each octet into a string, "." sv joins them with dots, and `$ converts the result to a symbol.

Storing handle, username, and IP in a dictionary or table at connection time gives the server a live session registry — useful for logging, auditing, or per-user access control checked later in .z.pg or .z.ps.