Hello! I've been trying to figure out how to do unix domain sockets in Nim, but I'm having issues. So far, what I have is:
import std/[net, files, paths, strutils]
let socketPath = "/tmp/something.sock"
Path(socketPath).removeFile()
let sck = newSocket(AF_UNIX, SOCK_STREAM, cast[Protocol](0))
sck.bindUnix(socketPath)
sck.listen()
var client: Socket
var address = ""
while true:
sck.acceptAddr(client, address)
echo "Client connected from: ", address
while true:
var line: string
sck.readLine(line)
line = line.strip()
if line == "quit":
client.close()
break
First of all, I'm not sure if I'm specifying the protocol the best way possible - it doesn't seem like it has a 0-valued variant, so I had to use a cast. And second - the readLine call is erroring out, with an "Invalid argument" OSError. Not sure what is happening there.
/usr/lib/nim/lib/pure/net.nim(1606) readLine
/usr/lib/nim/lib/pure/net.nim(1007) socketError
/usr/lib/nim/lib/std/oserrors.nim(92) raiseOSError
Error: unhandled exception: Invalid argument [OSError]