The service query WinSock

All services on the TCP protocol checks the local

#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;
}

 

Published 27 original articles · won praise 11 · views 3462

Guess you like

Origin blog.csdn.net/Megurine_Luka_/article/details/104083993