35
Module 6 - Practical use of Sockets

Module 6 - Practical use of Sockets

  • Upload
    gayora

  • View
    29

  • Download
    4

Embed Size (px)

DESCRIPTION

Module 6 - Practical use of Sockets. Objectives. 고성능 인터넷 서버 프로그래밍 (High performance internet server programming) 기법에 대해 이해한다 . fork() 를 이용한 다중 프로세스 프로그램에 대해 이해한다 . 다중 스레드 기법을 이용한 프로그램에 대해 소개한다 . select() 시스템 콜을 활용하여 I/O Multiplexing 소켓을 작성한다 . Multicast 개념에 대해 알아본다 . - PowerPoint PPT Presentation

Citation preview

Page 1: Module 6 - Practical use of Sockets

Module 6 - Practical use of Sockets

Page 2: Module 6 - Practical use of Sockets

Objectives

• 고성능 인터넷 서버 프로그래밍 (High performance internet server programming) 기법에 대해 이해한다 .

• fork() 를 이용한 다중 프로세스 프로그램에 대해 이해한다 .• 다중 스레드 기법을 이용한 프로그램에 대해 소개한다 . • select() 시스템 콜을 활용하여 I/O Multiplexing 소켓을 작성한다 .• Multicast 개념에 대해 알아본다 .• Multicast socket 을 작성하고 간단한 프로그램을 작성한다 . • IPv6 라이브러리에 대해 알아본다 .• IPv6 소켓 라이브러리를 이용하여 간단한 프로그램을 작성한다 .

Page 3: Module 6 - Practical use of Sockets

Server program

• Single service

• Multiple service

서버

클라이언트

서버

클라이언트(1)

클라이언트(2)

클라이언트(3)

Page 4: Module 6 - Practical use of Sockets

Multi-process

• Process– 수행 중인 program– 고유한 메모리 영역을 가지며 , 각각 다른 실행 상태를 가질 수 있다 .

• Multi-process– 여러 개의 프로세스들이 연관을 가지거나 가지지 않은 채로 작동– Fork() 함수를 호출하여 생성

기존 프로세스의 완전한 복사본을 생성 동일한 메모리 구조 , 동일한 파일 설명자들을 가짐 부모 프로세스 : 자식 프로세스의 프로세스 번호를 반환 자식 프로세스 : 0 값을 반환

– 생성된 프로세스 종료 : exit() 호출– 프로세스간의 통신

IPC(Inter-process Communication), FIFO(First In, First Out), Shared Memory, …

Page 5: Module 6 - Practical use of Sockets

Multi-thread

• 프로세스에서 새로운 영역을 가진다는 특징을 제외하고 , 새로운 파 일 설 명 자 들 을 갖 는 다 는 특 징 을 제 외 한 개 념 의 작 은 프로세스라고 볼 수 있다 .

• 한 프로세스에는 여러 개의 스레드가 존재할 수 있다 .• 스레드는 스택을 제외한 모든 메모리 공간을 공유하게 된다 .• 컨텍스트 스위칭이 빠르며 자원의 낭비가 적다 .• 기본 스레드 생성

int pthread_create(phtread_t *tid, const pthread_attr_t *inttr, void*(*start_routine)(void *), void *arg)

Page 6: Module 6 - Practical use of Sockets

select() function

• Select() function

# include <time.h>int select (

int maxfdp1, // 최대 파일 ( 및 소켓 ) 번호 크기 +1fd_set *readfds, // 읽기상태 변화를 감지할 소켓 지정fd_set *writefds, // 쓰기상태 변화를 감지할 소켓 지정fd_set *exceptfds, // 예외상태 변화를 감지할 소켓 지정struct timeval *tvptr); // select() 시스템 콜이 기다리는 시간

• Return– Success : number of file descriptors selected, file descriptor masks modified

for selected device– Failure : -1 and errno set to indicate error– Timeout : 1 and file descriptor masks cleared

Page 7: Module 6 - Practical use of Sockets

0 0…00

소켓 번호 : 0 1 2 maxfdp - 1

exceptfds :

1 0…10

소켓 번호 : 0 1 2 maxfdp - 1

Readfds :

1 0…10

소켓 번호 : 0 1 2 maxfdp - 1

writefds :

1로 셋팅되어진 소켓에 대해 조건이 만족할 때까지 select()는 호출되어지며,만족하면 select()는 리턴된다.0

Page 8: Module 6 - Practical use of Sockets

Fd_set 를 사용하기 위한 매크로

• FD_ZERO(fd_set *fdset)– *fdset 의 모든 비트를 clear

• FD_SET(int fd, fd_set *fdset)– 소켓 fd 를 해당 *fdset 이 가능하도록 구조체에 해당 비트를 1 로 세트

• FD_CLR(int fd, fd_set *fdset)– *fdset 중 소켓 fd 에 해당하는 비트를 0 으로 세트

• FD_ISSET(int fd, fd_set *fdset) – fdset 중 소켓 fd 에 해당하는 비트가 세트되어 있으면 양수 값인 fd 를 리턴

Page 9: Module 6 - Practical use of Sockets

9

Broadcast and Multicast

• Unicast– a single interface

• Broadcast– all hosts on(small, local)network

• Multicast– multiple recipients(group)

Page 10: Module 6 - Practical use of Sockets

Internet Multicasting

Sender-Oriented(One-to-Many)

Receiver-Oriented(Many-to-Many)

Page 11: Module 6 - Practical use of Sockets

Unicast Traffic

1.5 Mb x 2 = 3 Mb 1.5 Mb x 1 = 1.5 Mb

1.5 Mb x 1 = 1.5 Mb

1.5 Mb x 1 = 1.5 Mb1.5 Mb x 1 = 1.5 Mb

VideoServer

Receiver Receiver Receiver Not AReceiver

Page 12: Module 6 - Practical use of Sockets

Broadcast Traffic

Broadcast Traffic 의 경우 , Multimedia Application 을 사용하지 않는 Host 도 Broadcast Traffic 을 처리해야 한다 . 통상 Transport Layer 에서 Packet 을 폐기한다

I don’t want to receive this video stream, but my CPU still needs to process that 1.5 MB

of data!

1.5 Mb

1.5 Mb 1.5 Mb

1.5 Mb 1.5 Mb 1.5 Mb 1.5 Mb

VideoServer

Receiver Receiver Receiver Not AReceiver

Page 13: Module 6 - Practical use of Sockets

Multicast Traffic

Multicast Server 는 Multicast Address(special broadcast address) 를 사용하여 하나의 Packet 을 사용하여 모든 노드에 Data 를 전달한다 . Multicast Application 을 사용하지 않는 호스트는 통상 Layer2 에서 Frame 을 폐기할 수 있다 .

1.5 Mb

1.5 Mb 1.5 Mb

1.5 Mb 1.5 Mb 1.5 Mb

VideoServer

Receiver Receiver Receiver Not AReceiver

Page 14: Module 6 - Practical use of Sockets

Unicast vs. Multicast

Server

Router

Unicast

Server

Router

Multicast

Page 15: Module 6 - Practical use of Sockets

15

Local multicast

• Some local networks are by nature multi/broadcast: Ethernet, Token Ring, FDDI, ...

• Ethernet, Token Ring:– broadcast: all ones– multicast: 01.xx.xx.xx.xx.xx

• ATM– point-to-point links

need ATM-level multicast server

Page 16: Module 6 - Practical use of Sockets

16

IP multicast

• host-group model– Deering, 1991:– 같은 ip Multicast 주소를 가지는 host 들– groups may have any number of members– there are no topological restrictions on group membership– membership is dynamic and autonomous– host groups may be transient or permanent

• network-level– data packets same, only address changes

• need help of routers• special IP addresses(class D)

– 224.0.0.0 through 239.255.255.255• map IP multicast address into Ethernet multicast address : 01.00.5E.

00.00.00 + lower 23bits– ex) 224.0.0.1 --> 01.00.5E.00.00.01

Page 17: Module 6 - Practical use of Sockets

17

IP Multicast 와 이더넷주소 매핑

0000000100000000 001011110

Class D IP 주소

48-Bit 이더넷 주소 (MAC)

1110

0 7 8 15 16 23 24 31

Not

Used

Low-Order 23 Bits of Multi-cast

Group ID Copied to Ethernet Address

01 00 5E

224

• 01:00:5e:00:00:00 - 01:00:5e:7f:ff:ff • 01:00:5e 는 동일하다 .( 상위 24Bit 는 동일한 값을 사용하며 25 번째 bit 값은 항상 0 이다 .)• 01:00:5e +0+ 그룹 Address 의 하위 23bit 를 Octet 단위로 16 진수 값으로 변환한 값으로

산출된다 .• 224.128.64.32 와 224.0.64.32 는 01:00:5e:00:40:20 으로 동일한 이더넷 주소를 갖게 된다 .

이 경우는 IP Level 에서 구분될 것이다 .

Page 18: Module 6 - Practical use of Sockets

Multicasting 의 장점

• Bandwidth 의 최소화

unicastmulticast

sender

R receiver

R

R

R

R

Page 19: Module 6 - Practical use of Sockets

MBONE

• Multicast BackBONE• Virtual Network on the Internet• Testbed for multicast routing and applications

– routers and hosts support IP Multicasting– Multimedia Conferencing, Live TV/Radio, Distributed Game, Distance lea

rning lecture• World Wide Experimental Network• Many-to-Many, One-to-Many transfer• Tunnel Based

– virtual point-to-point link– IP-in-IP encapsulation 기술을 이용 – needed until deployment of multicast-capable a backbone routers

• tunnel 들의 모임이 MBONE• mrouter Based

Page 20: Module 6 - Practical use of Sockets

MBONE Elements

• IP Multicast(Receiver-Oriented Multicast)– “Host Extension for IP Multicasting” : RFC 1112– use D Class IP address

• Tunnel and Encapsulation• Metric, Threshold, TTL• Mbone Protocols

Page 21: Module 6 - Practical use of Sockets

Virtual Network using Tunneling• Not all routers cannot support multicasting

네트워크

: Router : Multicast Router

터 널

Page 22: Module 6 - Practical use of Sockets

Tunnel & IP EncapsulationHm

Hm HmHm

Rm

tunnelsource

tunneldest.

originalsource

multicastdest.

IP Encapsulation Normal Multicast Packet

Gm

1

1

2 3

2 3

Multicast PacketEncapsulated Packet

Rm

203.252.134.48 224.2.0.1 17 UDP RTP audio/video data (UDP)

203.252.134.46 143.248.172.41 4 (IP)

source: 203.252.134.48; group:224.0.0.1;MBONE tunnel: 203.252.134.46 to 143.248.172.41

Page 23: Module 6 - Practical use of Sockets

Metric & Threshold & TTL

• Metric– Tunnel cost– selecting Tunnel which has low metric sum– prevent multicast routing loop

• Threshold– Tunnel access-level– restrict multicast range

• TTL(Time-to-Live)– Multicast packet life-time– decrease 1 by passing mrouter– control of multicasting range

transfer multicast packet if the value of TTL is greater than that of Threshold

Page 24: Module 6 - Practical use of Sockets

MBONE Protocols

• Group Management Protocol– IGMP (Internet Group Management Protocol)

• Multicast Packet Routing Protocol– DVMRP (Distance Vector Multicast Routing Protocol)– MOSPF (Multicast Open Shortest Path First)– PIM (Protocol Independent Multicast)– CBT(Core Based Tree)

• Reliable Multicast Transport Protocol – RMTP, SRM, TMTP……..

• Multicast key management Protocol– GKMP, SMKD, …

Page 25: Module 6 - Practical use of Sockets

MBONE Tools

• Session Manager tool– Sd, sdr

• Audio tool– Vat

• Video tool– Nv, vic, ivs

• Others– Wb, nt, imm,mMOD

Page 26: Module 6 - Practical use of Sockets

Multicast programming

• UDP 소켓을 이용• Multicast 구조체

struct ip_mreq {struct in_addr imr_multiaddr;struct in_addr imr_interface;

}

• Multicast options– IP_ADD_MEMBERSHIP– IP_DROP_MEMBERSHIP– IP_MULTICAST_LOOP– IP_MULTICAST_TTL– IP_MULTICAST_IF

Page 27: Module 6 - Practical use of Sockets

Programming 절차

• 멀티캐스트 그룹주소 지정• ip_mreq 구조체 지정• 수신 UDP 소켓 생성• 멀티캐스트 그룹 가입• 소켓 재사용 옵션 지정• 바인딩• 멀티캐스트 패킷전송

Page 28: Module 6 - Practical use of Sockets

ip_mreq 구조체 지정

struct sockaddr_in mcast_group;struct ip_mreq mreq;

/* 멀티캐스트 그룹 주소 지정 : mcast_group */mreq.imr_multiaddr = mcast_group.sin_addr;mreq.imr_interfaces.s_addr = htonl(INADDR_ANY);

Page 29: Module 6 - Practical use of Sockets

• 수신 UDP 소켓 생성recv_sock = socket(AF_INET, SOCK_DGRAM, 0);

• Multicast 그룹 가입setsockopt(recv_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));

Page 30: Module 6 - Practical use of Sockets

• 소켓 재사용 옵션 지정unsigned int flag = 1;

setsockopt(recv_sock, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));

• Socket bindingbind(recv_sock, (struct sockaddr*)&mreq_group, sizeof(mcast_group));

Page 31: Module 6 - Practical use of Sockets

• 멀티캐스트 패킷전송send_sock = socket(AF_INET, SOCK_DGRAM, 0);sendto(send_sock, msg, strlen(msg), (struct sockaddr*) &mcast_group, sizeof)mcast_group));

• Loopback setsockopt(send_sock, IPPROTO_IP, IP_MULTICCAST_LOOP, &no, sizeof(no));

크기가 1 바이트 이어야 함

Page 32: Module 6 - Practical use of Sockets

IPv6 programming

• IPv4 에서 주요 변경 부분

– IP 주소의 크기를 표시한 API 부분 새로운 데이터 구조 필요

– IP 주소를 처리하는 어플리케이션 부분

Page 33: Module 6 - Practical use of Sockets

IPv6 Address family

IPv4 IPv6

Address family AF_INET AF_INET6

Protocol family PF_INET PF_INET6

Wildcard address INADDR_ANY in6addr_any

Page 34: Module 6 - Practical use of Sockets

Structure

IPv4 IPv6

IP Address 구조체 in_addr in6_addr

Socket address 구조체

sockaddr_in sockaddr_in6

Page 35: Module 6 - Practical use of Sockets

Function 변환IPv4 IPv6

Dotted Decimal 형태의 주소를 Binary 형태의 주소로

변환

inet_addr() inet_pton()

inet6_addr() : MSgetaddrinfo()

Binary 형태의 주소를 Dotted Decimal 형태의 주소로 변환

inet_ntoa() inet_ntop()inet6_ntoa() : MS

도메인 네임을 이용하여 호스트의 정보를 가져오는 함수

gethostbyname() getipnodebyname()

gethostbyname2() : Unixgetaddrinfo()

Binary 형태의 IPv4 주소를 이용하여 호스트의 정보를

가져오는 함수 gethostbyname() getipnodebyaddr()

소켓주소를 통해 호스트와 서비스의 문자열을 가져오는

함수 getnameinfo()