小编Ban*_*ana的帖子

getHostAddress()返回一个反向的ip地址

我正在尝试使用WifiManager和WifiInfo类获取我的手机IP地址.

它返回正确的IP地址反转.

public String getWifiIpAddress() {
    WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
    WifiInfo wi = wm.getConnectionInfo();

    byte[] ipAddress = BigInteger.valueOf(wi.getIpAddress()).toByteArray();
    try {
        InetAddress myAddr = InetAddress.getByAddress(ipAddress);
        String hostAddr = myAddr.getHostAddress();
        return hostAddr;
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return "";
}
Run Code Online (Sandbox Code Playgroud)

结果:73.0.168.192

java android ipv4

8
推荐指数
1
解决办法
1951
查看次数

Mac中的C Write()错误


我正试图在我的Macbook Air上的文本文件中写字符,但它似乎没有用.
我尝试通过Xcode和Terminal进行编译.

但结果是一样的:
文件描述:3
写()错误!

这是代码.

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

void Error_handling(char* message);

int main() {
    int fd;

    char buf[] = "Let's go! \n";

    fd = open("data.txt", O_CREAT|O_RDONLY|O_TRUNC);
    if (fd == -1)
        Error_handling("open() Error! \n");

    printf("File Descripter: %d \n", fd);

    if(write(fd, buf, sizeof(buf))==-1)
        Error_handling("write() Error! \n");

    close(fd);

    return 0;
}

void Error_handling(char* message)
{
    fputs(message, stderr);
    exit(1);
}
Run Code Online (Sandbox Code Playgroud)

c

0
推荐指数
1
解决办法
109
查看次数

标签 统计

android ×1

c ×1

ipv4 ×1

java ×1