Using the net module, how does one really disconnect a Socket. The following code
# disconn.nim
import strutils
import net
var socket = newSocket()
socket.bindAddr(Port(8088),"127.0.0.1")
var host = repeat('A',128)
socket.listen()
# ACCEPT JUST ONE HTTP request
echo("waiting")
var client = newSocket()
socket.acceptAddr(client,host)
var line = repeat('B',512)
while true:
client.readLine(line)
if line == "\r\n": break
client.send("HTTP/1.1 400 NOT FOUND\r\n")
client.close()
socket.close()
echo "Closed Socket"
seems to work, but running the disconn program quickly twice in a row (using a browser to request url http://localhost:8088/ produces the following ubuntu error message for the second run:
Traceback (most recent call last)
disconn.nim(6) disconn
net.nim(381) bindAddr
os.nim(135) raiseOSError
Error: unhandled exception: Address already in use [OSError]
About a minute wait between runs avoids the error message. I realize that Linux takes a while to close an improperly terminated TCP connection - but similar code in go did not have this problem.
I do not see a disconnect proc in the net module. What can be done?
Yes - that worked. Many Thanks to @dom96 .
BTW why are there module net errors during the compile???
lib/pure/net.nim(13, 37) Warning: unsigned is deprecated [Deprecated]
lib/pure/net.nim(135, 7) Warning: Cannot prove that 'result' is initialized. This will become a compile time error in the future. [ProveInit]
I will ignore them - hoping future releases will remove them. OR --- am I doing something wrong?
Again, Thanks for the help. This forum is MUCH more helpful than stackoverflow!