获取本地ip以及时间

#include <QCoreApplication>
#include <QHostAddress>
#include <QHostInfo>
#include <QDebug>
#include <QTime>
#include <QDateTime>
#include <QTimer>

#define cout qDebug() << "["<<__FILE__<<":"<<__LINE__<<"]"


QString getLocalIP()
{
    //获取本机IPv4地址
    QString hostName = QHostInfo::localHostName();//本机主机名
    QHostInfo hostInfo = QHostInfo::fromName(hostName);
    QString localIP="";
    QList<QHostAddress> addList = hostInfo.addresses();
    if(!addList.isEmpty())
    {
        for(int i = 0;i<addList.count();i++)
        {
            QHostAddress aHost = addList.at(i);
            if(QAbstractSocket::IPv4Protocol==aHost.protocol())
            {
                localIP = aHost.toString();
                break;
            }
        }
    }
   return localIP;
}

void getLocalTime()
{
    QDateTime times = QDateTime::currentDateTime();
    QString time = times.toString("yyyy/MM/dd HH:mm:ss");
    cout<< "Local Time is:" << time;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QString localIP = getLocalIP();
    if (!localIP.isEmpty()) {
        cout << "Local IPv4 address is:" << localIP;
     } else {
        cout << "Failed to get local IPv4 address.";
     }
    QTimer timer;
    QObject::connect(&timer, &QTimer::timeout, &getLocalTime);
    timer.start(1000);
    return a.exec();
}

 windows中

#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#include <string>
#include <ctime>

using namespace std;
#pragma comment(lib, "WS2_32.lib")

const string getIP()
{
    WSADATA WSAData;
    if (WSAStartup(MAKEWORD(2, 2), &WSAData) != 0)
    {
        return "WSAStartup failed.";
    }

    char hostName[256];
    if (gethostname(hostName, sizeof(hostName)) == SOCKET_ERROR)
    {
        WSACleanup();
        return "Get hostname failed.";
    }

    addrinfo hints, * res;
    ZeroMemory(&hints, sizeof(hints));
    hints.ai_family = AF_INET; // AF_UNSPEC for IPv4 or IPv6
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    if (getaddrinfo(hostName, NULL, &hints, &res) != 0)
    {
        WSACleanup();
        return "Get address info failed.";
    }

    char ipStr[INET_ADDRSTRLEN];
    if (inet_ntop(AF_INET, &((struct sockaddr_in*)res->ai_addr)->sin_addr, ipStr, sizeof(ipStr)) == NULL)
    {
        freeaddrinfo(res);
        WSACleanup();
        return "Convert IP to string failed.";
    }

    string ip = ipStr;
    freeaddrinfo(res);
    WSACleanup();
    return ip;
}

const string getLocalTime()
{
    time_t now = time(0);
    tm lt;
    localtime_s(&lt, &now);
    struct SYSTEMTIME
    {
        int wYear;
        int wMonth;
        int wDay;
        int wHour;
        int wMinute;
        int wSecond;
    };
    SYSTEMTIME sys;
    sys.wYear = lt.tm_year + 1900;
    sys.wMonth = lt.tm_mon + 1;
    sys.wDay = lt.tm_mday;
    sys.wHour = lt.tm_hour;
    sys.wMinute = lt.tm_min;
    sys.wSecond = lt.tm_sec;
    // 将SYSTEMTIME转换为字符串
    char buffer[100];
    sprintf_s(buffer, "%04d-%02d-%02d %02d:%02d:%02d",
        sys.wYear, sys.wMonth, sys.wDay,
        sys.wHour, sys.wMinute, sys.wSecond);
    return std::string(buffer);
}

int main()
{
    string time_now = getLocalTime();
    cout << time_now<<" IP地址为:" << getIP() << endl;
    system("pause");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值