std/net: show addr+port on address already in use error; code-block => runnableExamples (#18428)
* std/net: show addr+port on address already in use error; code-block => runnableExamples * var=>let
This commit is contained in:
parent
d1d2498c7b
commit
ffa638ed50
1 changed files with 37 additions and 43 deletions
|
|
@ -44,46 +44,44 @@
|
||||||
## After you create a socket with the `newSocket` procedure, you can easily
|
## After you create a socket with the `newSocket` procedure, you can easily
|
||||||
## connect it to a server running at a known hostname (or IP address) and port.
|
## connect it to a server running at a known hostname (or IP address) and port.
|
||||||
## To do so over TCP, use the example below.
|
## To do so over TCP, use the example below.
|
||||||
##
|
|
||||||
## .. code-block:: Nim
|
runnableExamples("-r:off"):
|
||||||
## var socket = newSocket()
|
let socket = newSocket()
|
||||||
## socket.connect("google.com", Port(80))
|
socket.connect("google.com", Port(80))
|
||||||
##
|
|
||||||
## For SSL, use the following example (and make sure to compile with `-d:ssl`):
|
## For SSL, use the following example:
|
||||||
##
|
|
||||||
## .. code-block:: Nim
|
runnableExamples("-r:off -d:ssl"):
|
||||||
## var socket = newSocket()
|
let socket = newSocket()
|
||||||
## var ctx = newContext()
|
let ctx = newContext()
|
||||||
## wrapSocket(ctx, socket)
|
wrapSocket(ctx, socket)
|
||||||
## socket.connect("google.com", Port(443))
|
socket.connect("google.com", Port(443))
|
||||||
##
|
|
||||||
## UDP is a connectionless protocol, so UDP sockets don't have to explicitly
|
## UDP is a connectionless protocol, so UDP sockets don't have to explicitly
|
||||||
## call the `connect <net.html#connect%2CSocket%2Cstring>`_ procedure. They can
|
## call the `connect <net.html#connect%2CSocket%2Cstring>`_ procedure. They can
|
||||||
## simply start sending data immediately.
|
## simply start sending data immediately.
|
||||||
##
|
|
||||||
## .. code-block:: Nim
|
runnableExamples("-r:off"):
|
||||||
## var socket = newSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
|
let socket = newSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
|
||||||
## socket.sendTo("192.168.0.1", Port(27960), "status\n")
|
socket.sendTo("192.168.0.1", Port(27960), "status\n")
|
||||||
##
|
|
||||||
## Creating a server
|
## Creating a server
|
||||||
## -----------------
|
## -----------------
|
||||||
##
|
##
|
||||||
## After you create a socket with the `newSocket` procedure, you can create a
|
## After you create a socket with the `newSocket` procedure, you can create a
|
||||||
## TCP server by calling the `bindAddr` and `listen` procedures.
|
## TCP server by calling the `bindAddr` and `listen` procedures.
|
||||||
##
|
|
||||||
## .. code-block:: Nim
|
runnableExamples("-r:off"):
|
||||||
## var socket = newSocket()
|
let socket = newSocket()
|
||||||
## socket.bindAddr(Port(1234))
|
socket.bindAddr(Port(1234))
|
||||||
## socket.listen()
|
socket.listen()
|
||||||
##
|
|
||||||
## You can then begin accepting connections using the `accept` procedure.
|
# You can then begin accepting connections using the `accept` procedure.
|
||||||
##
|
var client: Socket
|
||||||
## .. code-block:: Nim
|
var address = ""
|
||||||
## var client: Socket
|
while true:
|
||||||
## var address = ""
|
socket.acceptAddr(client, address)
|
||||||
## while true:
|
echo "Client connected from: ", address
|
||||||
## socket.acceptAddr(client, address)
|
|
||||||
## echo("Client connected from: ", address)
|
|
||||||
|
|
||||||
import std/private/since
|
import std/private/since
|
||||||
|
|
||||||
|
|
@ -960,7 +958,7 @@ proc bindAddr*(socket: Socket, port = Port(0), address = "") {.
|
||||||
var aiList = getAddrInfo(realaddr, port, socket.domain)
|
var aiList = getAddrInfo(realaddr, port, socket.domain)
|
||||||
if bindAddr(socket.fd, aiList.ai_addr, aiList.ai_addrlen.SockLen) < 0'i32:
|
if bindAddr(socket.fd, aiList.ai_addr, aiList.ai_addrlen.SockLen) < 0'i32:
|
||||||
freeaddrinfo(aiList)
|
freeaddrinfo(aiList)
|
||||||
raiseOSError(osLastError())
|
raiseOSError(osLastError(), "address: $# port: $#" % [address, $port])
|
||||||
freeaddrinfo(aiList)
|
freeaddrinfo(aiList)
|
||||||
|
|
||||||
proc acceptAddr*(server: Socket, client: var owned(Socket), address: var string,
|
proc acceptAddr*(server: Socket, client: var owned(Socket), address: var string,
|
||||||
|
|
@ -1233,12 +1231,10 @@ proc getPeerAddr*(socket: Socket): (string, Port) =
|
||||||
proc setSockOpt*(socket: Socket, opt: SOBool, value: bool,
|
proc setSockOpt*(socket: Socket, opt: SOBool, value: bool,
|
||||||
level = SOL_SOCKET) {.tags: [WriteIOEffect].} =
|
level = SOL_SOCKET) {.tags: [WriteIOEffect].} =
|
||||||
## Sets option `opt` to a boolean value specified by `value`.
|
## Sets option `opt` to a boolean value specified by `value`.
|
||||||
##
|
runnableExamples("-r:off"):
|
||||||
## .. code-block:: Nim
|
let socket = newSocket()
|
||||||
## var socket = newSocket()
|
socket.setSockOpt(OptReusePort, true)
|
||||||
## socket.setSockOpt(OptReusePort, true)
|
socket.setSockOpt(OptNoDelay, true, level = IPPROTO_TCP.cint)
|
||||||
## socket.setSockOpt(OptNoDelay, true, level=IPPROTO_TCP.toInt)
|
|
||||||
##
|
|
||||||
var valuei = cint(if value: 1 else: 0)
|
var valuei = cint(if value: 1 else: 0)
|
||||||
setSockOptInt(socket.fd, cint(level), toCInt(opt), valuei)
|
setSockOptInt(socket.fd, cint(level), toCInt(opt), valuei)
|
||||||
|
|
||||||
|
|
@ -2025,10 +2021,8 @@ proc getPrimaryIPAddr*(dest = parseIpAddress("8.8.8.8")): IpAddress =
|
||||||
##
|
##
|
||||||
## Supports IPv4 and v6.
|
## Supports IPv4 and v6.
|
||||||
## Raises OSError if external networking is not set up.
|
## Raises OSError if external networking is not set up.
|
||||||
##
|
runnableExamples("-r:off"):
|
||||||
## .. code-block:: Nim
|
echo getPrimaryIPAddr() # "192.168.1.2"
|
||||||
## echo $getPrimaryIPAddr() # "192.168.1.2"
|
|
||||||
|
|
||||||
let socket =
|
let socket =
|
||||||
if dest.family == IpAddressFamily.IPv4:
|
if dest.family == IpAddressFamily.IPv4:
|
||||||
newSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
|
newSocket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue