- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我是 Rust 的新手,我正在尝试编写 hostname
实用程序,形成一个 core-utils 的 rust backport。更多信息:https://github.com/uutils/coreutils
我有以下程序:
use std::libc;
extern {
pub fn gethostname(name: *libc::c_char, size: libc::size_t) -> libc::c_int;
}
fn main() {
unsafe {
let len = 34 as uint;
let mut buf = std::vec::with_capacity(len);
std::vec::raw::set_len (&mut buf, len as uint);
gethostname (std::vec::raw::to_ptr(buf), len as u64);
println(format!("{:?}", buf));
println(format!("{:?}", len));
//println(std::str::from_chars(buf));
}
}
我试图打印到 char vector 中的任何 gethostname 副本,但我得到的东西看起来不像字符串。
~[65i8, 108i8, 97i8, 110i8, 115i8, 45i8, 77i8, 97i8, 99i8, 66i8, 111i8, 111i8, 107i8, 45i8, 80i8, 114i8, 111i8, 46i8, 108i8, 111i8, 99i8, 97i8, 108i8, 0i8, 0i8, 0i8, 0i8, 0i8, 0i8, 0i8, 0i8, 0i8, 0i8, 0i8]
34u
我需要做什么才能:1. 确保 gethostname() 正在做我认为正在做的事情?2. 确保我正确编码?
最佳答案
buf
是一个 ~[u8]
并按原样打印(即任意数字的 vector ); std::str::from_utf8_owned
和 std::str::from_utf8_slice
将(假定的)UTF-8 [u8]
转换为 str
。 (后者在 master 中称为 from_utf8
;0.8 from_utf8
是坏的,已被删除,它进行分配和复制,而这两者都不做。)
因此,类似于
use std::{libc, str, vec};
extern {
pub fn gethostname(name: *mut libc::c_char, size: libc::size_t) -> libc::c_int;
}
fn main() {
let len = 34u;
let mut buf = std::vec::from_elem(len, 0u8);
let err = unsafe {gethostname (vec::raw::to_mut_ptr(buf) as *mut i8, len as u64)};
if err != 0 { println("oops, gethostname failed"); return; }
// find the first 0 byte (i.e. just after the data that gethostname wrote)
let actual_len = buf.iter().position(|byte| *byte == 0).unwrap_or(len);
// trim the hostname to the actual data written
println(str::from_utf8_slice(buf.slice_to(actual_len)));
}
将打印主机名。
文档:
Iterator.position
Option.unwrap_or
[].slice_to
(主文档因为 0.8 版本没有任何文本,但是功能没有改变。)关于c - Rust,如何与 gethostname() std::libc 交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20450637/
我正在运行一个虚拟机,它提供以下值以及运行代码的时间(通过在网站上执行一些操作) socket.getfqdn() x-vps-01.abc.it socket.gethostname() x-vps
我在使用时遇到问题Java 中的 InetAdress.getHostName() 类。在某些情况下,java 类没有得到正确的结果。在这种情况下,我只收到 IP 地址(如预期的错误)。 这很可能是我
我需要获取我正在使用的系统主机名 gethostname功能 但是它失败了,错误代码是 10093 WSANOTINITIALISED 10093 Successful WSAStartup not
当我设置我的 Macbook Pro 时,我将主机名更改为我的名字 Tristan .我正在尝试使用套接字并调用: socket.gethostbyname(socket.gethostname())
我正在尝试使用此方法获取主机名/计算机名。不幸的是,我只能获得 localhost 而不是其他计算机。 private String getHostName(String _strIP) {
我发现 gethostname() 函数返回本地计算机的标准主机名,但我对术语“主机名”有点困惑,它是函数所在计算机的名称吗?被调用或计算机在网络中连接的服务器的名称... cha
我尝试使用 gethostname() 分配一个变量 (1)并使用 $_SERVER (2)我不断收到 ReferenceError: gethostname is not Defined。 我想要的
我有以下小代码片段: InetAddress address = InetAddress.getByName(host); if(address.isReachable
我遇到了更改主机名后我的应用程序失败的问题。在调查中,我发现 Dns.GetHostName() 仍在返回旧主机名。 我正在尝试获取有关函数 Dns.GetHostName() 的主机名来源的信息。
我有一个从设备接收数据的线程。当到达 myDatagramPacket.getAddress().getHostName() 行时,它会停止 5-6 秒,然后继续。 当我尝试像“192.168.1.1
正在做一个python套接字教程,整个代码库如下 import socket as so s = so.socket() host = so.gethostname() port = 12345 s.
我正在修复对 gethostname() 的调用,其中旧代码将 hostName 数组声明为 10 个字符,导致边界错误,因为主机名大于该值。我的修复方法是使用 MAXHOSTNAMELEN 声明 h
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
只是标题,它们之间有什么区别?在 python 中,socket.gethostbyname(socket.gethostname()) 和 socket.gethostbyname(socket.g
我需要帮助将主机名转换为 ip 并插入到 sockaddr_in->sin_addr 以便能够分配给 char。例如我输入:localhost,它给了我 127.0.0.1 我找到了代码,但我不知道为
为什么有时,Java inetAddress.getHostName(); 给出完全限定的域名,有时只给出没有域的主机名?是否可以在系统级别进行调整,以便仅通过调用该方法来获取 FQDN 或主机名。我
我正在使用一个使用开源 java 库 (Calimero) 的 Android 应用程序。我的代码尝试使用以下代码获取主机名和端口: private static KNXNetworkLinkIP c
在我的 docker 容器(Java 应用程序)中,调用 InetAddress.getLocalHost().getHostName()返回我们在 docker run --name 'myserv
我正在使用 ITK(通过 itk 的 .lib)制作一个 C++ 项目。在 qmake 上,我像这样与 ITK 库建立链接:在 .pri 中: MyITK { CONFIG_FOUND += MyIT
我正在开发一个 After Effects 插件,我正在尝试集成 raknet,这是一个 C++ 网络库。当 raknet 库试图通过调用获取 ipv4 地址时 gethostbyname 然后它抛出
我是一名优秀的程序员,十分优秀!