-
[TIL] 2022년 10월 8일 토요일카테고리 없음 2022. 10. 8. 12:05
1.2 IP주소(IP address)
- IP(Internet Protocol) 인터넷에서 다른 컴퓨터와 통신할 때 사용하는 프로토콜
- 웹사이트에 접속할 때, 우리가 네이버의 주소를 입력하더라도 DNS Server를 거쳐 IP주소로 변환하고 이 주소로 통신함
- terminal이나 cmd 창에 nslookup naver.com 이라고 검색하면 아래와 같은 결과가 나오는데 아래 네 개의 IP주소를 입력하면 모두 네이버로 연결됨
- 현재 IP는 32비트(4byte) 체계를 사용하여 약 40억 개의 주소를 표현 가능
- 1byte씩 구분하여 점을 찍고 부호 없는 1byte의 값, 0~255 사이의 정수로 표현
- 현재 사용하는 IP 주소 체계는 IPv4로 현재 전세계의 컴퓨터를 40억 개로 처리할 수 없어 IPv6가 등장
- IP주소는 네트워크주소와 호스트주소로 나눌 수 있는데, 네트워크를 어떻게 구성했는가에 따라 네트워크주소와 호스트주소 각각의 bit는 달라짐
- IP주소와 서브넷마스크를 '&'연산하면 네트워크 주소를 얻을 수 있어 서로 다른 호스트의 IP주소를 서브넷마스크로 '&'연산하여 비교하면 두 호스트가 같은 네트워크 상 존재하는지 확인할 수 있음
1.3 InetAddress
- InetAddress는 자바에서 IP주소를 다루는 클래스
더보기package ch16; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Arrays; public class NetworkEx1 { public static void main(String[] args) { InetAddress ip = null; InetAddress[] ipArr = null; try{ ip = InetAddress.getByName("www.naver.com"); System.out.println("getHostName() : " + ip.getHostName()); System.out.println("getHostAddress() : " + ip.getHostAddress()); System.out.println("toString() : " + ip.toString()); byte[] ipAddr = ip.getAddress(); System.out.println("getAddress() : " + Arrays.toString(ipAddr)); String result = ""; for(int i=0; i<ipAddr.length; i++){ result += (ipAddr[i] < 0) ? ipAddr[i] + 256 : ipAddr[i]; result += "."; } System.out.println("getAddress() + 256 : " + result); System.out.println(); }catch (UnknownHostException e){ e.printStackTrace(); } try { ip = InetAddress.getLocalHost(); System.out.println("getHostName() : " + ip.getHostName()); System.out.println("getHostAddress() : " + ip.getHostAddress()); System.out.println(); }catch (UnknownHostException e){ e.printStackTrace(); } try { ipArr = InetAddress.getAllByName("www.naver.com"); for(int i=0; i<ipArr.length; i++){ System.out.println("ipArr[" + i + "] : " + ipArr[i]); } }catch (UnknownHostException e){\ e.printStackTrace(); } } }
1.4 URL(Uniform Resource Locator)
- 인터넷에 존재하는 여러 서버들이 제공하는 자원에 접근할 수 있는 주소를 표현하기 위함
- '프로토콜://호스트명:포트번호/경로명/파일명?쿼리스트링#참조'의 형태
더보기package ch16; import java.net.URL; public class NetworkEx2 { public static void main(String[] args) throws Exception{ URL url = new URL("http://www.codechobo.com:80/sample/hello.html?referer=codechobo#index1"); System.out.println("url.getAuthority() : " + url.getAuthority()); System.out.println("url.getContent() : " + url.getContent()); System.out.println("url.getDefaultPort() : " + url.getDefaultPort()); System.out.println("url.getPort() : " + url.getPort()); System.out.println("url.getFile() : " + url.getFile()); System.out.println("url.getHost() : " + url.getHost()); System.out.println("url.getPath() : " + url.getPath()); System.out.println("url.getProtocol() : " + url.getProtocol()); System.out.println("url.getQuery() : " + url.getQuery()); System.out.println("url.getRef() : " + url.getRef()); System.out.println("url.getUserInfo() : " + url.getUserInfo()); System.out.println("url.toExternalForm() : " + url.toExternalForm()); System.out.println("url.toURI() : " + url.toURI()); } }
1.5 URLConnection
- URLConnection클래스는 애플리케이션과 URL사이의 통신 연결을 대표하는 모든 클래스의 최상위 클래스로 추상클래스
- URLConnection클래스의 인스턴스는 URL이 참조하는 자원으로부터 데이터를 읽고 쓸 수 있음
- URLConnection클래스를 상속받은 클래스는 HttpURLConnection, JarURLConnection이 있음
2. 소켓 프로그래밍
- 소켓이란 프로세스 사이 통신에 사용되는 양쪽 끝단(endpoint)
- 소켓 프로그래밍은 소켓을 이용한 통신 프로그래밍
2.1 TCP와 UDP
- TCP와 UDP는 OSI 7계층의 하나인 전송계층에 해당하는 프로토콜로 TCP/IP 프로토콜에 포함
- 이기종 시스템간 통신을 위한 표준 프로토콜로 프로토콜의 집합