2. IP, Port, EndPoint

IP & Port
IP 사용하기
더보기
일반적으로 IPv4 주소체계 기반으로 IPAddress 객체를 생성하여 IP 주소를 변수 형태로 사용
[1]문자열 파싱, [2]바이트 배열, [3]정수 입력 할 수 있다.
using System;
using System.Net;
namespace NetworkTest
{
class Program
{
static void Main(string[] args)
{
// [1] 문자열(String)로부터 IPAddress 객체 생성
// 문자열 형식의 IPv4 주소를 직접 파싱
IPAddress ip1 = IPAddress.Parse("192.168.1.13");
Console.WriteLine($"ip1 (from string): {ip1}");
// [2] 바이트 배열(byte[])로부터 IPAddress 생성
// IPv4 주소의 각 옥텟(192.168.1.13)을 바이트 배열로 지정
IPAddress ip2 = new IPAddress(new byte[] { 192, 168, 1, 13 });
Console.WriteLine($"ip2 (from byte array): {ip2}");
// [3] 32비트 정수값으로부터 IPAddress 생성
// 218212544(10진수)는 내부적으로 0x0D01A8C0 (리틀엔디안 기준)
// 결과는 13.1.168.192 로 표시됨 (Endian 차이 설명)
IPAddress ip3 = new IPAddress(218212544);
Console.WriteLine($"ip3 (from Int32): {ip3}");
/*
* @ 엔디안(Endian) 참고:
* - IPAddress(Int64) 생성자는 "호스트 바이트 순서(리틀엔디안)" 기준으로 동작함.
* - 따라서 218212544 == 0x0D01A8C0 → 실제 IP는 13.1.168.192 로 해석됨.
* - 반면, 우리가 일반적으로 보는 '192.168.1.13'은 네트워크 바이트 순서(빅엔디안).
*/
// [4] IPAddress를 바이트 배열로 변환 + IPv6 매핑
IPAddress ip4 = IPAddress.Parse("216.58.216.174"); // 예: google.com IP 중 하나
byte[] ipBytes = ip4.GetAddressBytes(); // IP → byte[4] 변환
IPAddress ipv6 = ip4.MapToIPv6(); // IPv4 → IPv6 매핑 (::ffff:216.58.216.174)
// 결과 출력
Console.WriteLine("\n[4] IPv4 to Byte Array & IPv6 Mapping:");
Console.WriteLine($"ip4 (IPv4): {ip4}");
Console.WriteLine($"ip4 bytes : {BitConverter.ToString(ipBytes)}"); // 예: C8-3A-D8-AE
Console.WriteLine($"ip4 mapped to IPv6: {ipv6}");
}
}
}
EndPoint 사용하기
: 네트워크 종단점(IP + Port) 정보
더보기

EndPoint(종단점)
TCP나 UDP는 IP 주소와 함께 Port번호를 사용한다.
IPEndPoint는 IP주소와 포트를 받아들인 것으로 ToString() 메서드를 호출하면 "IP주소:포트" 형식으로 문자열을 리턴하거나 IP 주소 변환 등 편의 기능을 사용 할 수 있다.

using System.Net;
namespace ConsoleApp4
{
internal class Program
{
static void Main(string[] args)
{
// [1] IP 주소 생성 (로컬 루프백 주소)
IPAddress ipv4 = IPAddress.Parse("127.0.0.1");
// [2] 포트 번호 지정 (예: 5000)
int port = 5000;
// [3] IP 주소와 포트를 결합하여 IPEndPoint 생성
IPEndPoint ipv4EndPoint = new IPEndPoint(ipv4, port);
// [4] 결과 출력
Console.WriteLine("EndPoint 정보");
Console.WriteLine($"[1]주소(Address): {ipv4EndPoint.Address}");
Console.WriteLine($"[2]포트(Port): {ipv4EndPoint.Port}");
Console.WriteLine($"[3]문자열 형태: {ipv4EndPoint}");
// IPv4 주소 체계 (32비트 주소, 4옥텟) 를 사용한다는 뜻
Console.WriteLine($"[4]패밀리(AddressFamily): {ipv4EndPoint.AddressFamily}");
}
}
}
AddressFamily 알아보기
: 소켓이 사용하는 주소 체계(IPv4, IPv6 등)
더보기
using System;
using System.Net;
namespace EndpointExample
{
class Program
{
static void Main(string[] args)
{
// [1] IPv4 주소 생성 (로컬 루프백: 자기 자신)
IPAddress ipv4 = IPAddress.Parse("127.0.0.1");
// [2] 포트 번호 지정
int port = 5000;
// [3] IP + 포트를 결합하여 IPv4용 IPEndPoint 생성
IPEndPoint ipv4EndPoint = new IPEndPoint(ipv4, port);
// [4] IPv4 EndPoint 정보 출력
Console.WriteLine("=== IPv4 EndPoint 정보 ===");
Console.WriteLine($"[1] 주소(Address): {ipv4EndPoint.Address}");
Console.WriteLine($"[2] 포트(Port): {ipv4EndPoint.Port}");
Console.WriteLine($"[3] 문자열 형태(ToString): {ipv4EndPoint}");
Console.WriteLine($"[4] 패밀리(AddressFamily): {ipv4EndPoint.AddressFamily}");
// → 결과: InterNetwork (IPv4 주소 체계 사용)
Console.WriteLine();
// [5] IPv4 주소를 IPv6 형식으로 매핑 (::ffff:127.0.0.1)
IPAddress ipv6Mapped = ipv4.MapToIPv6();
Console.WriteLine($"IPv4 → IPv6 매핑 결과: {ipv6Mapped}");
// [6] 별도의 IPv6 주소로 IPEndPoint 생성
IPEndPoint ipv6EndPoint = new IPEndPoint(IPAddress.Parse("fe80::1"), 5001);
// [7] IPv6 EndPoint 정보 출력
Console.WriteLine("\n=== IPv6 EndPoint 정보 ===");
Console.WriteLine($"[1] 주소(Address): {ipv6EndPoint.Address}");
Console.WriteLine($"[2] 포트(Port): {ipv6EndPoint.Port}");
Console.WriteLine($"[3] 문자열 형태(ToString): {ipv6EndPoint}");
Console.WriteLine($"[4] 패밀리(AddressFamily): {ipv6EndPoint.AddressFamily}");
// → 결과: InterNetworkV6 (IPv6 주소 체계 사용)
}
}
}
| 열거형 | 상수 의미 | 예시 주소 |
| InterNetwork | IPv4 네트워크 | 192.168.1.13 |
| InterNetworkV6 | IPv6 네트워크 | fe80::1ff:fe23:4567:890a |
| Unix | Unix 도메인 소켓 (Linux/Unix 내부 통신) | /tmp/socket.sock |
| Bluetooth | 블루투스 통신용 | - |
| Unknown | 정의되지 않은 주소 체계 | - |