vs2010 网络编程练习时间:2021-10-08 04:57:26// hostconfig.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "hostconfig.h" #include "winsock2.h" #include <string> #include <Nb30.h> #pragma comment(lib, "ws2_32.lib") #pragma comment(lib,"Netapi32.lib") #ifdef _DEBUG #define new DEBUG_NEW #endif // The one and only application object CWinApp theApp; using namespace std; void init(); void getHostName(char *); void getIP(char*); int getMac(); void setIP(string s); char mac[15]; int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; HMODULE hModule = ::GetModuleHandle(NULL); if (hModule != NULL) { // initialize MFC and print and error on failure if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs _tprintf(_T("Fatal Error: MFC initialization failed/n")); nRetCode = 1; } else { // TODO: code your application's behavior here. init(); char hostname[128]; getHostName(hostname); getIP(hostname); getMac(); string newIP; cout<<"请输入新的本地ip"<<endl; cin>>newIP; setIP(newIP); } } else { // TODO: change error code to suit your needs _tprintf(_T("Fatal Error: GetModuleHandle failed/n")); nRetCode = 1; } system("pause"); return nRetCode; } void init() { WSADATA wsa; if (WSAStartup(MAKEWORD(2,2), &wsa) != 0) { cout<<"WSAStartup failed!/n"; } } void getHostName(char* a) { if (gethostname(a,128)==0) { cout<<a<<endl; } else { cout<<"can not get hostname"<<endl; } } void getIP(char* a) { struct hostent *pHost; pHost=gethostbyname(a); for( int i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ ) { const char* pIP=inet_ntoa (*(struct in_addr* )pHost->h_addr_list[i]); cout<<pIP<<endl; } } int getMac() { NCB ncb; UCHAR uRetCode; int num = 0; LANA_ENUM lana_enum; memset(&ncb, 0, sizeof(ncb) ); ncb.ncb_command = NCBENUM; // 对网卡发送请求的命令,获取网卡数 ncb.ncb_buffer = (unsigned char*)&lana_enum; ncb.ncb_length = sizeof(lana_enum); struct ASTAT //copy头文件中定义 { ADAPTER_STATUS adapt; NAME_BUFFER NameBuf[30]; }; //向网卡发送NCBENUM命令,以获取当前机器的网卡信息,如有多少个网卡 //每张网卡的编号等 uRetCode = Netbios(&ncb); if (uRetCode == 0) { num = lana_enum.length; //对每一张网卡,以其网卡编号为输入编号,获取其MAC地址 for (int i = 0; i < num; i++) { ASTAT Adapter; memset(&ncb,0,sizeof(ncb)); //使用之前要使ncb结构清0 ncb.ncb_command=NCBRESET; //对选定的网卡发送命令,以便进行初始化 ncb.ncb_lana_num=lana_enum.lana[i]; uRetCode=Netbios(&ncb); // memset(&ncb,0,sizeof(ncb)); ncb.ncb_command=NCBASTAT; strcpy((char*)ncb.ncb_callname,"*"); ncb.ncb_buffer=(UCHAR*)&Adapter; ncb.ncb_length=sizeof(Adapter); //接着发送命令获取网卡信息 uRetCode=Netbios(&ncb); if(uRetCode==0) { char *lpmac; sprintf(mac,"%02X-%02X-%02X-%02X-%02X-%02X", Adapter.adapt.adapter_address[0], Adapter.adapt.adapter_address[1], Adapter.adapt.adapter_address[2], Adapter.adapt.adapter_address[3], Adapter.adapt.adapter_address[4], Adapter.adapt.adapter_address[5]); lpmac=mac; cout<<lpmac<<endl; } else { cout<<"exceptions:"<<(int)uRetCode<<endl; } } } return num; } void setIP(string s) { string cmd="netsh interface ip set address name=/"无线网络连接/" source=static addr="+s+" mask=255.255.255.0 gateway=192.168.1.1 "; WinExec(cmd.c_str() , SW_SHOW); //WinExec("netsh interface ip set address name=/"无线网络连接/" source=static addr=192.168.1.5 mask=255.255.255.0 gateway=192.168.1.1 ", SW_SHOW); } 参照vc++网络高级编程。在拿网卡时调试了好久。。。后来在呀爷的帮助下,才发现。。。原来写错了个字母。。。正确为NCBASTAT; 还有,网卡不活动时是测试不到的。。。