我想从包含机器当前 IP 地址的应用程序发送一封电子邮件。
我有适当的电子邮件代码,它可以工作。我只需要将 ipaddress 添加到电子邮件的正文中(即我没有以编程方式使用 IP 地址做任何事情)。
我希望有一种非常简单的方法,比如通过系统命令运行 ipconfig 并获取结果文本。
我宁愿不必打开套接字。
谢谢,
我想从包含机器当前 IP 地址的应用程序发送一封电子邮件。
我有适当的电子邮件代码,它可以工作。我只需要将 ipaddress 添加到电子邮件的正文中(即我没有以编程方式使用 IP 地址做任何事情)。
我希望有一种非常简单的方法,比如通过系统命令运行 ipconfig 并获取结果文本。
我宁愿不必打开套接字。
谢谢,
我已将“Jason Heine”(不知道如何标记名称)代码更新为 c++。现在它应该工作了。顺便告诉他。所以这就是我得到的:
using namespace System;
using namespace System::Net;
void main(){
String ^strHostName = String::Empty;
// Getting Ip address of local machine...
// First get the host name of local machine.
strHostName = Dns::GetHostName();
Console::WriteLine("Local Machine's Host Name: " + strHostName);
// Then using host name, get the IP address list..
IPHostEntry^ ipEntry = Dns::GetHostEntry(strHostName);
array<IPAddress^> ^addr = ipEntry->AddressList;
for (int i = 0; i < addr->Length; i++)
{
Console::WriteLine("IP Address {0}: {1} ", i, addr[i]->ToString());
}
Console::ReadKey();
}
我希望这能帮到您。
在我的电脑上测试了 wifi 和 LAN 连接以及一些用于“VMware Player”的虚拟卡,我得到了 4 个 IPv6,然后被 4 个 IPv4 所关注。如果您只需要 IPv4,您可以使用:
using namespace System;
using namespace System::Net;
void main(){
String ^strHostName = String::Empty;
// Getting Ip address of local machine...
// First get the host name of local machine.
strHostName = Dns::GetHostName();
Console::WriteLine("Local Machine's Host Name: " + strHostName);
// Then using host name, get the IP address list..
IPHostEntry^ ipEntry = Dns::GetHostEntry(strHostName);
array<IPAddress^> ^addr = ipEntry->AddressList;
for (int i = 0; i < addr->Length; i++)
{
if(addr[i]->ToString()->Length < 20){
Console::WriteLine("IP Address {0}: {1} ", i, addr[i]->ToString());
}
}
Console::ReadKey();
}
然后我得到只有 IPv4。只是数字从 4 开始。但对我来说没关系。您可以添加一个新变量来重新计算编号:)
使用gethostbyname
(不推荐)或更新的getaddrinfo
. MSDN 链接也包含示例。
编辑:好吧,如果我读到您使用的是 VC++ 而不是 c# 会有所帮助.....
所以你可以忽略我的回答,或者把它作为指导......
给你:
using System;
using System.Net;
namespace testProgram
{
class Program
{
static void Main()
{
String strHostName = string.Empty;
// Getting Ip address of local machine...
// First get the host name of local machine.
strHostName = Dns.GetHostName();
Console.WriteLine("Local Machine's Host Name: " + strHostName);
// Then using host name, get the IP address list..
IPHostEntry ipEntry = Dns.GetHostEntry(strHostName);
IPAddress[] addr = ipEntry.AddressList;
for (int i = 0; i < addr.Length; i++)
{
Console.WriteLine("IP Address {0}: {1} ", i, addr[i].ToString());
}
Console.ReadLine();
}
}
}
这将为您提供所需的所有信息,然后您可以解析出您需要和不需要的信息。
此代码将循环所有适配器并检查第一个已启动的适配器。
#include <afxtempl.h>
#include <afxsock.h>
#include <iphlpapi.h>
u_long GetFirstIpAddressUp(SOCKET s)
{
#define MAX_ADAPTERS 30
#pragma comment(lib, "Iphlpapi.lib")
IP_ADAPTER_ADDRESSES AdapterAddresses[MAX_ADAPTERS];
PIP_ADAPTER_ADDRESSES pAdapterAddresses = AdapterAddresses;
DWORD dwBufLen = sizeof(AdapterAddresses);
if (GetAdaptersAddresses(AF_INET, 0, NULL, AdapterAddresses,&dwBufLen) == ERROR_SUCCESS)
{
do {
if ((pAdapterAddresses->OperStatus == IfOperStatusUp))
{
sockaddr_in* pAdr = (sockaddr_in*)pAdapterAddresses->FirstUnicastAddress->Address.lpSockaddr;
return pAdr->sin_addr.S_un.S_addr;
}
pAdapterAddresses = pAdapterAddresses->Next;
} while(pAdapterAddresses);
}
return INADDR_ANY; // No adapters are up
}
我相信您可能需要查看 winsock2.h
当然 getaddrinfo 适用于您的情况,但我想我会提到,如果您不只是想读取 ip,您可以使用 windows IP Helper api来完成 ipconfig 实用程序的大部分工作。