WinSock之服务查询

查询本机上TCP协议的所有服务

#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include "WinSock2.h"
#include "iostream"
#pragma comment(lib,"ws2_32.lib")  //链接WinSock导入库
using namespace std;
int main(int argc, char **argv) {
	WSADATA wsaData;
	WORD wVersionRequested = MAKEWORD(2, 2);   //调用2.2版本
	if (WSAStartup(wVersionRequested, &wsaData) != 0) {   //加载WinSock动态链接库
		cout << "加载WinSock DLL失败!\n";
		return 0;
	}
	struct servent *pServer;
	for (int i = 1; i < 65535; i++) {
		pServer = getservbyport(htons(i), "TCP");//获取服务信息
		if (pServer != NULL) {
			cout << "服务名:" << pServer->s_name << endl;
			cout << "协议:" << pServer->s_proto << endl;
			cout << "端口号:" << ntohs(pServer->s_port) << endl;
		}
	}
	
	WSACleanup();
	return 0;
}

 

你可能感兴趣的:(WinSock)