其他分享
首页 > 其他分享> > WinSock之服务查询

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

 

巡音luka 发布了27 篇原创文章 · 获赞 11 · 访问量 3406 私信 关注

标签:wVersionRequested,服务,lib,int,查询,wsaData,include,WinSock
来源: https://blog.csdn.net/Megurine_Luka_/article/details/104083993