Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Transform the following Factor implementation into Python, maintaining the same output and logic.
USING: dns io kernel sequences ; "www.kame.net" [ dns-A-query ] [ dns-AAAA-query ] bi [ message>names second print ] bi@
>>> import socket >>> ips = set(i[4][0] for i in socket.getaddrinfo('www.kame.net', 80)) >>> for ip in ips: print ip ... 2001:200:dff:fff1:216:3eff:feb1:44d7 203.178.141.194
Generate a VB translation of this Factor snippet without changing its computational steps.
USING: dns io kernel sequences ; "www.kame.net" [ dns-A-query ] [ dns-AAAA-query ] bi [ message>names second print ] bi@
#include "win\winsock2.bi" Function GetSiteAddress(stuff As String = "www.freebasic.net") As Long Dim As WSADATA _wsadate Dim As in_addr addr Dim As hostent Ptr res Dim As Integer i = 0 WSAStartup(MAKEWORD(2,2),@_wsadate) res = gethostbyname(stuff) If res Then Print !"\nURL: "; stuff While (res->h_addr_list[i] <> 0) addr.s_addr = *(Cast (Ulong Ptr,res->h_addr_list[i])) Print "IPv4 address: ";*inet_ntoa(addr) i+=1 Wend WSACleanup() Return 1 Else Print "website error?" Return 0 End If End Function GetSiteAddress "rosettacode.org" GetSiteAddress "www.kame.net" Sleep
Rewrite this program in Go while keeping its functionality equivalent to the Factor version.
USING: dns io kernel sequences ; "www.kame.net" [ dns-A-query ] [ dns-AAAA-query ] bi [ message>names second print ] bi@
package main import ( "fmt" "net" ) func main() { if addrs, err := net.LookupHost("www.kame.net"); err == nil { fmt.Println(addrs) } else { fmt.Println(err) } }
Change the following Groovy code into C without altering its purpose.
def addresses = InetAddress.getAllByName('www.kame.net') println "IPv4: ${addresses.find { it instanceof Inet4Address }?.hostAddress}" println "IPv6: ${addresses.find { it instanceof Inet6Address }?.hostAddress}"
#include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo("www.kame.net", NULL, &hints, &res0); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); exit(1); } for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); } else { printf("%s\n", host); } } freeaddrinfo(res0); return 0; }
Change the programming language of this snippet from Groovy to C# without modifying what it does.
def addresses = InetAddress.getAllByName('www.kame.net') println "IPv4: ${addresses.find { it instanceof Inet4Address }?.hostAddress}" println "IPv6: ${addresses.find { it instanceof Inet6Address }?.hostAddress}"
private string LookupDns(string s) { try { System.Net.IPHostEntry ip = System.Net.Dns.GetHostEntry(s); string result = ip.AddressList[0].ToString(); for (int i = 1; i < ip.AddressList.Length; ++i) result += ", " + ip.AddressList[i].ToString(); return result; } catch (System.Net.Sockets.SocketException se) { return se.Message; } }
Write the same code in C++ as shown below in Groovy.
def addresses = InetAddress.getAllByName('www.kame.net') println "IPv4: ${addresses.find { it instanceof Inet4Address }?.hostAddress}" println "IPv6: ${addresses.find { it instanceof Inet6Address }?.hostAddress}"
#include <Rcpp.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace Rcpp ; CharacterVector getNameInfo(std::string fqdn) { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo(fqdn.c_str(), NULL, &hints, &res0); if (error) { return(NA_STRING); } int i = 0 ; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { i++ ; } } CharacterVector results(i) ; i = 0; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { results[i++] = host ; } } freeaddrinfo(res0); return(results) ; }
Please provide an equivalent version of this Groovy code in Java.
def addresses = InetAddress.getAllByName('www.kame.net') println "IPv4: ${addresses.find { it instanceof Inet4Address }?.hostAddress}" println "IPv6: ${addresses.find { it instanceof Inet6Address }?.hostAddress}"
import java.net.InetAddress; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.UnknownHostException; class DnsQuery { public static void main(String[] args) { try { InetAddress[] ipAddr = InetAddress.getAllByName("www.kame.net"); for(int i=0; i < ipAddr.length ; i++) { if (ipAddr[i] instanceof Inet4Address) { System.out.println("IPv4 : " + ipAddr[i].getHostAddress()); } else if (ipAddr[i] instanceof Inet6Address) { System.out.println("IPv6 : " + ipAddr[i].getHostAddress()); } } } catch (UnknownHostException uhe) { System.err.println("unknown host"); } } }
Convert this Groovy block to Python, preserving its control flow and logic.
def addresses = InetAddress.getAllByName('www.kame.net') println "IPv4: ${addresses.find { it instanceof Inet4Address }?.hostAddress}" println "IPv6: ${addresses.find { it instanceof Inet6Address }?.hostAddress}"
>>> import socket >>> ips = set(i[4][0] for i in socket.getaddrinfo('www.kame.net', 80)) >>> for ip in ips: print ip ... 2001:200:dff:fff1:216:3eff:feb1:44d7 203.178.141.194
Change the programming language of this snippet from Groovy to VB without modifying what it does.
def addresses = InetAddress.getAllByName('www.kame.net') println "IPv4: ${addresses.find { it instanceof Inet4Address }?.hostAddress}" println "IPv6: ${addresses.find { it instanceof Inet6Address }?.hostAddress}"
#include "win\winsock2.bi" Function GetSiteAddress(stuff As String = "www.freebasic.net") As Long Dim As WSADATA _wsadate Dim As in_addr addr Dim As hostent Ptr res Dim As Integer i = 0 WSAStartup(MAKEWORD(2,2),@_wsadate) res = gethostbyname(stuff) If res Then Print !"\nURL: "; stuff While (res->h_addr_list[i] <> 0) addr.s_addr = *(Cast (Ulong Ptr,res->h_addr_list[i])) Print "IPv4 address: ";*inet_ntoa(addr) i+=1 Wend WSACleanup() Return 1 Else Print "website error?" Return 0 End If End Function GetSiteAddress "rosettacode.org" GetSiteAddress "www.kame.net" Sleep
Translate this program into Go but keep the logic exactly as in Groovy.
def addresses = InetAddress.getAllByName('www.kame.net') println "IPv4: ${addresses.find { it instanceof Inet4Address }?.hostAddress}" println "IPv6: ${addresses.find { it instanceof Inet6Address }?.hostAddress}"
package main import ( "fmt" "net" ) func main() { if addrs, err := net.LookupHost("www.kame.net"); err == nil { fmt.Println(addrs) } else { fmt.Println(err) } }
Transform the following Haskell implementation into C, maintaining the same output and logic.
module Main where import Network.Socket getWebAddresses :: HostName -> IO [SockAddr] getWebAddresses host = do results <- getAddrInfo (Just defaultHints) (Just host) (Just "http") return [ addrAddress a | a <- results, addrSocketType a == Stream ] showIPs :: HostName -> IO () showIPs host = do putStrLn $ "IP addresses for " ++ host ++ ":" addresses <- getWebAddresses host mapM_ (putStrLn . (" "++) . show) addresses main = showIPs "www.kame.net"
#include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo("www.kame.net", NULL, &hints, &res0); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); exit(1); } for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); } else { printf("%s\n", host); } } freeaddrinfo(res0); return 0; }
Convert this Haskell snippet to C# and keep its semantics consistent.
module Main where import Network.Socket getWebAddresses :: HostName -> IO [SockAddr] getWebAddresses host = do results <- getAddrInfo (Just defaultHints) (Just host) (Just "http") return [ addrAddress a | a <- results, addrSocketType a == Stream ] showIPs :: HostName -> IO () showIPs host = do putStrLn $ "IP addresses for " ++ host ++ ":" addresses <- getWebAddresses host mapM_ (putStrLn . (" "++) . show) addresses main = showIPs "www.kame.net"
private string LookupDns(string s) { try { System.Net.IPHostEntry ip = System.Net.Dns.GetHostEntry(s); string result = ip.AddressList[0].ToString(); for (int i = 1; i < ip.AddressList.Length; ++i) result += ", " + ip.AddressList[i].ToString(); return result; } catch (System.Net.Sockets.SocketException se) { return se.Message; } }
Rewrite this program in C++ while keeping its functionality equivalent to the Haskell version.
module Main where import Network.Socket getWebAddresses :: HostName -> IO [SockAddr] getWebAddresses host = do results <- getAddrInfo (Just defaultHints) (Just host) (Just "http") return [ addrAddress a | a <- results, addrSocketType a == Stream ] showIPs :: HostName -> IO () showIPs host = do putStrLn $ "IP addresses for " ++ host ++ ":" addresses <- getWebAddresses host mapM_ (putStrLn . (" "++) . show) addresses main = showIPs "www.kame.net"
#include <Rcpp.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace Rcpp ; CharacterVector getNameInfo(std::string fqdn) { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo(fqdn.c_str(), NULL, &hints, &res0); if (error) { return(NA_STRING); } int i = 0 ; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { i++ ; } } CharacterVector results(i) ; i = 0; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { results[i++] = host ; } } freeaddrinfo(res0); return(results) ; }
Write the same algorithm in Java as shown in this Haskell implementation.
module Main where import Network.Socket getWebAddresses :: HostName -> IO [SockAddr] getWebAddresses host = do results <- getAddrInfo (Just defaultHints) (Just host) (Just "http") return [ addrAddress a | a <- results, addrSocketType a == Stream ] showIPs :: HostName -> IO () showIPs host = do putStrLn $ "IP addresses for " ++ host ++ ":" addresses <- getWebAddresses host mapM_ (putStrLn . (" "++) . show) addresses main = showIPs "www.kame.net"
import java.net.InetAddress; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.UnknownHostException; class DnsQuery { public static void main(String[] args) { try { InetAddress[] ipAddr = InetAddress.getAllByName("www.kame.net"); for(int i=0; i < ipAddr.length ; i++) { if (ipAddr[i] instanceof Inet4Address) { System.out.println("IPv4 : " + ipAddr[i].getHostAddress()); } else if (ipAddr[i] instanceof Inet6Address) { System.out.println("IPv6 : " + ipAddr[i].getHostAddress()); } } } catch (UnknownHostException uhe) { System.err.println("unknown host"); } } }
Can you help me rewrite this code in Python instead of Haskell, keeping it the same logically?
module Main where import Network.Socket getWebAddresses :: HostName -> IO [SockAddr] getWebAddresses host = do results <- getAddrInfo (Just defaultHints) (Just host) (Just "http") return [ addrAddress a | a <- results, addrSocketType a == Stream ] showIPs :: HostName -> IO () showIPs host = do putStrLn $ "IP addresses for " ++ host ++ ":" addresses <- getWebAddresses host mapM_ (putStrLn . (" "++) . show) addresses main = showIPs "www.kame.net"
>>> import socket >>> ips = set(i[4][0] for i in socket.getaddrinfo('www.kame.net', 80)) >>> for ip in ips: print ip ... 2001:200:dff:fff1:216:3eff:feb1:44d7 203.178.141.194
Keep all operations the same but rewrite the snippet in VB.
module Main where import Network.Socket getWebAddresses :: HostName -> IO [SockAddr] getWebAddresses host = do results <- getAddrInfo (Just defaultHints) (Just host) (Just "http") return [ addrAddress a | a <- results, addrSocketType a == Stream ] showIPs :: HostName -> IO () showIPs host = do putStrLn $ "IP addresses for " ++ host ++ ":" addresses <- getWebAddresses host mapM_ (putStrLn . (" "++) . show) addresses main = showIPs "www.kame.net"
#include "win\winsock2.bi" Function GetSiteAddress(stuff As String = "www.freebasic.net") As Long Dim As WSADATA _wsadate Dim As in_addr addr Dim As hostent Ptr res Dim As Integer i = 0 WSAStartup(MAKEWORD(2,2),@_wsadate) res = gethostbyname(stuff) If res Then Print !"\nURL: "; stuff While (res->h_addr_list[i] <> 0) addr.s_addr = *(Cast (Ulong Ptr,res->h_addr_list[i])) Print "IPv4 address: ";*inet_ntoa(addr) i+=1 Wend WSACleanup() Return 1 Else Print "website error?" Return 0 End If End Function GetSiteAddress "rosettacode.org" GetSiteAddress "www.kame.net" Sleep
Write the same code in Go as shown below in Haskell.
module Main where import Network.Socket getWebAddresses :: HostName -> IO [SockAddr] getWebAddresses host = do results <- getAddrInfo (Just defaultHints) (Just host) (Just "http") return [ addrAddress a | a <- results, addrSocketType a == Stream ] showIPs :: HostName -> IO () showIPs host = do putStrLn $ "IP addresses for " ++ host ++ ":" addresses <- getWebAddresses host mapM_ (putStrLn . (" "++) . show) addresses main = showIPs "www.kame.net"
package main import ( "fmt" "net" ) func main() { if addrs, err := net.LookupHost("www.kame.net"); err == nil { fmt.Println(addrs) } else { fmt.Println(err) } }
Maintain the same structure and functionality when rewriting this code in C.
2!:0'dig -4 +short www.kame.net' orange.kame.net. 203.178.141.194 2!:0'dig -6 +short www.kame.net' |interface error | 2!:0'dig -6 +short www.kame.net'
#include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo("www.kame.net", NULL, &hints, &res0); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); exit(1); } for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); } else { printf("%s\n", host); } } freeaddrinfo(res0); return 0; }
Convert the following code from J to C#, ensuring the logic remains intact.
2!:0'dig -4 +short www.kame.net' orange.kame.net. 203.178.141.194 2!:0'dig -6 +short www.kame.net' |interface error | 2!:0'dig -6 +short www.kame.net'
private string LookupDns(string s) { try { System.Net.IPHostEntry ip = System.Net.Dns.GetHostEntry(s); string result = ip.AddressList[0].ToString(); for (int i = 1; i < ip.AddressList.Length; ++i) result += ", " + ip.AddressList[i].ToString(); return result; } catch (System.Net.Sockets.SocketException se) { return se.Message; } }
Write a version of this J function in C++ with identical behavior.
2!:0'dig -4 +short www.kame.net' orange.kame.net. 203.178.141.194 2!:0'dig -6 +short www.kame.net' |interface error | 2!:0'dig -6 +short www.kame.net'
#include <Rcpp.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace Rcpp ; CharacterVector getNameInfo(std::string fqdn) { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo(fqdn.c_str(), NULL, &hints, &res0); if (error) { return(NA_STRING); } int i = 0 ; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { i++ ; } } CharacterVector results(i) ; i = 0; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { results[i++] = host ; } } freeaddrinfo(res0); return(results) ; }
Generate an equivalent Java version of this J code.
2!:0'dig -4 +short www.kame.net' orange.kame.net. 203.178.141.194 2!:0'dig -6 +short www.kame.net' |interface error | 2!:0'dig -6 +short www.kame.net'
import java.net.InetAddress; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.UnknownHostException; class DnsQuery { public static void main(String[] args) { try { InetAddress[] ipAddr = InetAddress.getAllByName("www.kame.net"); for(int i=0; i < ipAddr.length ; i++) { if (ipAddr[i] instanceof Inet4Address) { System.out.println("IPv4 : " + ipAddr[i].getHostAddress()); } else if (ipAddr[i] instanceof Inet6Address) { System.out.println("IPv6 : " + ipAddr[i].getHostAddress()); } } } catch (UnknownHostException uhe) { System.err.println("unknown host"); } } }
Preserve the algorithm and functionality while converting the code from J to Python.
2!:0'dig -4 +short www.kame.net' orange.kame.net. 203.178.141.194 2!:0'dig -6 +short www.kame.net' |interface error | 2!:0'dig -6 +short www.kame.net'
>>> import socket >>> ips = set(i[4][0] for i in socket.getaddrinfo('www.kame.net', 80)) >>> for ip in ips: print ip ... 2001:200:dff:fff1:216:3eff:feb1:44d7 203.178.141.194
Generate an equivalent VB version of this J code.
2!:0'dig -4 +short www.kame.net' orange.kame.net. 203.178.141.194 2!:0'dig -6 +short www.kame.net' |interface error | 2!:0'dig -6 +short www.kame.net'
#include "win\winsock2.bi" Function GetSiteAddress(stuff As String = "www.freebasic.net") As Long Dim As WSADATA _wsadate Dim As in_addr addr Dim As hostent Ptr res Dim As Integer i = 0 WSAStartup(MAKEWORD(2,2),@_wsadate) res = gethostbyname(stuff) If res Then Print !"\nURL: "; stuff While (res->h_addr_list[i] <> 0) addr.s_addr = *(Cast (Ulong Ptr,res->h_addr_list[i])) Print "IPv4 address: ";*inet_ntoa(addr) i+=1 Wend WSACleanup() Return 1 Else Print "website error?" Return 0 End If End Function GetSiteAddress "rosettacode.org" GetSiteAddress "www.kame.net" Sleep
Ensure the translated Go code behaves exactly like the original J snippet.
2!:0'dig -4 +short www.kame.net' orange.kame.net. 203.178.141.194 2!:0'dig -6 +short www.kame.net' |interface error | 2!:0'dig -6 +short www.kame.net'
package main import ( "fmt" "net" ) func main() { if addrs, err := net.LookupHost("www.kame.net"); err == nil { fmt.Println(addrs) } else { fmt.Println(err) } }
Produce a functionally identical C code for the snippet given in Julia.
julia> using Sockets julia> getaddrinfo("www.kame.net") ip"203.178.141.194" julia> getaddrinfo("www.kame.net", IPv6) ip"2001:200:dff:fff1:216:3eff:feb1:44d7"
#include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo("www.kame.net", NULL, &hints, &res0); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); exit(1); } for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); } else { printf("%s\n", host); } } freeaddrinfo(res0); return 0; }
Please provide an equivalent version of this Julia code in C#.
julia> using Sockets julia> getaddrinfo("www.kame.net") ip"203.178.141.194" julia> getaddrinfo("www.kame.net", IPv6) ip"2001:200:dff:fff1:216:3eff:feb1:44d7"
private string LookupDns(string s) { try { System.Net.IPHostEntry ip = System.Net.Dns.GetHostEntry(s); string result = ip.AddressList[0].ToString(); for (int i = 1; i < ip.AddressList.Length; ++i) result += ", " + ip.AddressList[i].ToString(); return result; } catch (System.Net.Sockets.SocketException se) { return se.Message; } }
Rewrite this program in C++ while keeping its functionality equivalent to the Julia version.
julia> using Sockets julia> getaddrinfo("www.kame.net") ip"203.178.141.194" julia> getaddrinfo("www.kame.net", IPv6) ip"2001:200:dff:fff1:216:3eff:feb1:44d7"
#include <Rcpp.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace Rcpp ; CharacterVector getNameInfo(std::string fqdn) { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo(fqdn.c_str(), NULL, &hints, &res0); if (error) { return(NA_STRING); } int i = 0 ; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { i++ ; } } CharacterVector results(i) ; i = 0; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { results[i++] = host ; } } freeaddrinfo(res0); return(results) ; }
Convert the following code from Julia to Java, ensuring the logic remains intact.
julia> using Sockets julia> getaddrinfo("www.kame.net") ip"203.178.141.194" julia> getaddrinfo("www.kame.net", IPv6) ip"2001:200:dff:fff1:216:3eff:feb1:44d7"
import java.net.InetAddress; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.UnknownHostException; class DnsQuery { public static void main(String[] args) { try { InetAddress[] ipAddr = InetAddress.getAllByName("www.kame.net"); for(int i=0; i < ipAddr.length ; i++) { if (ipAddr[i] instanceof Inet4Address) { System.out.println("IPv4 : " + ipAddr[i].getHostAddress()); } else if (ipAddr[i] instanceof Inet6Address) { System.out.println("IPv6 : " + ipAddr[i].getHostAddress()); } } } catch (UnknownHostException uhe) { System.err.println("unknown host"); } } }
Convert this Julia snippet to Python and keep its semantics consistent.
julia> using Sockets julia> getaddrinfo("www.kame.net") ip"203.178.141.194" julia> getaddrinfo("www.kame.net", IPv6) ip"2001:200:dff:fff1:216:3eff:feb1:44d7"
>>> import socket >>> ips = set(i[4][0] for i in socket.getaddrinfo('www.kame.net', 80)) >>> for ip in ips: print ip ... 2001:200:dff:fff1:216:3eff:feb1:44d7 203.178.141.194
Please provide an equivalent version of this Julia code in VB.
julia> using Sockets julia> getaddrinfo("www.kame.net") ip"203.178.141.194" julia> getaddrinfo("www.kame.net", IPv6) ip"2001:200:dff:fff1:216:3eff:feb1:44d7"
#include "win\winsock2.bi" Function GetSiteAddress(stuff As String = "www.freebasic.net") As Long Dim As WSADATA _wsadate Dim As in_addr addr Dim As hostent Ptr res Dim As Integer i = 0 WSAStartup(MAKEWORD(2,2),@_wsadate) res = gethostbyname(stuff) If res Then Print !"\nURL: "; stuff While (res->h_addr_list[i] <> 0) addr.s_addr = *(Cast (Ulong Ptr,res->h_addr_list[i])) Print "IPv4 address: ";*inet_ntoa(addr) i+=1 Wend WSACleanup() Return 1 Else Print "website error?" Return 0 End If End Function GetSiteAddress "rosettacode.org" GetSiteAddress "www.kame.net" Sleep
Write a version of this Julia function in Go with identical behavior.
julia> using Sockets julia> getaddrinfo("www.kame.net") ip"203.178.141.194" julia> getaddrinfo("www.kame.net", IPv6) ip"2001:200:dff:fff1:216:3eff:feb1:44d7"
package main import ( "fmt" "net" ) func main() { if addrs, err := net.LookupHost("www.kame.net"); err == nil { fmt.Println(addrs) } else { fmt.Println(err) } }
Change the following Lua code into C without altering its purpose.
local socket = require('socket') local ip_tbl = socket.dns.getaddrinfo('www.kame.net') for _, v in ipairs(ip_tbl) do io.write(string.format('%s: %s\n', v.family, v.addr)) end
#include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo("www.kame.net", NULL, &hints, &res0); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); exit(1); } for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); } else { printf("%s\n", host); } } freeaddrinfo(res0); return 0; }
Write the same algorithm in C# as shown in this Lua implementation.
local socket = require('socket') local ip_tbl = socket.dns.getaddrinfo('www.kame.net') for _, v in ipairs(ip_tbl) do io.write(string.format('%s: %s\n', v.family, v.addr)) end
private string LookupDns(string s) { try { System.Net.IPHostEntry ip = System.Net.Dns.GetHostEntry(s); string result = ip.AddressList[0].ToString(); for (int i = 1; i < ip.AddressList.Length; ++i) result += ", " + ip.AddressList[i].ToString(); return result; } catch (System.Net.Sockets.SocketException se) { return se.Message; } }
Port the following code from Lua to C++ with equivalent syntax and logic.
local socket = require('socket') local ip_tbl = socket.dns.getaddrinfo('www.kame.net') for _, v in ipairs(ip_tbl) do io.write(string.format('%s: %s\n', v.family, v.addr)) end
#include <Rcpp.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace Rcpp ; CharacterVector getNameInfo(std::string fqdn) { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo(fqdn.c_str(), NULL, &hints, &res0); if (error) { return(NA_STRING); } int i = 0 ; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { i++ ; } } CharacterVector results(i) ; i = 0; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { results[i++] = host ; } } freeaddrinfo(res0); return(results) ; }
Can you help me rewrite this code in Java instead of Lua, keeping it the same logically?
local socket = require('socket') local ip_tbl = socket.dns.getaddrinfo('www.kame.net') for _, v in ipairs(ip_tbl) do io.write(string.format('%s: %s\n', v.family, v.addr)) end
import java.net.InetAddress; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.UnknownHostException; class DnsQuery { public static void main(String[] args) { try { InetAddress[] ipAddr = InetAddress.getAllByName("www.kame.net"); for(int i=0; i < ipAddr.length ; i++) { if (ipAddr[i] instanceof Inet4Address) { System.out.println("IPv4 : " + ipAddr[i].getHostAddress()); } else if (ipAddr[i] instanceof Inet6Address) { System.out.println("IPv6 : " + ipAddr[i].getHostAddress()); } } } catch (UnknownHostException uhe) { System.err.println("unknown host"); } } }
Can you help me rewrite this code in Python instead of Lua, keeping it the same logically?
local socket = require('socket') local ip_tbl = socket.dns.getaddrinfo('www.kame.net') for _, v in ipairs(ip_tbl) do io.write(string.format('%s: %s\n', v.family, v.addr)) end
>>> import socket >>> ips = set(i[4][0] for i in socket.getaddrinfo('www.kame.net', 80)) >>> for ip in ips: print ip ... 2001:200:dff:fff1:216:3eff:feb1:44d7 203.178.141.194
Generate a VB translation of this Lua snippet without changing its computational steps.
local socket = require('socket') local ip_tbl = socket.dns.getaddrinfo('www.kame.net') for _, v in ipairs(ip_tbl) do io.write(string.format('%s: %s\n', v.family, v.addr)) end
#include "win\winsock2.bi" Function GetSiteAddress(stuff As String = "www.freebasic.net") As Long Dim As WSADATA _wsadate Dim As in_addr addr Dim As hostent Ptr res Dim As Integer i = 0 WSAStartup(MAKEWORD(2,2),@_wsadate) res = gethostbyname(stuff) If res Then Print !"\nURL: "; stuff While (res->h_addr_list[i] <> 0) addr.s_addr = *(Cast (Ulong Ptr,res->h_addr_list[i])) Print "IPv4 address: ";*inet_ntoa(addr) i+=1 Wend WSACleanup() Return 1 Else Print "website error?" Return 0 End If End Function GetSiteAddress "rosettacode.org" GetSiteAddress "www.kame.net" Sleep
Keep all operations the same but rewrite the snippet in Go.
local socket = require('socket') local ip_tbl = socket.dns.getaddrinfo('www.kame.net') for _, v in ipairs(ip_tbl) do io.write(string.format('%s: %s\n', v.family, v.addr)) end
package main import ( "fmt" "net" ) func main() { if addrs, err := net.LookupHost("www.kame.net"); err == nil { fmt.Println(addrs) } else { fmt.Println(err) } }
Write the same algorithm in C as shown in this Nim implementation.
import nativesockets iterator items(ai: ptr AddrInfo): ptr AddrInfo = var current = ai while current != nil: yield current current = current.aiNext proc main() = let addrInfos = getAddrInfo("www.kame.net", Port 80, AfUnspec) defer: freeAddrInfo addrInfos for i in addrInfos: echo getAddrString i.aiAddr when isMainModule: main()
#include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo("www.kame.net", NULL, &hints, &res0); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); exit(1); } for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); } else { printf("%s\n", host); } } freeaddrinfo(res0); return 0; }
Ensure the translated C# code behaves exactly like the original Nim snippet.
import nativesockets iterator items(ai: ptr AddrInfo): ptr AddrInfo = var current = ai while current != nil: yield current current = current.aiNext proc main() = let addrInfos = getAddrInfo("www.kame.net", Port 80, AfUnspec) defer: freeAddrInfo addrInfos for i in addrInfos: echo getAddrString i.aiAddr when isMainModule: main()
private string LookupDns(string s) { try { System.Net.IPHostEntry ip = System.Net.Dns.GetHostEntry(s); string result = ip.AddressList[0].ToString(); for (int i = 1; i < ip.AddressList.Length; ++i) result += ", " + ip.AddressList[i].ToString(); return result; } catch (System.Net.Sockets.SocketException se) { return se.Message; } }
Transform the following Nim implementation into C++, maintaining the same output and logic.
import nativesockets iterator items(ai: ptr AddrInfo): ptr AddrInfo = var current = ai while current != nil: yield current current = current.aiNext proc main() = let addrInfos = getAddrInfo("www.kame.net", Port 80, AfUnspec) defer: freeAddrInfo addrInfos for i in addrInfos: echo getAddrString i.aiAddr when isMainModule: main()
#include <Rcpp.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace Rcpp ; CharacterVector getNameInfo(std::string fqdn) { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo(fqdn.c_str(), NULL, &hints, &res0); if (error) { return(NA_STRING); } int i = 0 ; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { i++ ; } } CharacterVector results(i) ; i = 0; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { results[i++] = host ; } } freeaddrinfo(res0); return(results) ; }
Translate this program into Java but keep the logic exactly as in Nim.
import nativesockets iterator items(ai: ptr AddrInfo): ptr AddrInfo = var current = ai while current != nil: yield current current = current.aiNext proc main() = let addrInfos = getAddrInfo("www.kame.net", Port 80, AfUnspec) defer: freeAddrInfo addrInfos for i in addrInfos: echo getAddrString i.aiAddr when isMainModule: main()
import java.net.InetAddress; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.UnknownHostException; class DnsQuery { public static void main(String[] args) { try { InetAddress[] ipAddr = InetAddress.getAllByName("www.kame.net"); for(int i=0; i < ipAddr.length ; i++) { if (ipAddr[i] instanceof Inet4Address) { System.out.println("IPv4 : " + ipAddr[i].getHostAddress()); } else if (ipAddr[i] instanceof Inet6Address) { System.out.println("IPv6 : " + ipAddr[i].getHostAddress()); } } } catch (UnknownHostException uhe) { System.err.println("unknown host"); } } }
Preserve the algorithm and functionality while converting the code from Nim to Python.
import nativesockets iterator items(ai: ptr AddrInfo): ptr AddrInfo = var current = ai while current != nil: yield current current = current.aiNext proc main() = let addrInfos = getAddrInfo("www.kame.net", Port 80, AfUnspec) defer: freeAddrInfo addrInfos for i in addrInfos: echo getAddrString i.aiAddr when isMainModule: main()
>>> import socket >>> ips = set(i[4][0] for i in socket.getaddrinfo('www.kame.net', 80)) >>> for ip in ips: print ip ... 2001:200:dff:fff1:216:3eff:feb1:44d7 203.178.141.194
Preserve the algorithm and functionality while converting the code from Nim to VB.
import nativesockets iterator items(ai: ptr AddrInfo): ptr AddrInfo = var current = ai while current != nil: yield current current = current.aiNext proc main() = let addrInfos = getAddrInfo("www.kame.net", Port 80, AfUnspec) defer: freeAddrInfo addrInfos for i in addrInfos: echo getAddrString i.aiAddr when isMainModule: main()
#include "win\winsock2.bi" Function GetSiteAddress(stuff As String = "www.freebasic.net") As Long Dim As WSADATA _wsadate Dim As in_addr addr Dim As hostent Ptr res Dim As Integer i = 0 WSAStartup(MAKEWORD(2,2),@_wsadate) res = gethostbyname(stuff) If res Then Print !"\nURL: "; stuff While (res->h_addr_list[i] <> 0) addr.s_addr = *(Cast (Ulong Ptr,res->h_addr_list[i])) Print "IPv4 address: ";*inet_ntoa(addr) i+=1 Wend WSACleanup() Return 1 Else Print "website error?" Return 0 End If End Function GetSiteAddress "rosettacode.org" GetSiteAddress "www.kame.net" Sleep
Write a version of this Nim function in Go with identical behavior.
import nativesockets iterator items(ai: ptr AddrInfo): ptr AddrInfo = var current = ai while current != nil: yield current current = current.aiNext proc main() = let addrInfos = getAddrInfo("www.kame.net", Port 80, AfUnspec) defer: freeAddrInfo addrInfos for i in addrInfos: echo getAddrString i.aiAddr when isMainModule: main()
package main import ( "fmt" "net" ) func main() { if addrs, err := net.LookupHost("www.kame.net"); err == nil { fmt.Println(addrs) } else { fmt.Println(err) } }
Generate an equivalent C version of this OCaml code.
let dns_query ~host ~ai_family = let opts = [ Unix.AI_FAMILY ai_family; Unix.AI_SOCKTYPE Unix.SOCK_DGRAM; ] in let addr_infos = Unix.getaddrinfo host "" opts in match addr_infos with | [] -> failwith "dns_query" | ai :: _ -> match ai.Unix.ai_addr with | Unix.ADDR_INET (addr, _) -> (Unix.string_of_inet_addr addr) | Unix.ADDR_UNIX addr -> failwith "addr_unix" let () = let host = "www.kame.net" in Printf.printf "primary addresses of %s are:\n" host; Printf.printf " IPv4 address: %s\n" (dns_query host Unix.PF_INET); Printf.printf " IPv6 address: %s\n" (dns_query host Unix.PF_INET6); ;;
#include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo("www.kame.net", NULL, &hints, &res0); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); exit(1); } for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); } else { printf("%s\n", host); } } freeaddrinfo(res0); return 0; }
Maintain the same structure and functionality when rewriting this code in C#.
let dns_query ~host ~ai_family = let opts = [ Unix.AI_FAMILY ai_family; Unix.AI_SOCKTYPE Unix.SOCK_DGRAM; ] in let addr_infos = Unix.getaddrinfo host "" opts in match addr_infos with | [] -> failwith "dns_query" | ai :: _ -> match ai.Unix.ai_addr with | Unix.ADDR_INET (addr, _) -> (Unix.string_of_inet_addr addr) | Unix.ADDR_UNIX addr -> failwith "addr_unix" let () = let host = "www.kame.net" in Printf.printf "primary addresses of %s are:\n" host; Printf.printf " IPv4 address: %s\n" (dns_query host Unix.PF_INET); Printf.printf " IPv6 address: %s\n" (dns_query host Unix.PF_INET6); ;;
private string LookupDns(string s) { try { System.Net.IPHostEntry ip = System.Net.Dns.GetHostEntry(s); string result = ip.AddressList[0].ToString(); for (int i = 1; i < ip.AddressList.Length; ++i) result += ", " + ip.AddressList[i].ToString(); return result; } catch (System.Net.Sockets.SocketException se) { return se.Message; } }
Preserve the algorithm and functionality while converting the code from OCaml to C++.
let dns_query ~host ~ai_family = let opts = [ Unix.AI_FAMILY ai_family; Unix.AI_SOCKTYPE Unix.SOCK_DGRAM; ] in let addr_infos = Unix.getaddrinfo host "" opts in match addr_infos with | [] -> failwith "dns_query" | ai :: _ -> match ai.Unix.ai_addr with | Unix.ADDR_INET (addr, _) -> (Unix.string_of_inet_addr addr) | Unix.ADDR_UNIX addr -> failwith "addr_unix" let () = let host = "www.kame.net" in Printf.printf "primary addresses of %s are:\n" host; Printf.printf " IPv4 address: %s\n" (dns_query host Unix.PF_INET); Printf.printf " IPv6 address: %s\n" (dns_query host Unix.PF_INET6); ;;
#include <Rcpp.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace Rcpp ; CharacterVector getNameInfo(std::string fqdn) { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo(fqdn.c_str(), NULL, &hints, &res0); if (error) { return(NA_STRING); } int i = 0 ; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { i++ ; } } CharacterVector results(i) ; i = 0; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { results[i++] = host ; } } freeaddrinfo(res0); return(results) ; }
Rewrite this program in Java while keeping its functionality equivalent to the OCaml version.
let dns_query ~host ~ai_family = let opts = [ Unix.AI_FAMILY ai_family; Unix.AI_SOCKTYPE Unix.SOCK_DGRAM; ] in let addr_infos = Unix.getaddrinfo host "" opts in match addr_infos with | [] -> failwith "dns_query" | ai :: _ -> match ai.Unix.ai_addr with | Unix.ADDR_INET (addr, _) -> (Unix.string_of_inet_addr addr) | Unix.ADDR_UNIX addr -> failwith "addr_unix" let () = let host = "www.kame.net" in Printf.printf "primary addresses of %s are:\n" host; Printf.printf " IPv4 address: %s\n" (dns_query host Unix.PF_INET); Printf.printf " IPv6 address: %s\n" (dns_query host Unix.PF_INET6); ;;
import java.net.InetAddress; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.UnknownHostException; class DnsQuery { public static void main(String[] args) { try { InetAddress[] ipAddr = InetAddress.getAllByName("www.kame.net"); for(int i=0; i < ipAddr.length ; i++) { if (ipAddr[i] instanceof Inet4Address) { System.out.println("IPv4 : " + ipAddr[i].getHostAddress()); } else if (ipAddr[i] instanceof Inet6Address) { System.out.println("IPv6 : " + ipAddr[i].getHostAddress()); } } } catch (UnknownHostException uhe) { System.err.println("unknown host"); } } }
Please provide an equivalent version of this OCaml code in Python.
let dns_query ~host ~ai_family = let opts = [ Unix.AI_FAMILY ai_family; Unix.AI_SOCKTYPE Unix.SOCK_DGRAM; ] in let addr_infos = Unix.getaddrinfo host "" opts in match addr_infos with | [] -> failwith "dns_query" | ai :: _ -> match ai.Unix.ai_addr with | Unix.ADDR_INET (addr, _) -> (Unix.string_of_inet_addr addr) | Unix.ADDR_UNIX addr -> failwith "addr_unix" let () = let host = "www.kame.net" in Printf.printf "primary addresses of %s are:\n" host; Printf.printf " IPv4 address: %s\n" (dns_query host Unix.PF_INET); Printf.printf " IPv6 address: %s\n" (dns_query host Unix.PF_INET6); ;;
>>> import socket >>> ips = set(i[4][0] for i in socket.getaddrinfo('www.kame.net', 80)) >>> for ip in ips: print ip ... 2001:200:dff:fff1:216:3eff:feb1:44d7 203.178.141.194
Write a version of this OCaml function in VB with identical behavior.
let dns_query ~host ~ai_family = let opts = [ Unix.AI_FAMILY ai_family; Unix.AI_SOCKTYPE Unix.SOCK_DGRAM; ] in let addr_infos = Unix.getaddrinfo host "" opts in match addr_infos with | [] -> failwith "dns_query" | ai :: _ -> match ai.Unix.ai_addr with | Unix.ADDR_INET (addr, _) -> (Unix.string_of_inet_addr addr) | Unix.ADDR_UNIX addr -> failwith "addr_unix" let () = let host = "www.kame.net" in Printf.printf "primary addresses of %s are:\n" host; Printf.printf " IPv4 address: %s\n" (dns_query host Unix.PF_INET); Printf.printf " IPv6 address: %s\n" (dns_query host Unix.PF_INET6); ;;
#include "win\winsock2.bi" Function GetSiteAddress(stuff As String = "www.freebasic.net") As Long Dim As WSADATA _wsadate Dim As in_addr addr Dim As hostent Ptr res Dim As Integer i = 0 WSAStartup(MAKEWORD(2,2),@_wsadate) res = gethostbyname(stuff) If res Then Print !"\nURL: "; stuff While (res->h_addr_list[i] <> 0) addr.s_addr = *(Cast (Ulong Ptr,res->h_addr_list[i])) Print "IPv4 address: ";*inet_ntoa(addr) i+=1 Wend WSACleanup() Return 1 Else Print "website error?" Return 0 End If End Function GetSiteAddress "rosettacode.org" GetSiteAddress "www.kame.net" Sleep
Please provide an equivalent version of this OCaml code in Go.
let dns_query ~host ~ai_family = let opts = [ Unix.AI_FAMILY ai_family; Unix.AI_SOCKTYPE Unix.SOCK_DGRAM; ] in let addr_infos = Unix.getaddrinfo host "" opts in match addr_infos with | [] -> failwith "dns_query" | ai :: _ -> match ai.Unix.ai_addr with | Unix.ADDR_INET (addr, _) -> (Unix.string_of_inet_addr addr) | Unix.ADDR_UNIX addr -> failwith "addr_unix" let () = let host = "www.kame.net" in Printf.printf "primary addresses of %s are:\n" host; Printf.printf " IPv4 address: %s\n" (dns_query host Unix.PF_INET); Printf.printf " IPv6 address: %s\n" (dns_query host Unix.PF_INET6); ;;
package main import ( "fmt" "net" ) func main() { if addrs, err := net.LookupHost("www.kame.net"); err == nil { fmt.Println(addrs) } else { fmt.Println(err) } }
Ensure the translated C code behaves exactly like the original Perl snippet.
use feature 'say'; use Socket qw(getaddrinfo getnameinfo); my ($err, @res) = getaddrinfo('orange.kame.net', 0, { protocol=>Socket::IPPROTO_TCP } ); die "getaddrinfo error: $err" if $err; say ((getnameinfo($_->{addr}, Socket::NI_NUMERICHOST))[1]) for @res
#include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo("www.kame.net", NULL, &hints, &res0); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); exit(1); } for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); } else { printf("%s\n", host); } } freeaddrinfo(res0); return 0; }
Keep all operations the same but rewrite the snippet in C#.
use feature 'say'; use Socket qw(getaddrinfo getnameinfo); my ($err, @res) = getaddrinfo('orange.kame.net', 0, { protocol=>Socket::IPPROTO_TCP } ); die "getaddrinfo error: $err" if $err; say ((getnameinfo($_->{addr}, Socket::NI_NUMERICHOST))[1]) for @res
private string LookupDns(string s) { try { System.Net.IPHostEntry ip = System.Net.Dns.GetHostEntry(s); string result = ip.AddressList[0].ToString(); for (int i = 1; i < ip.AddressList.Length; ++i) result += ", " + ip.AddressList[i].ToString(); return result; } catch (System.Net.Sockets.SocketException se) { return se.Message; } }
Port the provided Perl code into C++ while preserving the original functionality.
use feature 'say'; use Socket qw(getaddrinfo getnameinfo); my ($err, @res) = getaddrinfo('orange.kame.net', 0, { protocol=>Socket::IPPROTO_TCP } ); die "getaddrinfo error: $err" if $err; say ((getnameinfo($_->{addr}, Socket::NI_NUMERICHOST))[1]) for @res
#include <Rcpp.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace Rcpp ; CharacterVector getNameInfo(std::string fqdn) { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo(fqdn.c_str(), NULL, &hints, &res0); if (error) { return(NA_STRING); } int i = 0 ; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { i++ ; } } CharacterVector results(i) ; i = 0; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { results[i++] = host ; } } freeaddrinfo(res0); return(results) ; }
Please provide an equivalent version of this Perl code in Java.
use feature 'say'; use Socket qw(getaddrinfo getnameinfo); my ($err, @res) = getaddrinfo('orange.kame.net', 0, { protocol=>Socket::IPPROTO_TCP } ); die "getaddrinfo error: $err" if $err; say ((getnameinfo($_->{addr}, Socket::NI_NUMERICHOST))[1]) for @res
import java.net.InetAddress; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.UnknownHostException; class DnsQuery { public static void main(String[] args) { try { InetAddress[] ipAddr = InetAddress.getAllByName("www.kame.net"); for(int i=0; i < ipAddr.length ; i++) { if (ipAddr[i] instanceof Inet4Address) { System.out.println("IPv4 : " + ipAddr[i].getHostAddress()); } else if (ipAddr[i] instanceof Inet6Address) { System.out.println("IPv6 : " + ipAddr[i].getHostAddress()); } } } catch (UnknownHostException uhe) { System.err.println("unknown host"); } } }
Maintain the same structure and functionality when rewriting this code in Python.
use feature 'say'; use Socket qw(getaddrinfo getnameinfo); my ($err, @res) = getaddrinfo('orange.kame.net', 0, { protocol=>Socket::IPPROTO_TCP } ); die "getaddrinfo error: $err" if $err; say ((getnameinfo($_->{addr}, Socket::NI_NUMERICHOST))[1]) for @res
>>> import socket >>> ips = set(i[4][0] for i in socket.getaddrinfo('www.kame.net', 80)) >>> for ip in ips: print ip ... 2001:200:dff:fff1:216:3eff:feb1:44d7 203.178.141.194
Port the following code from Perl to VB with equivalent syntax and logic.
use feature 'say'; use Socket qw(getaddrinfo getnameinfo); my ($err, @res) = getaddrinfo('orange.kame.net', 0, { protocol=>Socket::IPPROTO_TCP } ); die "getaddrinfo error: $err" if $err; say ((getnameinfo($_->{addr}, Socket::NI_NUMERICHOST))[1]) for @res
#include "win\winsock2.bi" Function GetSiteAddress(stuff As String = "www.freebasic.net") As Long Dim As WSADATA _wsadate Dim As in_addr addr Dim As hostent Ptr res Dim As Integer i = 0 WSAStartup(MAKEWORD(2,2),@_wsadate) res = gethostbyname(stuff) If res Then Print !"\nURL: "; stuff While (res->h_addr_list[i] <> 0) addr.s_addr = *(Cast (Ulong Ptr,res->h_addr_list[i])) Print "IPv4 address: ";*inet_ntoa(addr) i+=1 Wend WSACleanup() Return 1 Else Print "website error?" Return 0 End If End Function GetSiteAddress "rosettacode.org" GetSiteAddress "www.kame.net" Sleep
Transform the following Perl implementation into Go, maintaining the same output and logic.
use feature 'say'; use Socket qw(getaddrinfo getnameinfo); my ($err, @res) = getaddrinfo('orange.kame.net', 0, { protocol=>Socket::IPPROTO_TCP } ); die "getaddrinfo error: $err" if $err; say ((getnameinfo($_->{addr}, Socket::NI_NUMERICHOST))[1]) for @res
package main import ( "fmt" "net" ) func main() { if addrs, err := net.LookupHost("www.kame.net"); err == nil { fmt.Println(addrs) } else { fmt.Println(err) } }
Convert this PowerShell block to C, preserving its control flow and logic.
$DNS = Resolve-DnsName -Name www.kame.net Write-Host "IPv4:" $DNS.IP4Address "`nIPv6:" $DNS.IP6Address
#include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo("www.kame.net", NULL, &hints, &res0); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); exit(1); } for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); } else { printf("%s\n", host); } } freeaddrinfo(res0); return 0; }
Write the same code in C# as shown below in PowerShell.
$DNS = Resolve-DnsName -Name www.kame.net Write-Host "IPv4:" $DNS.IP4Address "`nIPv6:" $DNS.IP6Address
private string LookupDns(string s) { try { System.Net.IPHostEntry ip = System.Net.Dns.GetHostEntry(s); string result = ip.AddressList[0].ToString(); for (int i = 1; i < ip.AddressList.Length; ++i) result += ", " + ip.AddressList[i].ToString(); return result; } catch (System.Net.Sockets.SocketException se) { return se.Message; } }
Port the following code from PowerShell to C++ with equivalent syntax and logic.
$DNS = Resolve-DnsName -Name www.kame.net Write-Host "IPv4:" $DNS.IP4Address "`nIPv6:" $DNS.IP6Address
#include <Rcpp.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace Rcpp ; CharacterVector getNameInfo(std::string fqdn) { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo(fqdn.c_str(), NULL, &hints, &res0); if (error) { return(NA_STRING); } int i = 0 ; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { i++ ; } } CharacterVector results(i) ; i = 0; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { results[i++] = host ; } } freeaddrinfo(res0); return(results) ; }
Translate this program into Java but keep the logic exactly as in PowerShell.
$DNS = Resolve-DnsName -Name www.kame.net Write-Host "IPv4:" $DNS.IP4Address "`nIPv6:" $DNS.IP6Address
import java.net.InetAddress; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.UnknownHostException; class DnsQuery { public static void main(String[] args) { try { InetAddress[] ipAddr = InetAddress.getAllByName("www.kame.net"); for(int i=0; i < ipAddr.length ; i++) { if (ipAddr[i] instanceof Inet4Address) { System.out.println("IPv4 : " + ipAddr[i].getHostAddress()); } else if (ipAddr[i] instanceof Inet6Address) { System.out.println("IPv6 : " + ipAddr[i].getHostAddress()); } } } catch (UnknownHostException uhe) { System.err.println("unknown host"); } } }
Write a version of this PowerShell function in Python with identical behavior.
$DNS = Resolve-DnsName -Name www.kame.net Write-Host "IPv4:" $DNS.IP4Address "`nIPv6:" $DNS.IP6Address
>>> import socket >>> ips = set(i[4][0] for i in socket.getaddrinfo('www.kame.net', 80)) >>> for ip in ips: print ip ... 2001:200:dff:fff1:216:3eff:feb1:44d7 203.178.141.194
Rewrite the snippet below in VB so it works the same as the original PowerShell code.
$DNS = Resolve-DnsName -Name www.kame.net Write-Host "IPv4:" $DNS.IP4Address "`nIPv6:" $DNS.IP6Address
#include "win\winsock2.bi" Function GetSiteAddress(stuff As String = "www.freebasic.net") As Long Dim As WSADATA _wsadate Dim As in_addr addr Dim As hostent Ptr res Dim As Integer i = 0 WSAStartup(MAKEWORD(2,2),@_wsadate) res = gethostbyname(stuff) If res Then Print !"\nURL: "; stuff While (res->h_addr_list[i] <> 0) addr.s_addr = *(Cast (Ulong Ptr,res->h_addr_list[i])) Print "IPv4 address: ";*inet_ntoa(addr) i+=1 Wend WSACleanup() Return 1 Else Print "website error?" Return 0 End If End Function GetSiteAddress "rosettacode.org" GetSiteAddress "www.kame.net" Sleep
Ensure the translated Go code behaves exactly like the original PowerShell snippet.
$DNS = Resolve-DnsName -Name www.kame.net Write-Host "IPv4:" $DNS.IP4Address "`nIPv6:" $DNS.IP6Address
package main import ( "fmt" "net" ) func main() { if addrs, err := net.LookupHost("www.kame.net"); err == nil { fmt.Println(addrs) } else { fmt.Println(err) } }
Write the same algorithm in C as shown in this R implementation.
library(Rcpp) sourceCpp("dns.cpp") getNameInfo("www.kame.net")
#include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo("www.kame.net", NULL, &hints, &res0); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); exit(1); } for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); } else { printf("%s\n", host); } } freeaddrinfo(res0); return 0; }
Can you help me rewrite this code in C# instead of R, keeping it the same logically?
library(Rcpp) sourceCpp("dns.cpp") getNameInfo("www.kame.net")
private string LookupDns(string s) { try { System.Net.IPHostEntry ip = System.Net.Dns.GetHostEntry(s); string result = ip.AddressList[0].ToString(); for (int i = 1; i < ip.AddressList.Length; ++i) result += ", " + ip.AddressList[i].ToString(); return result; } catch (System.Net.Sockets.SocketException se) { return se.Message; } }
Produce a functionally identical C++ code for the snippet given in R.
library(Rcpp) sourceCpp("dns.cpp") getNameInfo("www.kame.net")
#include <Rcpp.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace Rcpp ; CharacterVector getNameInfo(std::string fqdn) { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo(fqdn.c_str(), NULL, &hints, &res0); if (error) { return(NA_STRING); } int i = 0 ; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { i++ ; } } CharacterVector results(i) ; i = 0; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { results[i++] = host ; } } freeaddrinfo(res0); return(results) ; }
Change the programming language of this snippet from R to Java without modifying what it does.
library(Rcpp) sourceCpp("dns.cpp") getNameInfo("www.kame.net")
import java.net.InetAddress; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.UnknownHostException; class DnsQuery { public static void main(String[] args) { try { InetAddress[] ipAddr = InetAddress.getAllByName("www.kame.net"); for(int i=0; i < ipAddr.length ; i++) { if (ipAddr[i] instanceof Inet4Address) { System.out.println("IPv4 : " + ipAddr[i].getHostAddress()); } else if (ipAddr[i] instanceof Inet6Address) { System.out.println("IPv6 : " + ipAddr[i].getHostAddress()); } } } catch (UnknownHostException uhe) { System.err.println("unknown host"); } } }
Keep all operations the same but rewrite the snippet in Python.
library(Rcpp) sourceCpp("dns.cpp") getNameInfo("www.kame.net")
>>> import socket >>> ips = set(i[4][0] for i in socket.getaddrinfo('www.kame.net', 80)) >>> for ip in ips: print ip ... 2001:200:dff:fff1:216:3eff:feb1:44d7 203.178.141.194
Produce a functionally identical VB code for the snippet given in R.
library(Rcpp) sourceCpp("dns.cpp") getNameInfo("www.kame.net")
#include "win\winsock2.bi" Function GetSiteAddress(stuff As String = "www.freebasic.net") As Long Dim As WSADATA _wsadate Dim As in_addr addr Dim As hostent Ptr res Dim As Integer i = 0 WSAStartup(MAKEWORD(2,2),@_wsadate) res = gethostbyname(stuff) If res Then Print !"\nURL: "; stuff While (res->h_addr_list[i] <> 0) addr.s_addr = *(Cast (Ulong Ptr,res->h_addr_list[i])) Print "IPv4 address: ";*inet_ntoa(addr) i+=1 Wend WSACleanup() Return 1 Else Print "website error?" Return 0 End If End Function GetSiteAddress "rosettacode.org" GetSiteAddress "www.kame.net" Sleep
Keep all operations the same but rewrite the snippet in Go.
library(Rcpp) sourceCpp("dns.cpp") getNameInfo("www.kame.net")
package main import ( "fmt" "net" ) func main() { if addrs, err := net.LookupHost("www.kame.net"); err == nil { fmt.Println(addrs) } else { fmt.Println(err) } }
Rewrite the snippet below in C so it works the same as the original Racket code.
#lang racket (require net/dns) (dns-get-address "8.8.8.8" "www.kame.net")
#include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo("www.kame.net", NULL, &hints, &res0); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); exit(1); } for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); } else { printf("%s\n", host); } } freeaddrinfo(res0); return 0; }
Transform the following Racket implementation into C#, maintaining the same output and logic.
#lang racket (require net/dns) (dns-get-address "8.8.8.8" "www.kame.net")
private string LookupDns(string s) { try { System.Net.IPHostEntry ip = System.Net.Dns.GetHostEntry(s); string result = ip.AddressList[0].ToString(); for (int i = 1; i < ip.AddressList.Length; ++i) result += ", " + ip.AddressList[i].ToString(); return result; } catch (System.Net.Sockets.SocketException se) { return se.Message; } }
Change the following Racket code into C++ without altering its purpose.
#lang racket (require net/dns) (dns-get-address "8.8.8.8" "www.kame.net")
#include <Rcpp.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace Rcpp ; CharacterVector getNameInfo(std::string fqdn) { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo(fqdn.c_str(), NULL, &hints, &res0); if (error) { return(NA_STRING); } int i = 0 ; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { i++ ; } } CharacterVector results(i) ; i = 0; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { results[i++] = host ; } } freeaddrinfo(res0); return(results) ; }
Please provide an equivalent version of this Racket code in Java.
#lang racket (require net/dns) (dns-get-address "8.8.8.8" "www.kame.net")
import java.net.InetAddress; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.UnknownHostException; class DnsQuery { public static void main(String[] args) { try { InetAddress[] ipAddr = InetAddress.getAllByName("www.kame.net"); for(int i=0; i < ipAddr.length ; i++) { if (ipAddr[i] instanceof Inet4Address) { System.out.println("IPv4 : " + ipAddr[i].getHostAddress()); } else if (ipAddr[i] instanceof Inet6Address) { System.out.println("IPv6 : " + ipAddr[i].getHostAddress()); } } } catch (UnknownHostException uhe) { System.err.println("unknown host"); } } }
Ensure the translated Python code behaves exactly like the original Racket snippet.
#lang racket (require net/dns) (dns-get-address "8.8.8.8" "www.kame.net")
>>> import socket >>> ips = set(i[4][0] for i in socket.getaddrinfo('www.kame.net', 80)) >>> for ip in ips: print ip ... 2001:200:dff:fff1:216:3eff:feb1:44d7 203.178.141.194
Produce a functionally identical VB code for the snippet given in Racket.
#lang racket (require net/dns) (dns-get-address "8.8.8.8" "www.kame.net")
#include "win\winsock2.bi" Function GetSiteAddress(stuff As String = "www.freebasic.net") As Long Dim As WSADATA _wsadate Dim As in_addr addr Dim As hostent Ptr res Dim As Integer i = 0 WSAStartup(MAKEWORD(2,2),@_wsadate) res = gethostbyname(stuff) If res Then Print !"\nURL: "; stuff While (res->h_addr_list[i] <> 0) addr.s_addr = *(Cast (Ulong Ptr,res->h_addr_list[i])) Print "IPv4 address: ";*inet_ntoa(addr) i+=1 Wend WSACleanup() Return 1 Else Print "website error?" Return 0 End If End Function GetSiteAddress "rosettacode.org" GetSiteAddress "www.kame.net" Sleep
Change the following Racket code into Go without altering its purpose.
#lang racket (require net/dns) (dns-get-address "8.8.8.8" "www.kame.net")
package main import ( "fmt" "net" ) func main() { if addrs, err := net.LookupHost("www.kame.net"); err == nil { fmt.Println(addrs) } else { fmt.Println(err) } }
Produce a language-to-language conversion: from REXX to C, same semantics.
options replace format comments java crossref symbols nobinary ir = InetAddress addresses = InetAddress[] InetAddress.getAllByName('www.kame.net') loop ir over addresses if ir <= Inet4Address then do say 'IPv4 :' ir.getHostAddress end if ir <= Inet6Address then do say 'IPv6 :' ir.getHostAddress end end ir
#include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo("www.kame.net", NULL, &hints, &res0); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); exit(1); } for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); } else { printf("%s\n", host); } } freeaddrinfo(res0); return 0; }
Convert this REXX block to C#, preserving its control flow and logic.
options replace format comments java crossref symbols nobinary ir = InetAddress addresses = InetAddress[] InetAddress.getAllByName('www.kame.net') loop ir over addresses if ir <= Inet4Address then do say 'IPv4 :' ir.getHostAddress end if ir <= Inet6Address then do say 'IPv6 :' ir.getHostAddress end end ir
private string LookupDns(string s) { try { System.Net.IPHostEntry ip = System.Net.Dns.GetHostEntry(s); string result = ip.AddressList[0].ToString(); for (int i = 1; i < ip.AddressList.Length; ++i) result += ", " + ip.AddressList[i].ToString(); return result; } catch (System.Net.Sockets.SocketException se) { return se.Message; } }
Produce a language-to-language conversion: from REXX to C++, same semantics.
options replace format comments java crossref symbols nobinary ir = InetAddress addresses = InetAddress[] InetAddress.getAllByName('www.kame.net') loop ir over addresses if ir <= Inet4Address then do say 'IPv4 :' ir.getHostAddress end if ir <= Inet6Address then do say 'IPv6 :' ir.getHostAddress end end ir
#include <Rcpp.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace Rcpp ; CharacterVector getNameInfo(std::string fqdn) { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo(fqdn.c_str(), NULL, &hints, &res0); if (error) { return(NA_STRING); } int i = 0 ; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { i++ ; } } CharacterVector results(i) ; i = 0; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { results[i++] = host ; } } freeaddrinfo(res0); return(results) ; }
Port the following code from REXX to Java with equivalent syntax and logic.
options replace format comments java crossref symbols nobinary ir = InetAddress addresses = InetAddress[] InetAddress.getAllByName('www.kame.net') loop ir over addresses if ir <= Inet4Address then do say 'IPv4 :' ir.getHostAddress end if ir <= Inet6Address then do say 'IPv6 :' ir.getHostAddress end end ir
import java.net.InetAddress; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.UnknownHostException; class DnsQuery { public static void main(String[] args) { try { InetAddress[] ipAddr = InetAddress.getAllByName("www.kame.net"); for(int i=0; i < ipAddr.length ; i++) { if (ipAddr[i] instanceof Inet4Address) { System.out.println("IPv4 : " + ipAddr[i].getHostAddress()); } else if (ipAddr[i] instanceof Inet6Address) { System.out.println("IPv6 : " + ipAddr[i].getHostAddress()); } } } catch (UnknownHostException uhe) { System.err.println("unknown host"); } } }
Produce a functionally identical Python code for the snippet given in REXX.
options replace format comments java crossref symbols nobinary ir = InetAddress addresses = InetAddress[] InetAddress.getAllByName('www.kame.net') loop ir over addresses if ir <= Inet4Address then do say 'IPv4 :' ir.getHostAddress end if ir <= Inet6Address then do say 'IPv6 :' ir.getHostAddress end end ir
>>> import socket >>> ips = set(i[4][0] for i in socket.getaddrinfo('www.kame.net', 80)) >>> for ip in ips: print ip ... 2001:200:dff:fff1:216:3eff:feb1:44d7 203.178.141.194
Produce a functionally identical VB code for the snippet given in REXX.
options replace format comments java crossref symbols nobinary ir = InetAddress addresses = InetAddress[] InetAddress.getAllByName('www.kame.net') loop ir over addresses if ir <= Inet4Address then do say 'IPv4 :' ir.getHostAddress end if ir <= Inet6Address then do say 'IPv6 :' ir.getHostAddress end end ir
#include "win\winsock2.bi" Function GetSiteAddress(stuff As String = "www.freebasic.net") As Long Dim As WSADATA _wsadate Dim As in_addr addr Dim As hostent Ptr res Dim As Integer i = 0 WSAStartup(MAKEWORD(2,2),@_wsadate) res = gethostbyname(stuff) If res Then Print !"\nURL: "; stuff While (res->h_addr_list[i] <> 0) addr.s_addr = *(Cast (Ulong Ptr,res->h_addr_list[i])) Print "IPv4 address: ";*inet_ntoa(addr) i+=1 Wend WSACleanup() Return 1 Else Print "website error?" Return 0 End If End Function GetSiteAddress "rosettacode.org" GetSiteAddress "www.kame.net" Sleep
Generate an equivalent Go version of this REXX code.
options replace format comments java crossref symbols nobinary ir = InetAddress addresses = InetAddress[] InetAddress.getAllByName('www.kame.net') loop ir over addresses if ir <= Inet4Address then do say 'IPv4 :' ir.getHostAddress end if ir <= Inet6Address then do say 'IPv6 :' ir.getHostAddress end end ir
package main import ( "fmt" "net" ) func main() { if addrs, err := net.LookupHost("www.kame.net"); err == nil { fmt.Println(addrs) } else { fmt.Println(err) } }
Write a version of this Ruby function in C with identical behavior.
require "socket" Socket::Addrinfo.resolve( "www.kame.net", 80, type: Socket::Type::STREAM ).each { |a| puts a.ip_address.address }
#include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo("www.kame.net", NULL, &hints, &res0); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); exit(1); } for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); } else { printf("%s\n", host); } } freeaddrinfo(res0); return 0; }
Preserve the algorithm and functionality while converting the code from Ruby to C#.
require "socket" Socket::Addrinfo.resolve( "www.kame.net", 80, type: Socket::Type::STREAM ).each { |a| puts a.ip_address.address }
private string LookupDns(string s) { try { System.Net.IPHostEntry ip = System.Net.Dns.GetHostEntry(s); string result = ip.AddressList[0].ToString(); for (int i = 1; i < ip.AddressList.Length; ++i) result += ", " + ip.AddressList[i].ToString(); return result; } catch (System.Net.Sockets.SocketException se) { return se.Message; } }
Rewrite the snippet below in C++ so it works the same as the original Ruby code.
require "socket" Socket::Addrinfo.resolve( "www.kame.net", 80, type: Socket::Type::STREAM ).each { |a| puts a.ip_address.address }
#include <Rcpp.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace Rcpp ; CharacterVector getNameInfo(std::string fqdn) { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo(fqdn.c_str(), NULL, &hints, &res0); if (error) { return(NA_STRING); } int i = 0 ; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { i++ ; } } CharacterVector results(i) ; i = 0; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { results[i++] = host ; } } freeaddrinfo(res0); return(results) ; }
Write the same code in Python as shown below in Ruby.
require "socket" Socket::Addrinfo.resolve( "www.kame.net", 80, type: Socket::Type::STREAM ).each { |a| puts a.ip_address.address }
>>> import socket >>> ips = set(i[4][0] for i in socket.getaddrinfo('www.kame.net', 80)) >>> for ip in ips: print ip ... 2001:200:dff:fff1:216:3eff:feb1:44d7 203.178.141.194
Write a version of this Ruby function in VB with identical behavior.
require "socket" Socket::Addrinfo.resolve( "www.kame.net", 80, type: Socket::Type::STREAM ).each { |a| puts a.ip_address.address }
#include "win\winsock2.bi" Function GetSiteAddress(stuff As String = "www.freebasic.net") As Long Dim As WSADATA _wsadate Dim As in_addr addr Dim As hostent Ptr res Dim As Integer i = 0 WSAStartup(MAKEWORD(2,2),@_wsadate) res = gethostbyname(stuff) If res Then Print !"\nURL: "; stuff While (res->h_addr_list[i] <> 0) addr.s_addr = *(Cast (Ulong Ptr,res->h_addr_list[i])) Print "IPv4 address: ";*inet_ntoa(addr) i+=1 Wend WSACleanup() Return 1 Else Print "website error?" Return 0 End If End Function GetSiteAddress "rosettacode.org" GetSiteAddress "www.kame.net" Sleep
Write the same algorithm in Go as shown in this Ruby implementation.
require "socket" Socket::Addrinfo.resolve( "www.kame.net", 80, type: Socket::Type::STREAM ).each { |a| puts a.ip_address.address }
package main import ( "fmt" "net" ) func main() { if addrs, err := net.LookupHost("www.kame.net"); err == nil { fmt.Println(addrs) } else { fmt.Println(err) } }
Convert this Scala snippet to C and keep its semantics consistent.
import java.net.InetAddress import java.net.Inet4Address import java.net.Inet6Address fun showIPAddresses(host: String) { try { val ipas = InetAddress.getAllByName(host) println("The IP address(es) for '$host' is/are:\n") for (ipa in ipas) { print(when (ipa) { is Inet4Address -> " ipv4 : " is Inet6Address -> " ipv6 : " else -> " ipv? : " }) println(ipa.hostAddress) } } catch (ex: Exception) { println(ex.message) } } fun main(args: Array<String>) { showIPAddresses("www.kame.net") }
#include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo("www.kame.net", NULL, &hints, &res0); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); exit(1); } for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (error) { fprintf(stderr, "%s\n", gai_strerror(error)); } else { printf("%s\n", host); } } freeaddrinfo(res0); return 0; }
Produce a language-to-language conversion: from Scala to C#, same semantics.
import java.net.InetAddress import java.net.Inet4Address import java.net.Inet6Address fun showIPAddresses(host: String) { try { val ipas = InetAddress.getAllByName(host) println("The IP address(es) for '$host' is/are:\n") for (ipa in ipas) { print(when (ipa) { is Inet4Address -> " ipv4 : " is Inet6Address -> " ipv6 : " else -> " ipv? : " }) println(ipa.hostAddress) } } catch (ex: Exception) { println(ex.message) } } fun main(args: Array<String>) { showIPAddresses("www.kame.net") }
private string LookupDns(string s) { try { System.Net.IPHostEntry ip = System.Net.Dns.GetHostEntry(s); string result = ip.AddressList[0].ToString(); for (int i = 1; i < ip.AddressList.Length; ++i) result += ", " + ip.AddressList[i].ToString(); return result; } catch (System.Net.Sockets.SocketException se) { return se.Message; } }
Rewrite this program in C++ while keeping its functionality equivalent to the Scala version.
import java.net.InetAddress import java.net.Inet4Address import java.net.Inet6Address fun showIPAddresses(host: String) { try { val ipas = InetAddress.getAllByName(host) println("The IP address(es) for '$host' is/are:\n") for (ipa in ipas) { print(when (ipa) { is Inet4Address -> " ipv4 : " is Inet6Address -> " ipv6 : " else -> " ipv? : " }) println(ipa.hostAddress) } } catch (ex: Exception) { println(ex.message) } } fun main(args: Array<String>) { showIPAddresses("www.kame.net") }
#include <Rcpp.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #include <stdlib.h> #include <string.h> using namespace Rcpp ; CharacterVector getNameInfo(std::string fqdn) { struct addrinfo hints, *res, *res0; int error; char host[NI_MAXHOST]; memset(&hints, 0, sizeof hints); hints.ai_family = PF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo(fqdn.c_str(), NULL, &hints, &res0); if (error) { return(NA_STRING); } int i = 0 ; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { i++ ; } } CharacterVector results(i) ; i = 0; for (res = res0; res; res = res->ai_next) { error = getnameinfo(res->ai_addr, res->ai_addrlen, host, sizeof host, NULL, 0, NI_NUMERICHOST); if (!error) { results[i++] = host ; } } freeaddrinfo(res0); return(results) ; }
Generate an equivalent Java version of this Scala code.
import java.net.InetAddress import java.net.Inet4Address import java.net.Inet6Address fun showIPAddresses(host: String) { try { val ipas = InetAddress.getAllByName(host) println("The IP address(es) for '$host' is/are:\n") for (ipa in ipas) { print(when (ipa) { is Inet4Address -> " ipv4 : " is Inet6Address -> " ipv6 : " else -> " ipv? : " }) println(ipa.hostAddress) } } catch (ex: Exception) { println(ex.message) } } fun main(args: Array<String>) { showIPAddresses("www.kame.net") }
import java.net.InetAddress; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.UnknownHostException; class DnsQuery { public static void main(String[] args) { try { InetAddress[] ipAddr = InetAddress.getAllByName("www.kame.net"); for(int i=0; i < ipAddr.length ; i++) { if (ipAddr[i] instanceof Inet4Address) { System.out.println("IPv4 : " + ipAddr[i].getHostAddress()); } else if (ipAddr[i] instanceof Inet6Address) { System.out.println("IPv6 : " + ipAddr[i].getHostAddress()); } } } catch (UnknownHostException uhe) { System.err.println("unknown host"); } } }
Rewrite this program in Python while keeping its functionality equivalent to the Scala version.
import java.net.InetAddress import java.net.Inet4Address import java.net.Inet6Address fun showIPAddresses(host: String) { try { val ipas = InetAddress.getAllByName(host) println("The IP address(es) for '$host' is/are:\n") for (ipa in ipas) { print(when (ipa) { is Inet4Address -> " ipv4 : " is Inet6Address -> " ipv6 : " else -> " ipv? : " }) println(ipa.hostAddress) } } catch (ex: Exception) { println(ex.message) } } fun main(args: Array<String>) { showIPAddresses("www.kame.net") }
>>> import socket >>> ips = set(i[4][0] for i in socket.getaddrinfo('www.kame.net', 80)) >>> for ip in ips: print ip ... 2001:200:dff:fff1:216:3eff:feb1:44d7 203.178.141.194
Write the same code in VB as shown below in Scala.
import java.net.InetAddress import java.net.Inet4Address import java.net.Inet6Address fun showIPAddresses(host: String) { try { val ipas = InetAddress.getAllByName(host) println("The IP address(es) for '$host' is/are:\n") for (ipa in ipas) { print(when (ipa) { is Inet4Address -> " ipv4 : " is Inet6Address -> " ipv6 : " else -> " ipv? : " }) println(ipa.hostAddress) } } catch (ex: Exception) { println(ex.message) } } fun main(args: Array<String>) { showIPAddresses("www.kame.net") }
#include "win\winsock2.bi" Function GetSiteAddress(stuff As String = "www.freebasic.net") As Long Dim As WSADATA _wsadate Dim As in_addr addr Dim As hostent Ptr res Dim As Integer i = 0 WSAStartup(MAKEWORD(2,2),@_wsadate) res = gethostbyname(stuff) If res Then Print !"\nURL: "; stuff While (res->h_addr_list[i] <> 0) addr.s_addr = *(Cast (Ulong Ptr,res->h_addr_list[i])) Print "IPv4 address: ";*inet_ntoa(addr) i+=1 Wend WSACleanup() Return 1 Else Print "website error?" Return 0 End If End Function GetSiteAddress "rosettacode.org" GetSiteAddress "www.kame.net" Sleep
Ensure the translated Go code behaves exactly like the original Scala snippet.
import java.net.InetAddress import java.net.Inet4Address import java.net.Inet6Address fun showIPAddresses(host: String) { try { val ipas = InetAddress.getAllByName(host) println("The IP address(es) for '$host' is/are:\n") for (ipa in ipas) { print(when (ipa) { is Inet4Address -> " ipv4 : " is Inet6Address -> " ipv6 : " else -> " ipv? : " }) println(ipa.hostAddress) } } catch (ex: Exception) { println(ex.message) } } fun main(args: Array<String>) { showIPAddresses("www.kame.net") }
package main import ( "fmt" "net" ) func main() { if addrs, err := net.LookupHost("www.kame.net"); err == nil { fmt.Println(addrs) } else { fmt.Println(err) } }