windows下netstat代码

本文详细介绍了在Windows环境下如何使用netstat命令来查看网络连接状态,包括TCP和UDP连接,以及如何通过系统调用进行网络状态监控。同时,探讨了netstat输出结果的解析,涉及到数据结构与表格格式的理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

/   * author:skape
 
*  compile with:
 
*
 
*  cl  / ML  / W3  / GX  / O1 netstat.c  / link user32.lib iphlpapi.lib ws2_32.lib
 
*
 
*/
#include 
< stdlib.h >
#include 
< stdio.h >

#include 
< windows.h >
#include 
< iphlpapi.h >

/*
 * List of subnets and their respective netmasks that we wish to hide.
 * This array should always be terminated by a NULL subnet.
 
*/

struct   {
 
const char *subnet;
 
const char *netmask; 
}
 hideSubnets[]  =   {
 
"1.2.3.0",  "255.255.255.0" },
 
{ NULL,    NULL     },
}
;

/*
 * Array of listening ports we wish to hide. 
 
*/

unsigned 
short  hideListenPorts[]  =   {
 
12345
}
;

/*
 * Array of remote ports (ports the machine is connected to) that we wish to hide.
 
*/

unsigned 
short  hideRemotePorts[]  =   {
 
12345
}
;

struct   {

 unsigned 
char dontResolve;

 
struct {
  unsigned 
char allConnectAndListening;
  unsigned 
char ethernetStatistics;
  
char          specificProtocol[16];
  unsigned 
char routingTable;
  unsigned 
char perProtocolStatistics;
 }
 display;

 
int interval;

}
 netstat;

void  displayEthernetStatistics();
void  displayPerProtocolStatistics();
void  displayRoutingTable();
void  displayActiveConnections();
void  displayHelp();

void  resolveAddress(unsigned  long  addr,  char   * buf, unsigned  long  bufSize, unsigned  char  isSrcAddress);
void  resolvePort(unsigned  short  port,  char   * buf, unsigned  long  bufSize,  const   char   * proto);

int  main( int  argc,  char   ** argv)
{
 
int x = 1, y, incrementX;
 WORD version;
 WSADATA data;

 version 
= MAKEWORD(22);

 
if (WSAStartup(version, &data) != 0)
  
return 0;

 memset(
&netstat, 0sizeof(netstat));

 strcpy(netstat.display.specificProtocol, 
"DEF-TCP");

 
if (argc > 1)
 
{
  unsigned 
char unknownArgument = 0;

  
for (x = 1; x < argc; x++)
  
{
   
if (argv[x][0== '-')
   
{
    incrementX 
= 0;

    
for (y = 1; y < (int)strlen(argv[x]); y++)
    
{
     
switch (argv[x][y])
     
{
      
case 'a':
       netstat.display.allConnectAndListening 
= 1;
       
break;
      
case 'e':
       netstat.display.ethernetStatistics 
= 1;
       
break;
      
case 'n':
       netstat.dontResolve 
= 1;
       
break;
      
case 'p':
       incrementX 
= 1;

       
if (x+1 == argc)
        memset(netstat.display.specificProtocol, 
0sizeof(netstat.display.specificProtocol));
       
else
       
{
        
if ((stricmp(argv[x+1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值