Skip to content

Instantly share code, notes, and snippets.

@BitPuffin
Last active August 29, 2015 13:56
Show Gist options
  • Save BitPuffin/8984557 to your computer and use it in GitHub Desktop.
Save BitPuffin/8984557 to your computer and use it in GitHub Desktop.
import asyncio, sockets, logging
var socket: PAsyncSocket
var port: int
proc setupSocket() =
debug("Creating asynchronous socket")
socket = asyncsocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP, false)
doAssert(socket != nil, "Socket creation was unsuccessful")
debug("Done.")
debug("Binding socket to port")
const defaultPort = 13337
const highestPort = 13400
for port in defaultPort..highestPort:
try:
debug("Trying port: $#", port)
socket.bindAddr(TPort(port))
port = port # This part
break
except EOS:
let msg = getCurrentExceptionMsg()
error(msg)
if port == highestPort:
let msg = getCurrentExceptionMsg()
error("Tried all ports, failure: ", msg)
raise newException(EOS, getCurrentExceptionMsg())
else:
continue
proc serve*() =
info("Starting server..")
setupSocket()
echo "yay"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment