본문 바로가기
개발일지/수업내용

210817(목)_DAY 17

by jungwonyu 2021. 8. 12.
728x90

[Client & Server]

 

 - Client: 서비스를 받는 프로그램

 - Server: 서비스를 제공하는 프로그램

 

 

 

[IP & Port]

- IP: 컴퓨터의 고유 주소

- Port: 서버는 고정적인 포트 번호를 가지고 실행(binding) / 클라이언트가 서버에서 보낸 정보를 받을 때는 동적 포트 번호 사용

 eg/ FTP 클라이언트(21번) → FTP 서버(21번)

 

▶ getLocalHost( ) / getByName( ) / getAllByName( )

- getLocalHost( ): 로컬 컴퓨터의 InetAddress 가져오기

- getByName( ): 매개 값으로 준 도메인 이름으로 DNS에서 하나의 IP를 가져와서 InetAddress 생성 & 리턴

- getAllByName( ): 하나의 도메인, 여러 IP를 가지고 있을 때 사용

 

package com.test;
import java.net.*;
public class Test {
	public static void main(String[] args) {
		try {
			InetAddress[] address_all = InetAddress.getAllByName("www.daum.net"); // 다음에 등록된 모든 IP 주소 얻기
			for (InetAddress res : address_all) {
				System.out.println(res.getHostAddress());
				System.out.println(res.getHostName());

				byte[] res02 = res.getAddress();
				for (byte r0 : res02) {
					System.out.printf("%d.", r0);
				}
				System.out.println();
				System.out.println(res.getLocalHost()); // 로컬 컴퓨터의 InetAddress 얻기
				System.out.println(res.getByName("www.abc.com")); // 도메인 이름으로 InetAddress 얻기
			}
		} catch (UnknownHostException e) { 
			e.printStackTrace();
		}
	}
}

[TCP Networking]

- Transmission Control Protocol 연결 지향적 프로토콜(클라이언트-서버가 연결된 상태에서 데이터를 주고받는 프로토콜)

- 고정된 통신 선로를 통해 데이터를 순차적 전달 → 정확하고 안전하게 전달

- 데이터를 보내기 전에 연결이 되어 있어야 함 → 전송 속도 느림

 

▶ ServerSocket / Socket

 

 

- ServerSocket: 클라이언트의 연결 요청을 기다리며 연결 수락

- Socket: 연결된 클라이언트와 통신 담당

 

· Server: 실행 → Client: Socket 생성 & 연결 요청 → ServerSocket: 연결 수락(accept) & 통신용 Socket 생성 

⇒ 각각의 Socket을 이용해 데이터를 주고받음