This is my first time i'm playing with nim. I'm programming socket using module net (as suggested). I want to get the local ip address.
In python it is something like this:
sockname = sock.getsockname()
ip = sockname[0]
port = str(sockname[1])
In nim it something like this:
var port = getSockName(sock.getFd)
But i can't find the proc that can return our local ip. Is it possible to get local ip address without executing external app, or using c? (Of course, i can hack some proc in the modules rawsockets to get the ip)
Yes that which i mean by using some c. By using h_addr_list returned from getHostByName, i should get the address. Or the more easier one is, modify proc getSockName and parsing the ip address from it (not just the port), which i always do in c (posix).
Or another solution by invoking ifconfig / ipconfig and then regex it.
I want to know how easy programming socket using nim.
Until now, my fastest one (rapid development) still python. The same thing i build from c in 6 month, could just be build using python for 1 week. (Of course with worst performance) The longest one is c. So i kind of avoid c, if possible.
I've known nim from the wikipedia of python. (I wan't to know language which easier than python) I've tried nim for one day, and i think the time i need to develop is about the same with objective C. But definitely more shorter and a lot of readability than c :) and a much more better in performance than python.
It's such a good piece of language, in my opinion.
Here's what I have for something similar (returning a list of network interface information on posix systems). It doesn't correctly handle the union in the ifaddrs struct for point-to-point links but it should be enough to get you started.
import posix
type Interface* = object of RootObj
name*: string
address*: string
broadcast*: string
netmask*: string
type ifaddrs = object
pifaddrs: ref ifaddrs # Next item in list
ifa_name: cstring # Name of interface
ifa_flags: uint # Flags from SIOCGIFFLAGS
ifa_addr: ref SockAddr_in # Address of interface
ifa_netmask: ref SockAddr_in # Netmask of interface
ifu_broadaddr: ref SockAddr_in # Broadcast address of interface
ifa_data: pointer # Address-specific data
proc getifaddrs( ifap: var ref ifaddrs): int {.header: "<ifaddrs.h>", importc: "getifaddrs".}
proc freeifaddrs( ifap: ref ifaddrs): void {.header: "<ifaddrs.h>", importc: "freeifaddrs".}
proc interfaces(): seq[Interface] =
var interfaceList : ref ifaddrs
var currentInterface : ref ifaddrs
result = newSeq[Interface]()
discard getifaddrs( interfaceList )
# set currentInterface to the interfaceList "head"
currentInterface = interfaceList
while currentInterface != nil:
let address = currentInterface.ifa_addr
let broadcast = currentInterface.ifu_broadaddr
let netmask = currentInterface.ifa_netmask
if address != nil and netmask != nil:
let data = Interface( name: $currentInterface.ifa_name,
address: $inet_ntoa( address.sin_addr ),
broadcast: $inet_ntoa( broadcast.sin_addr ),
netmask: $inet_ntoa( netmask.sin_addr ) )
result.add( data )
currentInterface = currentInterface.pifaddrs
freeifaddrs( interfaceList )
echo( interfaces() )
If you want to get the IP address of whichever interface is used to connect to the network without having to know its name, you can use this:
import socket
def get_ip_address():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
return s.getsockname()[0]
You do not have to have a route to 8.8.8.8 to use this. All it is doing is opening a socket, but not sending any data.or with psutils module ( https://github.com/johnscillieri/psutil-nim )
import psutil
echo net_if_addrs()