tcp / ip 소켓 프로그래밍
DESCRIPTION
TCP / IP 소켓 프로그래밍. 4 주차 ( Ch.15 ~ Ch.18 + α ). 세부 계획. Chapter 15~24 다중 접속 서버의 구현. 다중 접속 서버와 비 다중 접속 서버. 비 다중 접속 서버. 다중 접속 서버. 서비스 시간이 조금 느리다 접속 대기 시간이 짧다 ( 병렬 ). 서비스 시간이 빠르다 접속 대기 시간이 길다 ( 순차 ). 다중 접속 서버란 ?. 둘 이상의 클라이언트 에게 동시에 접속을 허용하여 , 동시에 둘 이상의 클라이언트에게 - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/1.jpg)
4 주차 ( Ch.15 ~ Ch.18 + α )
TCP / IP 소켓 프로그래밍
![Page 2: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/2.jpg)
세부 계획
Ch.185 주차기말고
사
Ch.19 ~ Ch.24 6 주차
채팅 프로그래밍 7 주차
채팅 프로그래밍 8 주차
스터디 계획 및 Ch.1 ~ Ch.4 1 주차
Ch.5 ~ Ch.10.2 2 주차
Ch.10.3 ~ Ch.14 3 주차
Ch.15 ~ Ch. 174 주차기말고
사
![Page 3: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/3.jpg)
CHAPTER 15~24다중 접속 서버의 구현
![Page 4: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/4.jpg)
• 서비스 시간이 조금 느리다• 접속 대기 시간이 짧다 ( 병렬 )
다중 접속 서버와 비 다중 접속 서버비 다중 접속 서버 다중 접속 서버
• 서비스 시간이 빠르다• 접속 대기 시간이 길다 (
순차 )
![Page 5: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/5.jpg)
다중 접속 서버란 ?
둘 이상의 클라이언트에게 동시에 접속을 허용하여 ,
동시에 둘 이상의 클라이언트에게 서비스를 제공하는 서버를 의미한다 .
![Page 6: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/6.jpg)
다중 접속 서버의 종류
멀티 프로세스 서버
멀티 플렉싱 서버
멀티 쓰레드 서버
![Page 7: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/7.jpg)
멀티 플렉싱 서버에서의 EPOLLSelect 함수의 단점
해결책
if((fd_num=select(fd_max+1, &cpy_reads, 0, 0, &timeout))==-1)
…….
for(i=0; i<fd_max+1; i++) {
if(FD_ISSET(i, &cpy_reads)) { ….. FD_SET(clnt_sock, &reads); .…. }
else { ….. FD_CLR(i, &reads); ….. }
}
![Page 8: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/8.jpg)
EPOLL 의 장점
epoll 구현의 함수
select 구현의 함수
VS
![Page 9: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/9.jpg)
• 프로세스간 메모리가 독립적으로 운영되기 때문에 프로세스간 데이터 공유 불가능
• 운영체제가 별도로 제공하는 메모리공간을 대상으로 별도의 IPC 기법 적용
• 프로세스의 생성에는 많은 리소스가 소모• 프로세스가 생성되면 , 프로세스간의 컨텍스트 스위칭으로 인해서 성능이
저하• 컨텍스트 스위칭은 프로세스의 정보를 하드디스크에 저장 및 복원 하는
일
쓰레드의 등장 배경프로세스는 부담스럽다 .
데이터의 교환이 어렵다 .
![Page 10: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/10.jpg)
그래서 쓰레드는 ?
• 프로세스보다 가벼운 , 경량화된 프로세스• 컨텍스트 스위칭이 빠름• 쓰레드 별로 메모리공유가 가능하기 때문에
별도의 IPC 기법 불필요
![Page 11: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/11.jpg)
프로세스는 서로 완전히 독립적프로세스는 운영체제 관점에서의 실행 흐름을 구성
쓰레드와 프로세스의 차이점프로세스 쓰레드
쓰레드는 데이터 영역과 힙 영역을 공유 쓰레드는 프로세스 내에서의 실행 흐름
![Page 12: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/12.jpg)
쓰레드의 문제점• 쓰레드 메모리공유가 가능하기 때문에 문제가 발생
← 정상적인 접근↓ 잘못된 접근
![Page 13: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/13.jpg)
쓰레드의 임계영역• 둘 이상의 쓰레드가 동시에 실행하면 문제를 일으키는 영역
↑ 임계영역 ?
임계영역 ?
전역변수LONG LONG NUM = 0;
NO!!!YES!!
!
![Page 14: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/14.jpg)
해결 방법은 ? 동기화 !• 둘 이상의 쓰레드가 동시에 실행하면 문제를 일으키는 영역
뮤텍스 (MUTEX)
- 동기 접근에 대한 해결책
세마포어 (SEMAPHORE)
- 실행 순서 컨트롤 중심의 컨트롤
![Page 15: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/15.jpg)
다중 접속 서버의 종류
Multi Con-nection Server
Multi Process
Multi Threading
Multi Plexing
SelectEpoll(linux)
Asynchronous Notification IO
(windos)
Overlapped IO
(windows)
IOCP(windows)
![Page 16: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/16.jpg)
다중 접속 서버의 선택
절대 우위를 점하는 서버의 모델은 없다 !
상황에 맞게 적절한 모델을 선택할 수 있어야 한다 .
![Page 17: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/17.jpg)
채팅 프로그램 구현 계획
![Page 18: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/18.jpg)
세부 계획
Ch.18
5주차기말고사
Ch.19 ~ Ch.246
주차
채팅 프로그래밍7
주차
채팅 프로그래밍8
주차
Ch.185 주차기말고
사
Ch.19 ~ Ch.24 6 주차
채팅 프로그래밍 7 주차
![Page 19: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/19.jpg)
• 멀티캐스트• 멀티 쓰레드 ( 혹은
멀티 프로세스 )
• 리눅스 (C 언어 )
구현 환경과 내용구현 환경 구현 내용
![Page 20: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/20.jpg)
• 서버에 접속 및 종료• 전송 받은 내용 화면
표시• 파일 요청 및 수신
• 클라이언트간 통신 중계
• 대화 내용 및 요청 저장
• 파일 요청에 따른 제공 및 중계
구현 계획서버 클라이언트
![Page 21: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/21.jpg)
멀티프로세스 기반의 서버구현
![Page 22: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/22.jpg)
프로세스란 ?
• 간단하게는 실행중인 프로그램을 뜻한다 .
• 실행중인 프로그램에 관련된 메모리 , 리소스등을 총칭하는 의미이다 .
• 멀티프로세스 운영체제는 둘 이상의 프로세스를 동시에 생성 가능하다 .
![Page 23: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/23.jpg)
FORK 함수• fork 함수가 호출되면 ,
호출한 프로세스가 복사되어 fork 함수 호출 이후를 각각의 프로세스가 독립적으로 실행하게 된다 .
부모
자식
![Page 24: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/24.jpg)
FORK 함수 사용 방법pid=fork();
if(pid==0) // if Child Process
gval+=2, lval+=2;
else // if Parent Process
gval-=2, lval-=2;
if(pid==0)
printf("Child Proc: [%d, %d] \n", gval, lval);
else
printf("Parent Proc: [%d, %d] \n", gval, lval);
![Page 25: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/25.jpg)
좀비 프로세스실행이 완료되었음에도 불구하고 ,
소멸되지 않은 프로세스
퇴치 방법은 ?
• wait 함수• waitpid 함수
![Page 26: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/26.jpg)
시그널 핸들링특정 상황이 되었을 때 운영체제가 프로세스에게 해당 상황이 발생했음을 알리는 일종의 메시지를 가리켜 시그널이라 한다 .
pid=fork();
……………
if(pid==0)
printf("Child Proc: [%d, %d] \n", gval, lval);
else
printf("Parent Proc: [%d, %d] \n", gval, lval);
return 0;
시그널 발생 !
![Page 27: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/27.jpg)
프로세스 기반 다중접속 서버
연결이 하나 생성될 때마다 프로세스를 생성해서 해당 클라이언트에 대해 서비스를 제공하는 것이다 .
![Page 28: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/28.jpg)
FORK 를 통한 디스크립터의 복사
프로세스에 의해 만들어진 소켓이 복사되는 게 아니고 ,
파일 디스크립터가 복사된다
![Page 29: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/29.jpg)
멀티플렉싱 기반의 서버 구현
![Page 30: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/30.jpg)
멀티 프로세스의 단점
• 프로세스의 빈번한 생성은 성능의 저하로 이어진다 .
• 멀티프로세스의 흐름을 고려해서 구현해야 하기 때문에 구현이 쉽지않다 .
• 프로세스간 통신이 필요한 상황에서는 서버의 구현이 더 복잡해진다 .
![Page 31: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/31.jpg)
멀티플렉싱이란 ?전자 및 통신공학에서의 멀티
플렉싱의 의미“ 하나의 통신채널을 통해서
둘 이상의 데이터를 전송하는데 사용되는 기술”
Server
Client
Client
Client
서버에 적용한 멀티 플렉싱
![Page 32: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/32.jpg)
SELECT 함수• 멀티플렉싱 서버의 구현에 있어서 가장 대표적인 방법• Select 함수를 사용하면 한곳에 여러 개의 파일 디스크립터를 모아놓고
동시에 관찰 할수있다 .
• 수신한 데이터를 지니고 있는 소켓이 존재하는가 ?
• 블로킹되지 않고 데이터의 전송이 가능한 소켓은 무엇인가 ?
• 예외상황이 발생한 소켓은 무엇인가 ?
Select 함수 호출과정
![Page 33: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/33.jpg)
파일 디스크립터의 설정• 함수 안에는 파일들을 관찰항목 ( 수신 , 전송 , 예외 ) 에
따라서 구분해서 모으기 위한 fd_set 형 변수존재
![Page 34: TCP / IP 소켓 프로그래밍](https://reader030.vdocuments.pub/reader030/viewer/2022012819/56812b24550346895d8f2344/html5/thumbnails/34.jpg)
검사의 범위지정과 타임아웃의 설정
* 변수 maxfd 는 파일 스크립터의 검사 범위를 지정 파일 디스크립터의 수를 넣는다 .* 변수 timeout 은 함수의 블로킹 상태를 방지하기 위하여 설정 .* 반환 값 파일스크립터가 변화 변화한 파일스크립터 수timeout 에 의한 반환 값 0오류발생시 -1