❶ c語言查詢本機ip地址的程序
#include <windows.h>
#include <winsock2.h>
❷ 用C語言編寫程序如何獲得外網的IP的地址
//通過ip138網站來查詢外網IP的
#include <afxinet.h>
void CLanChatDlg::GetNetIP()
{
SetDlgItemText(IDC_NET_IP,"正在獲取外網IP");
CString strsource;
CString Address;
CInternetSession mySession(NULL,0);
CHttpFile* myHttpFile=NULL;
Address="http://www.ip138.com/ip2city.asp";//ip138網頁
myHttpFile=(CHttpFile*)mySession.OpenURL(Address);//讀取網路地址
while(myHttpFile->ReadString(strsource))
{ //循環讀取下載來的網頁文本
// AddToLog(strsource);
int begin=0;
begin=strsource.Find("[",0);
if(begin!=-1)//如果找到"[", 則找"]" 中括弧內的文本則是 你的外網ip
{ int end=strsource.Find("]");
m_internetip=strsource.Mid(begin+1,end-begin-1);//提取外網ip
SetDlgItemText(IDC_NET_IP,m_internetip);//在左下角顯示外網ip
}
}
❸ C語言IP地址查詢系統如何實現
C語言如何實現IP地址查詢系統
參考如下:
voidGetNameAndIp()
{
structhostent*host;
structin_addr*ptr;
DWORDdwScope=RESOURCE_CONTEXT;
NETRESOURCE*NetResource=NULL;
HANDLEhEnum;
WNetOpenEnum(dwScope,NULL,NULL,NULL,&hEnum);
WSADATAwsaData;
WSAStartup(MAKEWORD(1,1),&wsaData);
if(hEnum)
{
DWORDCount=0xFFFFFFFF;
DWORDBufferSize=10240;
LPVOIDBuffer=newchar[10240];
WNetEnumResource(hEnum,&Count,Buffer,&BufferSize);
NetResource=(NETRESOURCE*)Buffer;
charszHostName[200];
for(unsignedinti=0;i<BufferSize/sizeof(NETRESOURCE);i++,NetResource++)
{
if(NetResource->dwUsage==RESOURCEUSAGE_CONTAINER&&NetResource->dwType==RESOURCETYPE_ANY)
{
if(NetResource->lpRemoteName)
{
CStringstrFullName=NetResource->lpRemoteName;
if(0==strFullName.Left(2).Compare(_T("\\")))
strFullName=strFullName.Right(strFullName.GetLength()-2);
gethostname(szHostName,strlen(szHostName));
USES_CONVERSION;
char*pchar=T2A(strFullName);
host=gethostbyname(pchar);
if(host==NULL)continue;
ptr=(structin_addr*)host->h_addr_list[0];
stringstr="";
for(intn=0;n<4;n++)
{
CStringaddr;
if(n>0)
{
str+=".";
}
intvalue=(unsignedint)((unsignedchar*)host->h_addr_list[0])[n];
charp[20];
sprintf(p,"%d",value);
str.append(p);
}
std::cout<<"IP:"<<str<<"Name:"<<host->h_name<<std::endl;
}
}
}
deleteBuffer;
WNetCloseEnum(hEnum);
}
WSACleanup();
}
❹ c語言ip判斷
#include<stdio.h>
structIP
{
inta;
charq1;
intb;
charq2;
intc;
charq3;
intd;
}s;
voidyesno()
{
scanf("%d%c%d%c%d%c%d",&s.a,&s.q1,&s.b,&s.q2,&s.c,&s.q3,&s.d);
if(s.a<256&&s.a>0&&s.b<256&&s.b>0&&s.c<256&&s.c>0&&s.d<256&&s.d>0)
{
if(s.q1=='.'&&s.q1=='.'&&s.q1=='.')printf("YES ");
elseprintf("NO ");
}
elseprintf("NO ");
}
main()
{
inti;
for(i=0;i<99;i++)
{yesno();}
}
❺ c#怎麼獲取路由器的ip地址,求代碼
using System.Text;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
public class Router
{
Encoding gb2312 = Encoding.GetEncoding(936);//路由器的web管理系統默認編碼為gb2312
/// <summary>
/// 使用HttpWebRequest對象發送請求
/// </summary>
/// <param name="url"></param>
/// <param name="encoding">編碼</param>
/// <param name="cache">憑證</param>
/// <returns></returns>
private static string SendRequest(string url, Encoding encoding,CredentialCache cache)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
if (cache != null)
{
request.PreAuthenticate = true;
request.Credentials = cache;
}
string html = null;
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader srd = new StreamReader(response.GetResponseStream(), encoding);
html = srd.ReadToEnd();
srd.Close();
response.Close();
}
catch (Exception ex) { html = "FALSE" + ex.Message; }
return html;
}
/// <summary>
/// 獲取路由MAC和外網IP地址
/// </summary>
/// <param name="RouterIP">路由IP地址,就是網關地址了,默認192.168.1.1</param>
/// <param name="UserName">用戶名</param>
/// <param name="Passowrd">密碼</param>
/// <returns></returns>
private string LoadMACWanIP(string RouterIP,string UserName,string Passowrd)
{
CredentialCache cache = new CredentialCache();
string url = "http://" + RouterIP + "/userRpm/StatusRpm.htm";
cache.Add(new Uri(url), "Basic", new NetworkCredential(UserName, Passowrd));
return SendRequest(url, gb2312, cache);
}
}
❻ 只用C語言,怎麼通過域名得到IP地址
查找的方法(07版為例): 1、在左下方開始鍵,點擊之後出現搜索框 2、在搜索框內輸入指令cmd運行 3、進入窗口,按照要求輸入指令:nbtstat -a IP,ip地址為你所查詢主機的ip地址。 4、這樣就可以根據ip泛解析出域名。
❼ 請問 , 用c語言怎樣獲取ip地址啊 謝謝了
看你要獲得那裡的ip地址
如果是本及機的話,就使用windows的API啊
#include "winsock.h"
WORD wVersionRequested;
WSADATA wsaData;
char name[255];
char* ip;
PHOSTENT hostinfo;
wVersionRequested = MAKEWORD( 2, 0 );
if ( WSAStartup( wVersionRequested, &wsaData ) == 0 )
{
if( gethostname ( name, sizeof(name)) == 0)
{
if((hostinfo = gethostbyname(name)) != NULL)
{
ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);
}
}
// ip is ready
WSACleanup( );
}
❽ C語言如何實現IP地址查詢系統
C語言如何實現IP地址查詢系統
參考如下:
voidGetNameAndIp()
{
structhostent*host;
structin_addr*ptr;
DWORDdwScope=RESOURCE_CONTEXT;
NETRESOURCE*NetResource=NULL;
HANDLEhEnum;
WNetOpenEnum(dwScope,NULL,NULL,NULL,&hEnum);
WSADATAwsaData;
WSAStartup(MAKEWORD(1,1),&wsaData);
if(hEnum)
{
DWORDCount=0xFFFFFFFF;
DWORDBufferSize=10240;
LPVOIDBuffer=newchar[10240];
WNetEnumResource(hEnum,&Count,Buffer,&BufferSize);
NetResource=(NETRESOURCE*)Buffer;
charszHostName[200];
for(unsignedinti=0;i<BufferSize/sizeof(NETRESOURCE);i++,NetResource++)
{
if(NetResource->dwUsage==RESOURCEUSAGE_CONTAINER&&NetResource->dwType==RESOURCETYPE_ANY)
{
if(NetResource->lpRemoteName)
{
CStringstrFullName=NetResource->lpRemoteName;
if(0==strFullName.Left(2).Compare(_T("\\")))
strFullName=strFullName.Right(strFullName.GetLength()-2);
gethostname(szHostName,strlen(szHostName));
USES_CONVERSION;
char*pchar=T2A(strFullName);
host=gethostbyname(pchar);
if(host==NULL)continue;
ptr=(structin_addr*)host->h_addr_list[0];
stringstr="";
for(intn=0;n<4;n++)
{
CStringaddr;
if(n>0)
{
str+=".";
}
intvalue=(unsignedint)((unsignedchar*)host->h_addr_list[0])[n];
charp[20];
sprintf(p,"%d",value);
str.append(p);
}
std::cout<<"IP:"<<str<<"Name:"<<host->h_name<<std::endl;
}
}
}
deleteBuffer;
WNetCloseEnum(hEnum);
}
WSACleanup();
}