程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> C++檢測本地網絡端口占用

C++檢測本地網絡端口占用

編輯:C++入門知識

一、解決方法:
1、使用DOS netstat 命令查詢所有端口使用情況
2、使用DOS findstr 命令輔助篩選符合要求的進程PID
3、使用DOS tasklist 命令查詢PID對應的進程信息
4、使用DOS findstr 命令輔助篩選符合要求的進程名
5、在VC中執行DOS命令
WinExec 異步執行。不能等待命令結束,較簡單
ShellExecute 麻煩
CreateProcess 麻煩
注:使用任何一種方法,都需要將結果輸出到外部,然後再讀取結果分析

 

二、DOS查詢端口使用示例
比如要查看8080端口被哪個程序占用了,windows命令行窗口下執行:運行--cmd


C:\>netstat -aon|findstr ":8080 " ,輸出
TCP 127.0.0.1:80 0.0.0.0:0 LISTENING 2448


端口被進程號為2448的進程占用,繼續執行下面命令:


C:\>tasklist /fi "pid eq 2448" /nh
thread.exe 2016 Console 0 16,064 K


表示thread.exe程序占用了端口8080

 

三、windows下VC實現代碼

[cpp]
#include <windows.h>  
#include <string>  
using namespace std; 
 
//  
//根據端口查詢進程名,如果有多個進程,只返回第一個  
//  
bool GetProcNameByPort(int nPort, string &strResult) 

    bool bSuc = false; 
    char pszPort[16] = {0}; 
    itoa(nPort, pszPort, 10); 
    char pResult[80] = {0}; 
    const char* pPortFilePath = "c:\\~vtmp"; 
    const char* pProcessFilePath = "c:\\~vvtmp"; 
    sprintf(pResult, "cmd /c netstat -ano|findstr \":%d \" > %s",  nPort, pPortFilePath); 
 
    //WinExec 執行cmd命令  
    WinExec(pResult, SW_HIDE); 
    Sleep(450); 
 
    //查找端口號  
    FILE *pPortFile = fopen(pPortFilePath, "r"); 
    if ( pPortFile ) 
    { 
        while ( !feof(pPortFile) ) 
        { 
            memset(pResult, 0, sizeof(pResult)); 
            fread(pResult, sizeof(pResult), 1, pPortFile); 
            pResult[sizeof(pResult)-1] = 0x00; 
 
            string strPortTmp = pResult; 
            int offset = (int)strPortTmp.find_last_of(0x0A); 
            if ( offset > -1 ) 
            { 
                pResult[offset] = 0x00; 
                strPortTmp = strPortTmp.substr(0, offset); 
                if ( !feof(pPortFile) ) 
                { 
                    fseek(pPortFile, (long)(strPortTmp.length()+1-sizeof(pResult)), SEEK_CUR); 
                } 
 
                offset = (int)strPortTmp.find_first_of(':'); 
                if ( offset > -1 ) 
                { 
                    strPortTmp = strPortTmp.substr(offset+1, 6); 
                    offset = (int)strPortTmp.find_last_not_of(' '); 
                    if ( offset > -1 ) 
                    { 
                        strPortTmp = strPortTmp.substr(0, offset+1); 
                        if ( strPortTmp == pszPort ) 
                        { 
                            strPortTmp = pResult; 
                            offset = (int)strPortTmp.find_last_of(' '); 
                            if ( offset > -1 ) 
                            { 
                                strPortTmp = strPortTmp.substr(offset+1); 
                                sprintf(pResult, "cmd /c tasklist /fi \"pid eq %s\" /nh> %s", strPortTmp.c_str(), pProcessFilePath); 
                                //根據端口號查找進程ID  
                                WinExec(pResult, SW_HIDE); 
                                Sleep(450); 
 
                                FILE *pProcessFile = fopen(pProcessFilePath, "r"); 
                                if ( pProcessFile ) 
                                { 
                                    while (!feof(pProcessFile)) 
                                    { 
                                        memset(pResult, 0, sizeof(pResult)); 
                                        fread(pResult, sizeof(pResult), 1, pProcessFile); 
                                        pResult[sizeof(pResult)-1] = 0x00; 
 
                                        string strProcessTmp = pResult; 
                                        int offset = (int)strProcessTmp.find_last_of(0x0A); 
                                        if ( offset > -1 ) 
                                        { 
                                            pResult[offset] = 0x00; 
                                            strProcessTmp = strProcessTmp.substr(0, offset); 
                                            if ( !feof(pProcessFile) ) 
                                            { 
                                                fseek(pProcessFile, (long)(strProcessTmp.length()+1-sizeof(pResult)), SEEK_CUR); 
                                            } 
 
                                            if ( 0x0A == pResult[0] )       //首行只有一個字符 0x0A  
                                            { 
                                                strProcessTmp = pResult+1; 
                                            } 
                                            else 
                                            { 
                                                strProcessTmp = pResult; 
                                            } 
                                            offset = (int)strProcessTmp.find_first_of(' '); 
                                            if ( offset > -1 ) 
                                            { 
                                                { 
                                                    { 
                                                        { 
                                                            { 
                                                                strProcessTmp = strProcessTmp.substr(0, offset); 
                                                                if ( "" != strProcessTmp ) 
                                                                { 
                                                                    //查找成功,結束  
                                                                    strResult += "[" + strProcessTmp + "]"; 
                                                                    bSuc = true; 
                                                                } 
                                                                continue; 
                                                            } 
                                                        } 
                                                    } 
                                                } 
                                            } 
                                        } 
                                    } 
                                    fclose(pProcessFile); 
                                } 
                                sprintf(pResult, "cmd /c del %s", pProcessFilePath); 
                                WinExec(pResult, SW_HIDE); 
                                if(bSuc){ continue; } 
                            } 
                        } 
                    } 
                } 
            } 
        } 
        fclose(pPortFile); 
    } 
    if(!bSuc){ strResult=""; }; 
    sprintf(pResult, "cmd /c del %s", pPortFilePath); 
    WinExec(pResult, SW_HIDE); 
    return bSuc; 

 
int main() 

    int count = 100; 
    string str = ""; 
 
    while(count--) 
    { 
        str = ""; 
        GetProcNameByPort(843, str); 
        if ( str != "" ) 
            printf("_%s_\n", str.c_str()); 
        Sleep(1000); 
    } 
 
    printf("____End____"); 
 
 
    getchar(); 
    return 0; 

#include <windows.h>
#include <string>
using namespace std;

//
//根據端口查詢進程名,如果有多個進程,只返回第一個
//
bool GetProcNameByPort(int nPort, string &strResult)
{
 bool bSuc = false;
 char pszPort[16] = {0};
 itoa(nPort, pszPort, 10);
 char pResult[80] = {0};
 const char* pPortFilePath = "c:\\~vtmp";
 const char* pProcessFilePath = "c:\\~vvtmp";
 sprintf(pResult, "cmd /c netstat -ano|findstr \":%d \" > %s",  nPort, pPortFilePath);

 //WinExec 執行cmd命令
 WinExec(pResult, SW_HIDE);
 Sleep(450);

 //查找端口號
 FILE *pPortFile = fopen(pPortFilePath, "r");
 if ( pPortFile )
 {
  while ( !feof(pPortFile) )
  {
   memset(pResult, 0, sizeof(pResult));
   fread(pResult, sizeof(pResult), 1, pPortFile);
   pResult[sizeof(pResult)-1] = 0x00;

   string strPortTmp = pResult;
   int offset = (int)strPortTmp.find_last_of(0x0A);
   if ( offset > -1 )
   {
    pResult[offset] = 0x00;
    strPortTmp = strPortTmp.substr(0, offset);
    if ( !feof(pPortFile) )
    {
     fseek(pPortFile, (long)(strPortTmp.length()+1-sizeof(pResult)), SEEK_CUR);
    }

    offset = (int)strPortTmp.find_first_of(':');
    if ( offset > -1 )
    {
     strPortTmp = strPortTmp.substr(offset+1, 6);
     offset = (int)strPortTmp.find_last_not_of(' ');
     if ( offset > -1 )
     {
      strPortTmp = strPortTmp.substr(0, offset+1);
      if ( strPortTmp == pszPort )
      {
       strPortTmp = pResult;
       offset = (int)strPortTmp.find_last_of(' ');
       if ( offset > -1 )
       {
        strPortTmp = strPortTmp.substr(offset+1);
        sprintf(pResult, "cmd /c tasklist /fi \"pid eq %s\" /nh> %s", strPortTmp.c_str(), pProcessFilePath);
        //根據端口號查找進程ID
        WinExec(pResult, SW_HIDE);
        Sleep(450);

        FILE *pProcessFile = fopen(pProcessFilePath, "r");
        if ( pProcessFile )
        {
         while (!feof(pProcessFile))
         {
          memset(pResult, 0, sizeof(pResult));
          fread(pResult, sizeof(pResult), 1, pProcessFile);
          pResult[sizeof(pResult)-1] = 0x00;

          string strProcessTmp = pResult;
          int offset = (int)strProcessTmp.find_last_of(0x0A);
          if ( offset > -1 )
          {
           pResult[offset] = 0x00;
           strProcessTmp = strProcessTmp.substr(0, offset);
           if ( !feof(pProcessFile) )
           {
            fseek(pProcessFile, (long)(strProcessTmp.length()+1-sizeof(pResult)), SEEK_CUR);
           }

           if ( 0x0A == pResult[0] )  //首行只有一個字符 0x0A
           {
            strProcessTmp = pResult+1;
           }
           else
           {
            strProcessTmp = pResult;
           }
           offset = (int)strProcessTmp.find_first_of(' ');
           if ( offset > -1 )
           {
            {
             {
              {
               {
                strProcessTmp = strProcessTmp.substr(0, offset);
                if ( "" != strProcessTmp )
                {
                 //查找成功,結束
                 strResult += "[" + strProcessTmp + "]";
                 bSuc = true;
                }
                continue;
               }
              }
             }
            }
           }
          }
         }
         fclose(pProcessFile);
        }
        sprintf(pResult, "cmd /c del %s", pProcessFilePath);
        WinExec(pResult, SW_HIDE);
        if(bSuc){ continue; }
       }
      }
     }
    }
   }
  }
  fclose(pPortFile);
 }
 if(!bSuc){ strResult=""; };
 sprintf(pResult, "cmd /c del %s", pPortFilePath);
 WinExec(pResult, SW_HIDE);
 return bSuc;
}

int main()
{
 int count = 100;
 string str = "";

 while(count--)
 {
  str = "";
  GetProcNameByPort(843, str);
  if ( str != "" )
   printf("_%s_\n", str.c_str());
  Sleep(1000);
 }

 printf("____End____");


 getchar();
 return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved