#include <iostream> #include <WinSock2.h> #include <string> #pragma comment(lib, "Ws2_32.lib") using namespace std; void printMyService(); //实验提示信息 int getHostNameAndIP(); //实验1: 获取用户名和IP int getUdp(); //实验2: 获取所有使用UDP的服务 int getProtoByName(); //实验3-1:显示TCP与UDP的协议号 int getProtocol(); //实验3-2:显示因特网所有协议的名称和协议号 int main() { int choose = 0; char continueTest = 'y'; do { printMyService(); cin >> choose; switch (choose) { case 1: getHostNameAndIP(); break; case 2: getUdp(); break; case 3: getProtoByName(); break; case 4: getProtocol(); break; case 5: return 0; default: cout << "输入错误" << endl; break; } cout << "输入Y/y继续,任意键退出" << endl; string temp; getline(cin,temp); continueTest = cin.get(); } while (continueTest=='y'|| continueTest=='Y'); return 0; } void printMyService() { cout << "windows编程实验2:WinSock初步--于衡" << endl << "输入数字以使用相关功能" << endl << "1、获取当前主机的用户名和IP地址" << endl << "2、获取当前主机所有使用UDP协议的服务的名称和端口号" << endl << "3、通过getprotobyname查询TCP和UDP的协议号"<<endl << "4、显示因特网上所有协议的名称以及协议号" << endl << "5、退出" << endl; cout << "输入你的选择--->>"; } int getHostNameAndIP()//实验 1:获取用户名和IP { WSADATA wsaData; WORD wVersionRequested = MAKEWORD(2, 2); if (WSAStartup(wVersionRequested, &wsaData) != 0) { cout << "加载 WinSock DLL 失败" << endl; return 0; } char host_name[100]; if (gethostname(host_name, sizeof(host_name))) { cout << "获取主机名字失败" << endl; WSACleanup();//注销WinSock DLL库 return 0; } cout << "本机的名字是:" << host_name << endl; //获取主机名字的代码到此结束,准备获取IP地址 struct hostent *hptr; char* *pptr;//用来接收IP地址 hptr = gethostbyname(host_name); if (!hptr) { cout << "获取IP地址失败" << endl; WSACleanup(); return 0; } pptr = hptr->h_addr_list;//从hostent里面获得IP地址 cout << "本机的IP地址为:" << endl; while (*pptr != NULL) { /* pptr为指向h_addr_list的指针,以网络字节顺序排列。 我们把pptr转换为in_addr *类型的指针 然后通过*解除引用得出其32位长整形数值通过inet_ntoa转为点分十进制 */ cout << inet_ntoa(*(struct in_addr *)(*pptr)) << endl; pptr++; } WSACleanup();//注销 return 0; } int getUdp()//实验2:获取所有使用UDP的服务 { WSADATA wsaData; WORD wVersionRequested = MAKEWORD(2, 2); if (WSAStartup(wVersionRequested, &wsaData) != 0) { cout << "加载 WinSock DLL 失败" << endl; return 0; } //实验2 cout << "显示本机上所有的UDP协议" << endl; struct servent *pServer; for (int i = 1; i < 65535; i++) { pServer = getservbyport(htons(i), "UDP"); if (pServer != NULL) { cout << "服务名:" << pServer->s_name << endl << "协议:" << pServer->s_proto << endl << "端口号" << ntohs(pServer->s_port) << endl << endl; } } WSACleanup();//注销 return 0; } int getProtoByName()//实验3-1 显示TCP与UDP的协议号 { WSADATA wsaData; WORD wVersionRequested = MAKEWORD(2, 2); if (WSAStartup(wVersionRequested, &wsaData) != 0) { cout << "加载 WinSock DLL 失败" << endl; return 0; } cout << "通过getprotobyname查询TCP和UDP的协议号:" << endl; struct protoent *pProto; pProto = getprotobyname("TCP"); if(pProto!=NULL) cout << "TCP的协议号:" << pProto->p_proto << endl; pProto = getprotobyname("UDP"); if(pProto!=NULL) cout << "UDP的协议号:" << pProto->p_proto << endl; WSACleanup();//注销 return 0; } int getProtocol()//实验3-2 显示因特网所有协议的名称和协议号 { WSADATA wsaData; WORD wVersionRequested = MAKEWORD(2, 2); if (WSAStartup(wVersionRequested, &wsaData) != 0) { cout << "加载 WinSock DLL 失败" << endl; return 0; } cout << "显示因特网上所有协议的名称以及协议号:" << endl; struct protoent *pProto; for (int i = 1; i <= 256; i++) { pProto = getprotobynumber(i); if (pProto != NULL) { cout << "协议名:" << pProto->p_name << endl << "协议号" << pProto->p_proto << endl << endl; } } WSACleanup();//注销 return 0; }
打破0赞0回复