You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 접속 대기
Socket client = tcplistener.AcceptSocket();
// 2초 동안 수신하지 못하면 타임아웃으로 설정
client.SetSocketOption( SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 2000 );
var adapters = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
foreach (var adapter in adapters)
{
if (adapter.OperationalStatus.Equals(System.Net.NetworkInformation.OperationalStatus.Up))
{
var properties = adapter.GetIPProperties();
foreach (var ipInfo in properties.UnicastAddresses)
{
var ip = ipInfo.Address;
if (!System.Net.IPAddress.IsLoopback(ip))
{
Console.WriteLine("IP = " + ip);
Console.WriteLine("MAC = " + adapter.GetPhysicalAddress());
}
}
}
}
맥어드레스 얻기 - SendARP 방식
using System;
using System.Net;
using System.Runtime.InteropServices;
[DllImport("iphlpapi.dll", ExactSpelling=true)]
private static extern int SendARP( int DestIP, int SrcIP, byte[] pMacAddr, ref int PhyAddrLen );
private byte[] getMacAddress(string val)
{
return getMacAddress(IPAddress.Parse(val));
}
private byte[] getMacAddress(IPAddress addr)
{
byte[] mac = new byte[6];
int len = mac.Length;
int r = SendARP( BitConverter.ToInt32(addr.GetAddressBytes(), 0), 0, mac, ref len );
return mac;
}
UDP 브로드캐스트
IPEndPoint remoteIP = new IPEndPoint(IPAddress.Broadcast, 10002);
byte[] data = new byte[16];
Socket s = new Socket( AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp );
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, 16);
// 브로드캐스트는 옵션으로 사용 가능하도록 한다
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1);
s.SendTo(data, data.Length, SocketFlags.None, remoteIP);
UDP TTL 지정
// 보낼 곳
IPEndPoint remoteIP = new IPEndPoint(IPAddress.Parse("192.168.11.2"), 80);
// 보낼 데이터
byte[] data = new byte[16];
// UDP 소켓 만들기
Socket s = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.U에 );
// TTL를 설정
// TTL라는 것은…→ http://e-words.jp/w/TTL-2.html
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.IpTimeToLive, 255);
// 데이터를 보낸다
s.SendTo(data, 0, data.Length, SocketFlags.None, remoteIP);
컴퓨터의 네트워크 카드 리스트
using System.Management; // 참조 설정에 System.Management 를 추가
ManagementClass mc = new ManagementClass("Win32_PerfRawData_Tcpip_NetworkInterface");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
// 정보를 표시
Console.WriteLine("이름 = {0}", mo["Name"]);
Console.WriteLine("접속 속도 = {0} Mbps",
Convert.ToInt32(mo["CurrentBandwidth"]) / 1000 / 1000);
Console.WriteLine("------");
}