네트워크 구조는 두개로 나뉘어 진다.
Client-Server
한개의 서버에 여러개의 client가 접속하는 구조(대부분의 통신 방식)
e.g) web server(HTTP), file server(FTP), application server
Peer-to-Peer(P2P)
서버는 없고 client끼리 서로 통신하는 구조
e.g) bittorrent
IP로 쳤을때 우리가 원하는 사이트로 이동을 하지만 매번 IP로 이동하기에는 외우기 매우 어렵다. 그래서 IP를 Name으로 바꾸어 주는 Name Server가 존재한다.
인터넷 내부에 있는 name server(168.126.63.1)로 접속한다음 IP를 가지고 특정 URL에 접속하게 된다. 하지만 이렇게 하면 서버를 갔다 도메인으로 가는 귀찮은 구조이기 때문에 캐쉬를 사용하여서 이동한다.
서버에 접속하기 위해서는 IP address도 필요하지만 Port number도 필요하다. 서버용 컴퓨터가 여러가지 용도로 사용되기 때문에 IP address 하나에서 여러 용도를 사용하기 위해 구분이 필요하다. 그래서 Port numer가 따로 존재하여 기능을 구분해 준다.
16-bit integer로 구성되며 0 to 65535까지의 번호를 가지게 된다.
e.g) http(80), https(443), ftp(21), ssh(22), telnet(23)
그래서 client가 server에 접속시 IP address와 port number을 함께 보낸다.
InetAddress는 InetAdress Object를 얻는 것을 이야기 한다.
getByAddress는 IP Address를 이용해서 InetAddress라는 Object를 만들겠다.
getByName는 Domain Name을 가지고 InetAddress를 만들겠다.
getAllByName는 Domain Name이 가지고 있는 모든 IP Address를 InetAddress Object로 만들어 반환해 준다.
getHostName은 IP address의 host name을 반환해 준다.
InetAddress ip = 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());
} catch (UnknownHostException e) {
e.printStackTrace();
}
InetAddress ip = null;
try {
// Name을 기준으로 객체를 가져온다.
ip = InetAddress.getByName("www.naver.com");
// byte array 형식으로 주소를 받아오는 부분
byte[] ipAddr = ip.getAddress();
System.out.println("getAddress(): " + Arrays.toString(ipAddr));
String result = "";
//만약에 나온 값이 음수라면 256만큼을 더해준다는 의미이다. 이게 byte는 -128~127까지만 표현가능하기 때문에 해준 후속 처리이다.
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();
}
InetAddress ip = null;
try {
// 현재 사용하고 있는 컴퓨터의 InetAddress Object 생성
ip = InetAddress.getLocalHost();
// HostName 알아내기
System.out.println("getHostName(): " + ip.getHostName());
// HostAddress 알아내기
System.out.println("getHostAddress(): " + ip.getHostAddress());
System.out.println();
} catch (UnknownHostException e) {
e.printStackTrace();
}
InetAddress[] ipArr = null;
try {
// 하나의 address에 부여된 모든 IP Address를 가져오기
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();
}
URL 클래스는 주소창에 치게되는 도메인 주소에서 조금 더 구체적인 주소까지를 이야기 한다.
e.g) http://www.example.com/index.html
– URL = scheme:[//authority]path[?query][#fragment]
authority(www.example.com), path(/index.html), ?query(리소스를 받는 형태), #fragment(항목들)
– authority = [userinfo@]host[:port]
userinfo@(login이 필요한 경우), host(www.example.com), :port(포트 번호)
String에 대한 URL을 주거나
protocol, host, file을 주거나
protocol, host, port, file을 주는 방법이 존재한다.
URL url = new URL("http://docs.oracle.com/javase/10/docs/api/java/net/URL.html");
URL url = new URL("http", "docs.oracle.com", "/javase/10/docs/api/java/net/URL.html");
URL url = new URL("http", "docs.oracle.com", 80, "/javase/10/docs/api/java/net/URL.html");
URL에 대한 내장함수들은 위와 같이 구성된다.
URL에 접속해서 하는 활동들은 위와 같이 구성된다.
URL url = new URL("http://www.google.com");
//URL url = new URL("http://mickeymouse@www.google.com");
//URL url = new URL("http://www.youtube.com/results?search_query=java");
//URL url = new URL("https://wikitravel.org/en/Main_Page");
//URL url = new URL("https://en.wikipedia.org/wiki/Java_(programming_language)#Syntax");
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());