unixsockettest.nim 552 B

123456789101112131415161718192021222324252627
  1. import std/[assertions, net, os]
  2. let unixSocketPath = getCurrentDir() / "usox"
  3. removeFile(unixSocketPath)
  4. let socket = newSocket(AF_UNIX, SOCK_STREAM, IPPROTO_NONE)
  5. socket.bindUnix(unixSocketPath)
  6. socket.listen()
  7. var
  8. clientSocket: Socket
  9. data: string
  10. socket.accept(clientSocket)
  11. clientSocket.readLine(data)
  12. doAssert data == "data sent through the socket"
  13. clientSocket.send("Hello from server\c\l")
  14. clientSocket.readLine(data)
  15. doAssert data == "bye"
  16. clientSocket.send("bye\c\l")
  17. clientSocket.close()
  18. socket.close()
  19. removeFile(unixSocketPath)