성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Roll A Lisp In C - Reading ; https...
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>C# - dynamicport 값의 범위를 알아내는 방법</h1> <p> 이상하군요, netsh로 수행하는,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C:\temp> <a target='tab' href='https://www.sysnet.pe.kr/2/0/1807'>netsh int ipv4 show dynamicport tcp</a> Protocol tcp Dynamic Port Range --------------------------------- Start Port : 1024 Number of Ports : 64511 </pre> <br /> 결과를 API로 가져올 수 있는 방법을 모르겠습니다. (혹시 Win32 API로 아시는 분은 덧글 부탁드립니다.) 그렇다고 저 출력 결과를 파싱하는 것은 왠지 자존심이 허락지 않습니다. ^^<br /> <br /> 다행히 검색해 보면, PowerShell을 통해 가져오는 방법이 있는데,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Get-NetTCPSetting ; <a target='tab' href='https://learn.microsoft.com/en-us/powershell/module/nettcpip/get-nettcpsetting'>https://learn.microsoft.com/en-us/powershell/module/nettcpip/get-nettcpsetting</a> </pre> <br /> 테스트 PC에서 실행해 보니 다음과 같은 결과를 확인할 수 있고,<br /> <br /> <a name='output_nettcp'></a><br /> <pre style='height: 400px; margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > PS C:\WINDOWS\system32> <span style='color: blue; font-weight: bold'>Get-NetTCPSetting</span> SettingName : Automatic ...[생략]... SettingName : InternetCustom MinRto(ms) : 300 InitialCongestionWindow(MSS) : 10 CongestionProvider : CUBIC CwndRestart : False DelayedAckTimeout(ms) : 40 DelayedAckFrequency : 2 MemoryPressureProtection : Enabled AutoTuningLevelLocal : Normal AutoTuningLevelGroupPolicy : NotConfigured AutoTuningLevelEffective : Local EcnCapability : Disabled Timestamps : Disabled InitialRto(ms) : 1000 ScalingHeuristics : Disabled <span style='color: blue; font-weight: bold'>DynamicPortRangeStartPort : 1024 DynamicPortRangeNumberOfPorts : 64511</span> AutomaticUseCustom : Disabled NonSackRttResiliency : Disabled ForceWS : Enabled MaxSynRetransmissions : 4 AutoReusePortRangeStartPort : 0 AutoReusePortRangeNumberOfPorts : 0 SettingName : DatacenterCustom MinRto(ms) : 20 InitialCongestionWindow(MSS) : 10 CongestionProvider : CUBIC CwndRestart : False DelayedAckTimeout(ms) : 10 DelayedAckFrequency : 2 MemoryPressureProtection : Enabled AutoTuningLevelLocal : Normal AutoTuningLevelGroupPolicy : NotConfigured AutoTuningLevelEffective : Local EcnCapability : Disabled Timestamps : Disabled InitialRto(ms) : 1000 ScalingHeuristics : Disabled <span style='color: blue; font-weight: bold'>DynamicPortRangeStartPort : 1024 DynamicPortRangeNumberOfPorts : 64511</span> AutomaticUseCustom : Disabled NonSackRttResiliency : Disabled ForceWS : Enabled MaxSynRetransmissions : 4 AutoReusePortRangeStartPort : 0 AutoReusePortRangeNumberOfPorts : 0 SettingName : Compat MinRto(ms) : 300 InitialCongestionWindow(MSS) : 4 CongestionProvider : NewReno CwndRestart : False DelayedAckTimeout(ms) : 200 DelayedAckFrequency : 2 MemoryPressureProtection : Enabled AutoTuningLevelLocal : Normal AutoTuningLevelGroupPolicy : NotConfigured AutoTuningLevelEffective : Local EcnCapability : Disabled Timestamps : Disabled InitialRto(ms) : 1000 ScalingHeuristics : Disabled <span style='color: blue; font-weight: bold'>DynamicPortRangeStartPort : 1024 DynamicPortRangeNumberOfPorts : 64511</span> AutomaticUseCustom : Disabled NonSackRttResiliency : Disabled ForceWS : Enabled MaxSynRetransmissions : 4 AutoReusePortRangeStartPort : 0 AutoReusePortRangeNumberOfPorts : 0 SettingName : Datacenter MinRto(ms) : 20 InitialCongestionWindow(MSS) : 10 CongestionProvider : CUBIC CwndRestart : False DelayedAckTimeout(ms) : 10 DelayedAckFrequency : 2 MemoryPressureProtection : Enabled AutoTuningLevelLocal : Normal AutoTuningLevelGroupPolicy : NotConfigured AutoTuningLevelEffective : Local EcnCapability : Disabled Timestamps : Disabled InitialRto(ms) : 1000 ScalingHeuristics : Disabled <span style='color: blue; font-weight: bold'>DynamicPortRangeStartPort : 1024 DynamicPortRangeNumberOfPorts : 64511</span> AutomaticUseCustom : Disabled NonSackRttResiliency : Disabled ForceWS : Enabled MaxSynRetransmissions : 4 AutoReusePortRangeStartPort : 0 AutoReusePortRangeNumberOfPorts : 0 SettingName : Internet MinRto(ms) : 300 InitialCongestionWindow(MSS) : 10 CongestionProvider : CUBIC CwndRestart : False DelayedAckTimeout(ms) : 40 DelayedAckFrequency : 2 MemoryPressureProtection : Enabled AutoTuningLevelLocal : Normal AutoTuningLevelGroupPolicy : NotConfigured AutoTuningLevelEffective : Local EcnCapability : Disabled Timestamps : Disabled InitialRto(ms) : 1000 ScalingHeuristics : Disabled <span style='color: blue; font-weight: bold'>DynamicPortRangeStartPort : 1024 DynamicPortRangeNumberOfPorts : 64511</span> AutomaticUseCustom : Disabled NonSackRttResiliency : Disabled ForceWS : Enabled MaxSynRetransmissions : 4 AutoReusePortRangeStartPort : 0 AutoReusePortRangeNumberOfPorts : 0 </pre> <br /> 따라서 "netsh ..." 명령어의 출력 결과를 파싱하는 것보다 C#에서 Powershell 스크립트를 호출할 수 있으므로,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > How to call Powershell script using web API (cs or asp.net) ; <a target='tab' href='https://stackoverflow.com/questions/54523708/how-to-call-powershell-script-using-web-api-cs-or-asp-net'>https://stackoverflow.com/questions/54523708/how-to-call-powershell-script-using-web-api-cs-or-asp-net</a> Executing PowerShell scripts from C# ; <a target='tab' href='https://learn.microsoft.com/en-us/archive/blogs/kebab/executing-powershell-scripts-from-c'>https://learn.microsoft.com/en-us/archive/blogs/kebab/executing-powershell-scripts-from-c</a> </pre> <br /> 다음과 같이 처리해 줄 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Management.Automation; using System.Text; namespace ConsoleApp1 { // 참조 추가: C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll // 또는, Install-Package System.Management.Automation class Program { static void Main(string[] args) { // PowerShell using (<span style='color: blue; font-weight: bold'>PowerShell instance = PowerShell.Create()</span>) { instance.<span style='color: blue; font-weight: bold'>AddCommand("Get-NetTCPSetting");</span> Collection<PSObject> psOutput = instance.<span style='color: blue; font-weight: bold'>Invoke</span>(); foreach (PSObject obj in psOutput) { Console.WriteLine(obj); } } } } } /* 출력 결과 MSFT_NetTCPSetting (CreationClassName = "", PolicyActionName = "", PolicyRuleCreationClassName = "", PolicyRuleName = "Automatic", SystemCreationClassName = "", SystemName = "") MSFT_NetTCPSetting (CreationClassName = "", PolicyActionName = "", PolicyRuleCreationClassName = "", PolicyRuleName = "InternetCustom", SystemCreationClassName = "", SystemName = "") MSFT_NetTCPSetting (CreationClassName = "", PolicyActionName = "", PolicyRuleCreationClassName = "", PolicyRuleName = "DatacenterCustom", SystemCreationClassName = "", SystemName = "") MSFT_NetTCPSetting (CreationClassName = "", PolicyActionName = "", PolicyRuleCreationClassName = "", PolicyRuleName = "Compat", SystemCreationClassName = "", SystemName = "") MSFT_NetTCPSetting (CreationClassName = "", PolicyActionName = "", PolicyRuleCreationClassName = "", PolicyRuleName = "Datacenter", SystemCreationClassName = "", SystemName = "") MSFT_NetTCPSetting (CreationClassName = "", PolicyActionName = "", PolicyRuleCreationClassName = "", PolicyRuleName = "Internet", SystemCreationClassName = "", SystemName = "") */ </pre> <br /> 그런데, 출력 결과를 보면 왠지 낯이 익는 네이밍 룰이 나옵니다. "MSFT_NetTCPSetting" 이름에서 WMI의 느낌이 나는데요, 실제로 검색해 보면 이에 대한 항목을 찾을 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > MSFT_NetTCPSetting class ; <a target='tab' href='https://learn.microsoft.com/en-us/previous-versions/windows/desktop/nettcpipprov/msft-nettcpsetting'>https://learn.microsoft.com/en-us/previous-versions/windows/desktop/nettcpipprov/msft-nettcpsetting</a> </pre> <br /> 간단하게 <a target='tab' href='https://www.sysnet.pe.kr/2/0/11957'>wmic</a>를 이용해 빠르게 동작 여부를 확인해 보면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > C:\Windows\System32> <span style='color: blue; font-weight: bold'>wmic /NAMESPACE:\\root\StandardCimv2 path MSFT_NetTCPSetting where "SettingName='Internet'" get DynamicPortRangeStartPort</span> DynamicPortRangeStartPort 1024 </pre> <br /> 게임 끝이군요, ^^ 이제 최종적으로 "System.Management.dll"만 참조 추가해 다음과 같이 코딩할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System; using System.Management; namespace ConsoleApp1 { class Program { static void Main(string[] args) { ManagementScope scope = new ManagementScope(<span style='color: blue; font-weight: bold'>"\\\\.\\ROOT\\StandardCimv2"</span>); ObjectQuery query = new ObjectQuery(<span style='color: blue; font-weight: bold'>"SELECT * FROM MSFT_NetTCPSetting Where SettingName=\"Internet\""</span>); ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query); ManagementObjectCollection queryCollection = searcher.Get(); foreach (ManagementObject m in queryCollection) { Console.WriteLine("DynamicPortRangeStartPort : {0}", <span style='color: blue; font-weight: bold'>m["DynamicPortRangeStartPort"]</span>); Console.WriteLine("DynamicPortRangeNumberOfPorts : {0}", <span style='color: blue; font-weight: bold'>m["DynamicPortRangeNumberOfPorts"]</span>); } } } } /* 출력 결과 DynamicPortRangeStartPort : 1024 DynamicPortRangeNumberOfPorts : 64511 */ </pre> <br /> 참고로, MSFT_NetTCPSetting에서 다양하게 나오는 PolicyRuleName(또는 SettingName)에 대해 궁금하다면 다음의 글을 추천합니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > TCP Templates for Windows Server 2019 - How to tune your Windows Server Transports (Advanced users only) ; <a target='tab' href='https://argonsys.com/microsoft-cloud/library/tcp-templates-for-windows-server-2019-how-to-tune-your-windows-server-transports-advanced-users-only-%F0%9F%98%89/'>https://argonsys.com/microsoft-cloud/library/tcp-templates-for-windows-server-2019-how-to-tune-your-windows-server-transports-advanced-users-only-%F0%9F%98%89/</a> </pre> <br /> 저런 게 있는 줄 저도 처음 알았습니다. ^^<br /> <br /> <hr style='width: 50%' /><br /> <br /> 그건 그렇고, 점점 더 이야기가 재미있어지지 않나요? ^^ Get-NetTCPSetting의 결과를 자세하게 살펴보면 그동안 아래의 시리즈 글에서 고민했던,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > 윈도우 환경에서 클라이언트 소켓의 최대 접속 수 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12350'>https://www.sysnet.pe.kr/2/0/12350</a> 윈도우 환경에서 클라이언트 소켓의 최대 접속 수 (2) - SO_REUSEADDR ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12432'>https://www.sysnet.pe.kr/2/0/12432</a> 윈도우 환경에서 클라이언트 소켓의 최대 접속 수 (3) - SO_PORT_SCALABILITY ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12433'>https://www.sysnet.pe.kr/2/0/12433</a> </pre> <br /> 그 문제를 한 방에 날려버릴 단서가 있습니다. 그 이야기는 <a target='tab' href='https://www.sysnet.pe.kr/2/0/12435'>다음번 포스트</a>에서. ^^<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1169
(왼쪽의 숫자를 입력해야 합니다.)