Windows Sock

cxxer 2010-04-30 11:43:13
C如何调用Windows Sock设置本机IP,掩码,DNS,网关等地址。
...全文
195 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
cxxer 2010-05-01
  • 打赏
  • 举报
回复
如何设置本机IP,掩码,DNS,网关等地址,调用windows sock或其他APIs。获取并更改。
findcsdn 2010-05-01
  • 打赏
  • 举报
回复
IPHLPAPI.dll 中包括了对网络地址设置的API
到下面的地址下载头文件和库文件,把它加入到vc的路径下。
http://d.download.csdn.net/down/1985290/Donneyming

然后就可以用头文件中的函数了,函数的原型和参数MSDN上有介绍。


  • 打赏
  • 举报
回复
#include <windows.h>
#include <stdio.h>
#include <iphlpapi.h>

void Usage(void) {
printf("Usage: Ipchange [ -l ] [ -a -n<index id> -i<ip address> -m<subnet mask> ] "
"[ -d -c<context id>]\n\n"
"\t -l List adapter index IDs and IP Address context ID information\n"
"\t -a Add IP Address option\n"
"\t -d Delete IP Address option\n"
"\t -i IP Address to specify with -a option\n"
"\t -m Subnet Mask to specify with -a option\n"
"\t -c IP context ID for an existing IP address\n"
"\t -n Index ID of an existing network adapter\n");
}

void main(int argc, char *argv[]) {
ULONG NTEContext = 0;
ULONG NTEInstance;
IPAddr NewIP;
IPAddr NewMask;
DWORD Index;
DWORD Context;
CHAR NewIPStr[64];
CHAR NewMaskStr[64];

PIP_ADAPTER_INFO pAdapterInfo, pAdapt;
PIP_ADDR_STRING pAddrStr;
DWORD AdapterInfoSize;
DWORD Err;
BOOL OptList = FALSE;
BOOL OptAdd = FALSE;
BOOL OptDel = FALSE;

NewIPStr[0] = '\0';
NewMaskStr[0] = '\0';
Context = Index = -1;
for (int i = 1; i < argc; i++)
{
if ((argv[i][0] == '-') || (argv[i][0] == '/'))
{
switch(tolower(argv[i][1]))
{
case 'l':
OptList = TRUE;
break;
case 'a':
OptAdd = TRUE;
break;
case 'c':
if (strlen(argv[i]) > 2)
Context = atoi(&argv[i][2]);
break;
case 'd':
OptDel = TRUE;
break;
case 'i':
if (strlen(argv[i]) > 2)
strcpy(NewIPStr, &argv[i][2]);
break;
case 'm':
if (strlen(argv[i]) > 2)
strcpy(NewMaskStr, &argv[i][2]);
break;
case 'n':
if (strlen(argv[i]) > 2)
Index = atoi(&argv[i][2]);
break;
default:
printf("default\n");
Usage();
return;
}
}
else
{
printf("else\n");
Usage();
return;
}
}


// Check options
if ((OptAdd && (Index == -1 || NewIPStr[0] == '\0' || NewMaskStr[0] == '\0'))
|| (OptDel && Context == -1))
{
Usage();
return;
}

// Get sizing information about all adapters
AdapterInfoSize = 0;
if ((Err = GetAdaptersInfo(NULL, &AdapterInfoSize)) != 0)
{
if (Err != ERROR_BUFFER_OVERFLOW)
{
printf("GetAdaptersInfo sizing failed with error %d\n", Err);
return;
}
}

// Allocate memory from sizing information
if ((pAdapterInfo = (PIP_ADAPTER_INFO) GlobalAlloc(GPTR, AdapterInfoSize)) == NULL)
{
printf("Memory allocation error\n");
return;
}

// Get actual adapter information
if ((Err = GetAdaptersInfo(pAdapterInfo, &AdapterInfoSize)) != 0)
{
printf("GetAdaptersInfo failed with error %d\n", Err);
return;
}

if (OptList)
{
printf("MAC Address - Adapter\n"
"Index Context Ip Address Subnet Mask\n"
"--------------------------------------------------------------\n");

pAdapt = pAdapterInfo;

while (pAdapt)
{
for (UINT i=0; i<pAdapt->AddressLength; i++)
{
if (i == (pAdapt->AddressLength - 1))
printf("%.2X - ",(int)pAdapt->Address[i]);
else
printf("%.2X-",(int)pAdapt->Address[i]);
}
printf("%s\n", pAdapt->Description);

pAddrStr = &(pAdapt->IpAddressList);
while(pAddrStr)
{
printf("%-10.d%-10.d%-20.20s%s\n", pAdapt->Index, pAddrStr->Context, pAddrStr->IpAddress.String, pAddrStr->IpMask.String);
pAddrStr = pAddrStr->Next;
}

pAdapt = pAdapt->Next;
}
}

if (OptAdd)
{
NewIP = inet_addr(NewIPStr);
NewMask = inet_addr(NewMaskStr);
if ((Err = AddIPAddress(NewIP, NewMask, Index, &NTEContext, &NTEInstance)) != 0)
{
printf("AddIPAddress failed with error %d, %d\n", NTEContext, Err);
return;
}
}

if (OptDel)
{
if ((Err = DeleteIPAddress(Context)) != 0)
printf("DeleteIPAddress failed %d\n", Err);
}
}
azure110 2010-04-30
  • 打赏
  • 举报
回复
up。。
zhangweiit 2010-04-30
  • 打赏
  • 举报
回复
试一下下面这段程序

#include <windows.h>
#include <stdio.h>
#include <iphlpapi.h>

void Usage(void) {
printf("Usage: Ipchange [ -l ] [ -a -n<index id> -i<ip address> -m<subnet mask> ] "
"[ -d -c<context id>]\n\n"
"\t -l List adapter index IDs and IP Address context ID information\n"
"\t -a Add IP Address option\n"
"\t -d Delete IP Address option\n"
"\t -i IP Address to specify with -a option\n"
"\t -m Subnet Mask to specify with -a option\n"
"\t -c IP context ID for an existing IP address\n"
"\t -n Index ID of an existing network adapter\n");
}

void main(int argc, char *argv[]) {
ULONG NTEContext = 0;
ULONG NTEInstance;
IPAddr NewIP;
IPAddr NewMask;
DWORD Index;
DWORD Context;
CHAR NewIPStr[64];
CHAR NewMaskStr[64];

PIP_ADAPTER_INFO pAdapterInfo, pAdapt;
PIP_ADDR_STRING pAddrStr;
DWORD AdapterInfoSize;
DWORD Err;
BOOL OptList = FALSE;
BOOL OptAdd = FALSE;
BOOL OptDel = FALSE;

NewIPStr[0] = '\0';
NewMaskStr[0] = '\0';
Context = Index = -1;
for (int i = 1; i < argc; i++)
{
if ((argv[i][0] == '-') || (argv[i][0] == '/'))
{
switch(tolower(argv[i][1]))
{
case 'l':
OptList = TRUE;
break;
case 'a':
OptAdd = TRUE;
break;
case 'c':
if (strlen(argv[i]) > 2)
Context = atoi(&argv[i][2]);
break;
case 'd':
OptDel = TRUE;
break;
case 'i':
if (strlen(argv[i]) > 2)
strcpy(NewIPStr, &argv[i][2]);
break;
case 'm':
if (strlen(argv[i]) > 2)
strcpy(NewMaskStr, &argv[i][2]);
break;
case 'n':
if (strlen(argv[i]) > 2)
Index = atoi(&argv[i][2]);
break;
default:
printf("default\n");
Usage();
return;
}
}
else
{
printf("else\n");
Usage();
return;
}
}


// Check options
if ((OptAdd && (Index == -1 || NewIPStr[0] == '\0' || NewMaskStr[0] == '\0'))
|| (OptDel && Context == -1))
{
Usage();
return;
}

// Get sizing information about all adapters
AdapterInfoSize = 0;
if ((Err = GetAdaptersInfo(NULL, &AdapterInfoSize)) != 0)
{
if (Err != ERROR_BUFFER_OVERFLOW)
{
printf("GetAdaptersInfo sizing failed with error %d\n", Err);
return;
}
}

// Allocate memory from sizing information
if ((pAdapterInfo = (PIP_ADAPTER_INFO) GlobalAlloc(GPTR, AdapterInfoSize)) == NULL)
{
printf("Memory allocation error\n");
return;
}

// Get actual adapter information
if ((Err = GetAdaptersInfo(pAdapterInfo, &AdapterInfoSize)) != 0)
{
printf("GetAdaptersInfo failed with error %d\n", Err);
return;
}

if (OptList)
{
printf("MAC Address - Adapter\n"
"Index Context Ip Address Subnet Mask\n"
"--------------------------------------------------------------\n");

pAdapt = pAdapterInfo;

while (pAdapt)
{
for (UINT i=0; i<pAdapt->AddressLength; i++)
{
if (i == (pAdapt->AddressLength - 1))
printf("%.2X - ",(int)pAdapt->Address[i]);
else
printf("%.2X-",(int)pAdapt->Address[i]);
}
printf("%s\n", pAdapt->Description);

pAddrStr = &(pAdapt->IpAddressList);
while(pAddrStr)
{
printf("%-10.d%-10.d%-20.20s%s\n", pAdapt->Index, pAddrStr->Context, pAddrStr->IpAddress.String, pAddrStr->IpMask.String);
pAddrStr = pAddrStr->Next;
}

pAdapt = pAdapt->Next;
}
}

if (OptAdd)
{
NewIP = inet_addr(NewIPStr);
NewMask = inet_addr(NewMaskStr);
if ((Err = AddIPAddress(NewIP, NewMask, Index, &NTEContext, &NTEInstance)) != 0)
{
printf("AddIPAddress failed with error %d, %d\n", NTEContext, Err);
return;
}
}

if (OptDel)
{
if ((Err = DeleteIPAddress(Context)) != 0)
printf("DeleteIPAddress failed %d\n", Err);
}
}
cattycat 2010-04-30
  • 打赏
  • 举报
回复
1楼的代码需要iphlpapi.h头文件和库文件,这个在platform sdk中,也可以专门下载这几个库文件。
cxxer 2010-04-30
  • 打赏
  • 举报
回复
system(const char *)有局限性,如“本地连接”改名后就不通用了。

包含windows头文件winsock2.h后,可以读出本地IP地名,如何更改后再次写回呢?
赵4老师 2010-04-30
  • 打赏
  • 举报
回复
system("netsh interface ip set dns 本地连接 static 202.106.196.115");
赵4老师 2010-04-30
  • 打赏
  • 举报
回复
system("netsh interface ip set address 本地连接 static 192.168.1.123 255.255.255.0 192.168.1.1 1");

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧