Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일

(시리즈 글이 9개 있습니다.)
개발 환경 구성: 92. 윈도우 서버 환경에서, 최대 생성 가능한 소켓(socket) 연결 수는 얼마일까?
; https://www.sysnet.pe.kr/2/0/964

Windows: 175. 윈도우 환경에서 클라이언트 소켓의 최대 접속 수
; https://www.sysnet.pe.kr/2/0/12350

Windows: 178. 윈도우 환경에서 클라이언트 소켓의 최대 접속 수 (2) - SO_REUSEADDR
; https://www.sysnet.pe.kr/2/0/12432

Windows: 179. 윈도우 환경에서 클라이언트 소켓의 최대 접속 수 (3) - SO_PORT_SCALABILITY
; https://www.sysnet.pe.kr/2/0/12433

Windows: 181. 윈도우 환경에서 클라이언트 소켓의 최대 접속 수 (4) - ReuseUnicastPort를 이용한 포트 고갈 문제 해결
; https://www.sysnet.pe.kr/2/0/12435

.NET Framework: 981. C# - HttpWebRequest, WebClient와 ephemeral port 재사용
; https://www.sysnet.pe.kr/2/0/12448

.NET Framework: 982. C# - HttpClient에서의 ephemeral port 재사용
; https://www.sysnet.pe.kr/2/0/12449

.NET Framework: 983. C# - TIME_WAIT과 ephemeral port 재사용
; https://www.sysnet.pe.kr/2/0/12450

Linux: 35. C# - 리눅스 환경에서 클라이언트 소켓의 ephemeral port 재사용
; https://www.sysnet.pe.kr/2/0/12459




C# - 리눅스 환경에서 클라이언트 소켓의 ephemeral port 재사용

윈도우의 경우,

윈도우 환경에서 클라이언트 소켓의 최대 접속 수 (4) - ReuseUnicastPort를 이용한 포트 고갈 문제 해결
; https://www.sysnet.pe.kr/2/0/12435

AutoReusePortRangeStartPort/AutoReusePortRangeNumberOfPorts 설정을 통해 5-tuple 구분을 할 수 있었는데요, 그렇다면 .NET Core/5로 리눅스에서 실행하면 어떻게 바뀌는 걸까요?

일단 검색해 보면,

Ephemeral port exhaustion and how to avoid it
; https://making.pusher.com/ephemeral-port-exhaustion-and-how-to-avoid-it/

리눅스의 경우 기본적으로 5-tuple로 구분을 하고 있습니다. 그래도 혹시 모르니 눈으로 직접 확인을 해보겠습니다. ^^




(이 글의 테스트 환경은 우분투 20.04 + .NET 5입니다.)

이번에도 역시 테스트의 편의를 위해 로컬 포트 수를 줄이려고 했는데요, 제가 리눅스를 잘 몰라 포트 수 줄이는 게 좀 서툴군요. ^^; 우선 다음과 같이 sysctl을 이용해 봤는데,

$ sysctl net.ipv4.ip_local_port_range
net.ipv4.ip_local_port_range = 32768    60999

$ sudo sysctl -w net.ipv4.ip_local_port_range="32768 33768"

재부팅하고 나면 다시 32768 ~ 60999로 바뀝니다. 검색해 보면 아래와 같은 방법도 보이는데,

$ cat /proc/sys/net/ipv4/ip_local_port_range
32768   60999

$ sudo echo "32768 33768" > /proc/sys/net/ipv4/ip_local_port_range
bash: /proc/sys/net/ipv4/ip_local_port_range: Permission denied

안됩니다. ^^; 다시 검색을 해보면, Redhat 문서지만 /etc/sysctl.conf를 편집하는 방법도 나옵니다.

6.7. The ip_local_port_range parameters
; https://tldp.org/LDP/solrhe/Securing-Optimizing-Linux-RH-Edition-v1.3/chap6sec70.html

그래서 "/etc/sysctl.conf" 파일에 다음의 설정을 추가 후,

# Allowed local port range
net.ipv4.ip_local_port_range = 32768 33768

서비스 재시작을 하라고 하는데,

$ /etc/rc.d/init.d/network restart
bash: /etc/rc.d/init.d/network: No such file or directory

Ubuntu라 그런지 다른 듯하군요, 달리 방법을 모르니 그냥 "sudo reboot now"를 했고 다행히 반영이 잘 되었습니다. ^^

$ cat /proc/sys/net/ipv4/ip_local_port_range
32768   33768

(혹시 저 방법 말고 아시는 분은 덧글 부탁드립니다. ^^)




자, 그래서 테스트를 위해 서버와 클라이언트 2개를 실행하면,

$ dotnet ConsoleApp1.dll
# of 15000: 0, 15001: 0
# of 15000: 0, 15001: 0
# of 15000: 0, 15001: 0
# of 15000: 1000, 15001: 0
# of 15000: 1000, 15001: 0
# of 15000: 1000, 15001: 0
# of 15000: 1000, 15001: 1000
# of 15000: 1000, 15001: 1000
# of 15000: 1000, 15001: 1000

$ dotnet ConsoleApp2.dll 127.0.0.1 15000 1001
[::ffff:127.0.0.1]:33590-[::ffff:127.0.0.1]:15000
[::ffff:127.0.0.1]:33592-[::ffff:127.0.0.1]:15000
...[생략]...
[::ffff:127.0.0.1]:33759-[::ffff:127.0.0.1]:15000
[::ffff:127.0.0.1]:33761-[::ffff:127.0.0.1]:15000
[::ffff:127.0.0.1]:33763-[::ffff:127.0.0.1]:15000
[::ffff:127.0.0.1]:33765-[::ffff:127.0.0.1]:15000
[::ffff:127.0.0.1]:33767-[::ffff:127.0.0.1]:15000
[::ffff:127.0.0.1]:32769-[::ffff:127.0.0.1]:15000
System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (99): Cannot assign requested address [::ffff:127.0.0.1]:15000
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
   at System.Net.Sockets.TcpClient.Connect(IPEndPoint remoteEP)
   at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
--- End of stack trace from previous location ---
   at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
   at ConsoleApp2.Program.Main(String[] args) in C:\temp\ConsoleApp2\Program.cs:line 23
1000

$ dotnet ConsoleApp2.dll localhost 15001 1001
[::ffff:127.0.0.1]:32952-[::ffff:127.0.0.1]:15001
[::ffff:127.0.0.1]:32954-[::ffff:127.0.0.1]:15001
...[생략]...
[::ffff:127.0.0.1]:33759-[::ffff:127.0.0.1]:15001
[::ffff:127.0.0.1]:33761-[::ffff:127.0.0.1]:15001
[::ffff:127.0.0.1]:33763-[::ffff:127.0.0.1]:15001
[::ffff:127.0.0.1]:33765-[::ffff:127.0.0.1]:15001
[::ffff:127.0.0.1]:33767-[::ffff:127.0.0.1]:15001
[::ffff:127.0.0.1]:32769-[::ffff:127.0.0.1]:15001
System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (99): Cannot assign requested address [::ffff:127.0.0.1]:15001
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
   at System.Net.Sockets.TcpClient.Connect(IPEndPoint remoteEP)
   at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
--- End of stack trace from previous location ---
   at System.Net.Sockets.TcpClient.Connect(String hostname, Int32 port)
   at ConsoleApp2.Program.Main(String[] args) in C:\temp\ConsoleApp2\Program.cs:line 23
1000

클라이언트 2개가 모두 정확히 (포트 수를 1,000개로 제한했으므로) 1,000까지 잘 생성하고, 1,001번째에서 예외가 발생합니다. 즉, 5-tuple 구분을 클라이언트 소켓에서도 별다른 설정 없이 잘 동작하고 있는 것입니다.

윈도우가 AutoReusePortRangeStartPort/AutoReusePortRangeNumberOfPorts 설정을 해야 했던 반면, 리눅스는 그냥 되니 편리하긴 합니다. ^^

(첨부 파일은 이 글의 예제 코드를 포함합니다.)




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 1/4/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... [61]  62  63  64  65  66  67  68  69  70  71  72  73  74  75  ...
NoWriterDateCnt.TitleFile(s)
12115정성태1/14/202011034디버깅 기술: 157. C# - PEB.ProcessHeap을 이용해 디버깅 중인지 확인하는 방법파일 다운로드1
12114정성태1/13/202012878디버깅 기술: 156. C# - PDB 파일로부터 심벌(Symbol) 및 타입(Type) 정보 열거 [1]파일 다운로드3
12113정성태1/12/202013512오류 유형: 590. Visual C++ 빌드 오류 - fatal error LNK1104: cannot open file 'atls.lib' [1]
12112정성태1/12/202010065오류 유형: 589. PowerShell - 원격 Invoke-Command 실행 시 "WinRM cannot complete the operation" 오류 발생
12111정성태1/12/202013353디버깅 기술: 155. C# - KernelMemoryIO 드라이버를 이용해 실행 프로그램을 숨기는 방법(DKOM: Direct Kernel Object Modification) [16]파일 다운로드1
12110정성태1/11/202011961디버깅 기술: 154. Patch Guard로 인해 블루 스크린(BSOD)가 발생하는 사례 [5]파일 다운로드1
12109정성태1/10/20209860오류 유형: 588. Driver 프로젝트 빌드 오류 - Inf2Cat error -2: "Inf2Cat, signability test failed."
12108정성태1/10/20209914오류 유형: 587. Kernel Driver 시작 시 127(The specified procedure could not be found.) 오류 메시지 발생
12107정성태1/10/202010856.NET Framework: 877. C# - 프로세스의 모든 핸들을 열람 - 두 번째 이야기
12106정성태1/8/202012238VC++: 136. C++ - OSR Driver Loader와 같은 Legacy 커널 드라이버 설치 프로그램 제작 [1]
12105정성태1/8/202010932디버깅 기술: 153. C# - PEB를 조작해 로드된 DLL을 숨기는 방법
12104정성태1/7/202011607DDK: 9. 커널 메모리를 읽고 쓰는 NT Legacy driver와 C# 클라이언트 프로그램 [4]
12103정성태1/7/202014340DDK: 8. Visual Studio 2019 + WDK Legacy Driver 제작- Hello World 예제 [1]파일 다운로드2
12102정성태1/6/202011934디버깅 기술: 152. User 권한(Ring 3)의 프로그램에서 _ETHREAD 주소(및 커널 메모리를 읽을 수 있다면 _EPROCESS 주소) 구하는 방법
12101정성태1/5/202011272.NET Framework: 876. C# - PEB(Process Environment Block)를 통해 로드된 모듈 목록 열람
12100정성태1/3/20209313.NET Framework: 875. .NET 3.5 이하에서 IntPtr.Add 사용
12099정성태1/3/202011621디버깅 기술: 151. Windows 10 - Process Explorer로 확인한 Handle 정보를 windbg에서 조회 [1]
12098정성태1/2/202011196.NET Framework: 874. C# - 커널 구조체의 Offset 값을 하드 코딩하지 않고 사용하는 방법 [3]
12097정성태1/2/20209760디버깅 기술: 150. windbg - Wow64, x86, x64에서의 커널 구조체(예: TEB) 구조체 확인
12096정성태12/30/201911720디버깅 기술: 149. C# - DbgEng.dll을 이용한 간단한 디버거 제작 [1]
12095정성태12/27/201913137VC++: 135. C++ - string_view의 동작 방식
12094정성태12/26/201911317.NET Framework: 873. C# - 코드를 통해 PDB 심벌 파일 다운로드 방법
12093정성태12/26/201911339.NET Framework: 872. C# - 로딩된 Native DLL의 export 함수 목록 출력파일 다운로드1
12092정성태12/25/201910788디버깅 기술: 148. cdb.exe를 이용해 (ntdll.dll 등에 정의된) 커널 구조체 출력하는 방법
12091정성태12/25/201912299디버깅 기술: 147. pdb 파일을 다운로드하기 위한 symchk.exe 실행에 필요한 최소 파일 [1]
12090정성태12/24/201910939.NET Framework: 871. .NET AnyCPU로 빌드된 PE 헤더의 로딩 전/후 차이점 [1]파일 다운로드1
... [61]  62  63  64  65  66  67  68  69  70  71  72  73  74  75  ...