Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 |
Tags
- 너비우선탐색
- 백트래킹
- 부분 수열의 합
- 위클리 6주차
- 코딩테스트
- dfs
- 완전 탐색
- 몯느 순열
- ElementTree
- 39080
- openssl
- 백트랙킹
- 10597
- 입실 퇴실
- 재귀
- Java
- 위클리 챌린지
- 문서자동화
- 복서 정렬하기
- 프로그래머스
- BFS
- 좋은 수열
- 그래프
- 1174
- 백준
- 순열장난
- 백트렉킹
- BOJ
- 줄어드는 숫자
- DP
Archives
개발자-H 입니다.
NetworkInterface를 이용하여 특정 NIC IPv4 주소 찾기 본문
산업용이나 서버PC같은 경우, 여러개의 NIC를 가지고 있다.
어플리케이션을 개발하다보면 특정 NIC를 사용 해야 하는 경우가 있다.
아래의 샘플 코드는 호스트 이름을 이용하여 NIC를 찾는 예제이다.
PC에서 NIC 목록은 아래의 명령어로 찾을 수 있다.
Windows > cmd > ipconfig

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Net.NetworkInformation; | |
using System.Net.Sockets; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace SampleApplication | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.WriteLine(FindEthernet.From("이더넷")); | |
} | |
} | |
public class FindEthernet | |
{ | |
public static IPEndPoint From(string hostName) | |
{ | |
//모든 NIC 정보를 가져옴 | |
var netInterfaces = NetworkInterface.GetAllNetworkInterfaces(); | |
NetworkInterface localHost = netInterfaces.Where(i => i.Name.Contains(hostName)).FirstOrDefault(); | |
if (localHost == null) | |
{ | |
throw new NullReferenceException(hostName); | |
} | |
//NIC 정보에서 IPv4 Address를 찾음 | |
var netInformation = localHost.GetIPProperties().UnicastAddresses | |
.Where(net => net.Address.AddressFamily.Equals(AddressFamily.InterNetwork)).FirstOrDefault(); | |
if (netInformation == null) | |
{ | |
throw new NullReferenceException($"null {netInformation}"); | |
} | |
//해당 Address 정보 및 사용 할수 포트 랜덤 지정하여 리턴 | |
return new IPEndPoint(netInformation.Address, 0); | |
} | |
} | |
} |
'.NET' 카테고리의 다른 글
Lua 활용하여 C# 플러그인 API 제공하기. (0) | 2021.07.11 |
---|