
클라이언트에서 서버를 찾을때, 우리가 아는 주소 이름 naver.com으로는 찾을 수가 없다.
naver.com은 주소값을 찾아오기 위한 문자열일 뿐이고, 실제 주소값은 모두 숫자로 되어있다.

실제로는 이렇게 통신을 하는데, DNS서버가 존재해서 naver.com의 IP주소 (예를들어 7.7.7.7) 를 주고받고, 이를 통해서 IP주소를 알게 된다. 하지만 이렇게 하면 컴퓨터만 찾게 된다.
컴퓨터 내에서 프로그램을 식별하려면 Port가 필요한데, 이 Port값은 찾지 못하는 것이다.
=즉 , 프로그램을 실행하기 위해서는 IP주소와 Port번호가 필요하다.
cmd창에서 ipconfig를 쳐보면, 나의 IPv4주소를 알 수 있다.
InetAddress
- 특정 도메인의 ip주소에 대한 정보를 관리하는 클래스.
- hostname
- ip 매칭정보를 가지고 있다.
- 생성자가 아닌 static메소드만 제공한다.
-localHost는 127.0.0.1 이다.
private void test1() {
try {
InetAddress naver=InetAddress.getByName("naver.com");
//System.out.println(naver.getHostAddress()); ///ip반환 223.130.195.200
InetAddress[] navers = InetAddress.getAllByName("naver.com");
for(InetAddress n : navers) {
System.out.println(n.getHostAddress());
}
InetAddress localhost = InetAddress.getLocalHost(); //내컴퓨터 ip를 가진 InetAddress객체 반환
System.out.println(localhost.getHostAddress());
//localhost = 127.0.0.1
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

네이버의 IP 주소가 4개인 것을 확인할 수 있다.
URL클래스
자바8 api를 입력하는 Url 클래스를 만들어서 기능들을 확인해보자.
private void test1() {
String address="https://docs.oracle.com:443/javase/8/docs/api/java/net/InetAddress.html";
try {
URL url=new URL(address);
System.out.println(url.getProtocol()); //http https 를 프로토콜이라함.
System.out.println(url.getHost()); // docs.oracle.com
System.out.println(url.getPort()); // 443
System.out.println(url.getPath()); // /javase/8/docs/api/java/net/InetAddress.html
System.out.println(url.getQuery()); // id=abcde&mode=true
System.out.println(url.getRef()); // bookmark
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
다음과 같은 함수들을 확인할 수 있다.
TCP 소켓 프로그래밍

1. 서버 프로그램을 실행. 특정 포트에 서버 소켓을 만듦(연결요청 화살표 쪽)
2. 클라이언트는 어느컴퓨터인지, 어떤포트인지 알아야 한다. 1. 에서 만들어진 특정 포트와 소켓에 접속한다.
연결요청을 한 포트는 입장 포트일 뿐이다
3. 연결요청한 소켓이 아닌 다른 소켓을 만들어 통신을 한다.
다음과 같은 방법으로 코드를 작성해본다.
단일스레드 채팅 만들기
단점 ) 각자 1번밖에 말을 못함 ㅠㅠ

멀티쓰레드 채팅 만들기
동시에 대화 가능, 여러명 대화 가능. 한번에 여러번 말할 수 있음
(1컴퓨터에서 실행해서 동시에 하는 것은 찍을 수 없었음 ㅠㅠ)

이렇게 네트워크를 통한 멀티 클라이언트-서버 채팅까지 만들어보았다.