[C#] 인터넷 연결 체크 함수

 
인터넷 연결 체크 함수
인터넷이 연결됐는지 간단하게 체크하는 코드 .NET 2.0 이상 포함된 함수

GetIsNetworkAvailable

if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
return true;
}
else
{
return false;
}

PDA 에서 인터넷 연결체크를 위해 작성한 코드 입니다.
WIFI 로 IP를 할당 받았는지 확인하는 팁입니다.
인터넷에서 본 팁을 적용했습니다.

bool bRetVal;

try
{
string sHostName = System.Net.Dns.GetHostName();
  IPHostEntry ipheThisHost = System.Net.Dns.GetHostEntry(sHostName);
  IPAddress ipThisAddr = ipheThisHost.AddressList[0];
  string ip = ipThisAddr.ToString();
  string localhost = IPAddress.Parse("127.0.0.1").ToString();
  if (ip == localhost)
  {
    bRetVal = false;
  }
  else
  {
    bRetVal = true;
  }
}
catch
{
  bRetVal = false;
}

return bRetVal;

AP를 할당받았을때 IP가 세팅이 되면 인터넷이 연결된것으로 봅니다.
이코드의 문제점은 고정IP 할당된 PDA에서는 사용할수 없습니다.

댓글

이 블로그의 인기 게시물

2025년 7월 다이어리

1. 대학교 축제 전시 프로젝트