Programing (프로그래밍)/WPF & C# (C Sharp)
WPF & C# - IP Address Check ( IP 주소 체크 확인 하기 / 192.168.0.1:8080 )
insurang
2019. 2. 3. 20:19
728x90
반응형
WPF & C# - IP Address Check ( IP 주소 체크 확인 하기 / 192.168.0.1:8080 ) |
IP Address Check ( IP 주소 체크 확인 ) ex) 192.168.0.1:8080
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 31 32 33 34 35 36 37 38 39 40 41 42 | //IP Check int[] IPCheck(string ip) { string[] temp = new string[4]; int port = 0; int[] ipAddress = new int[5]; try { temp = ip.Trim().Split('.'); if (temp.Count() == 4) { if (temp[3].Contains(":")) { port = int.Parse(temp[3].Substring(temp[3].IndexOf(":") + 1)); // port temp[3] = temp[3].Substring(0, temp[3].IndexOf(":")); // } // ipAddress 배열에 넣기 for (int i = 0; i < 4; i++) ipAddress[i] = int.Parse(temp[i]); ipAddress[4] = port; // ipAddress 체크하기 for (int i = 0; i < 5; i++) { if (i == 4) // 포트 범위 체크 { if (ipAddress[i] > 65535) return null; } else // IP 대역 체크 { if (ipAddress[i] < 0) return null; if (ipAddress[i] > 255) return null; } } } } catch { return null; } // IP를 숫자로 변환 시 오류가 난다면 false return ipAddress; } | cs |
사용법
728x90
반응형