Upload
briar
View
31
Download
0
Embed Size (px)
DESCRIPTION
Module 4 – Sockets. Objectives. Socket 의 정의 및 개요에 대해 이해한다 . Communication Domain 의 개념과 Socket type 에 대해 알아본다 . Internet name 과 address conversion 에 대해 알아본다 . Connection-oriented/Connectionless Socket System Call 에 대해 알아본다 . TCP 와 UDP 를 사용하는 Client-Server program 을 설계하고 작성한다. - PowerPoint PPT Presentation
Citation preview
Module 4 – Sockets
Objectives
• Socket 의 정의 및 개요에 대해 이해한다 .
• Communication Domain 의 개념과 Socket type 에 대해 알아본다 .
• Internet name 과 address conversion 에 대해 알아본다 .
• Connection-oriented/Connectionless Socket System Call 에 대해 알아본다 .
• TCP 와 UDP 를 사용하는 Client-Server program 을 설계하고 작성한다 .
Socket introduction
• 정의– TCP/IP 를 이용하는 응용 프로그래밍 인터페이스 (Application Programming
Interface: API)– 소프트웨어로 만들어진 통신의 접속 점
• UNIX system 을 위하여 가장 많이 보급되고 있는 두 가지 통신 API– Berkeley socket– System V TLI(Transport Layer Interface)
TCP/IP
네트워크 드라이브
응용 1
소켓 1
응용 2
소켓 2
응용 3
소켓 3
1
2
3
4
5 ~ 7
소켓 인터페이스
Network
Socket Interface
Sockets as a Form of IPC
Host 1
Host 2
Socket
Proc 1
Proc 2
Proc 1
UNIX domain
Internet domain
Descriptor
pointer
pointer
pointer
pointer
.
.
.
0
1
2
3
Family : PF_INET
Service : SOCK_STREAM
Local IP :
Local port :
Remote IP :
Remote port :
.
.
.
파일 기술자 또는 소켓 기술자 ( 파일 또는 ) 소켓 데이터 구조체
Descriptor Table
( 파일 또는 소켓 )
응용 1(fd=3, sd=4)
소켓 1
응용 2(sd=3)
소켓 2
응용 4(sd=3)
소켓 3
소켓 인터페이스
Network
응용 3(sd=3)
소켓 2
연결형 서비스 비연결형 서비스
IP
UDPTCP
3000 3001 3002 3003
203.252.134.170
포트
TCP/IP
인터넷 계층
트랜스포트 계층
소켓
fd : File Descriptor
IP : Internet Protocol
sd : Socket Descriptor
TCP : Transmission Control Protocol
UDP : User Datagram Protocol
Application, Socket and TCP/IP
Socket types and Domains
• Socket type– SOCK_DGRAM DataGram– SOCK_STREAM Virtual circuit, byte stream– SOCK_RAW Raw
• Communications domain 정의– How sockets are named– Which protocols are used to send and receive data
• 대부분의 UNIX 에서 제공하는 두 가지 표준 도메인– UNIX domain– Internet domain
• Many other domains are possible– XNS, X.25
• Not all socket types will be supported in each domain.
UNIX Domain Sockets
• AF_UNIX
• Supported socket types : – SOCK_STREAM similar to pipes– SOCK_DGRAM unreliable, message-based
• Sockets named in the UNIX file system– Defined in /usr/include/sys/un.h – Address structure
struct sockaddr_un
Internet Domain Sockets
• AF_INET
• Supported socket types:– SOCK_STREAM access to TCP protocol– SOCK_DGRAM access to UDP protocol– SOCK_RAW access to IP or ICMP protocol
• Socket names are (Internet address, port number) pairs.– Address structure
Struct sockaddr_in– Defined in /usr/include/netinet/in.h
Connection-oriented protocol 에 대한 socket system calls
Server(connection-oriented protocol)
socket()
bind()
listen()
accept()
blocks until connectionfrom client
read()/recv()
process request
socket()
Client
connect()
write()/send()
data (reply)
data (request)
connection establish
Shoutdown()And/orClose()
Shoutdown()And/orClose()
write()/send() read()/recv()
Connectionless protocol 에 대한 socket system calls
Server(connectionless protocol)
socket()
bind()
recvfrom()
blocks until datareceived from a client
sendto()
process request
socket()
Client
bind()
sendto()
recvfrom()
data (reply)
data (request)
Normally block waiting for reply
• socket ()
#include <sys/socket.h>int socket (int family, int type, int protocol);
– family : AF_UNIX UNIX internalprotocols AF_INET Internet protocols AF_NS Xerox NS protocols AF_IMPLINK IMP link layer
– return value : sock 지정번호 , sockfd
– socket type SOCK_STREAM stream socket SOCK_DGRAM datagram socket SOCK_RAW raw socket SOCK_SEQPACKET sequenced packet socket SOCK_RDM reliably delivered message socket (not
implemented yet)
Sockets system function
Socketpair System call
• socketpair()
#include <sys/socket.h>int socketpair (
int family, // domain name int type, // socket type int protocol, // protocol
int sockvec[2] // 소켓 지정 번호 );
• Return– Success : 0– Failure : -1 and errno set to indicate error
• bind()
#include <sys/socket.h>int bind (
int socket, // 소켓 넘버const struct sockaddr *address, // 소켓과 연결할 자신의
주소를 가리키는 포인터
size_t address_len // address 가 가리키는 구조체의 크기 );
• Return– Success : 0– Failure : -1 and errno set to indicate error
Elementary Socket System call
Listen() system call
• listen()
#include <sys/socket.h>int listen (
int sockfd, // 소켓 번호 int backlog // 연결을 기다리는 클라이언트의
최대 수 );
• Return– Success : 0– Failure : -1 and errno set to indicate error
connect System call
• connect ()– local system 과 외부 system 사이의 실제적인 connection 을 설정
#include <sys/socket.h>
int connect (int sockfd, // 소켓 번호
struct sockaddr *servaddr, // 연결된 상대방의 주소 구조체를 가리키는 포인터
int addrlen // address 가 가리키는 구조체의 크기
);
• Return– Success : 0– Failure : -1 and errno set to indicate error
• accept ()– Connect 로 부터 연결을 수락
#include <sys/socket.h>int accept (
int sockfd, // 소켓 번호struct sockaddr *peer, // 연결 요청을 한 상대방의 주소를
저장하기 위한 구조체를 가리키는 포인터int *addrlen // peer 가 가리키는 구조체의 크기를
가리키는 포인터);
• Return– Success : 음이 아닌 정수 (new socket descriptor)– Failure : -1 and errno set to indicate error
accept() system call
send system call• send ()
#include <sys/socket.h>size_t send(
int socket, // 소켓 번호const void *buffer, // 전송할 메시지를 가리키는 포인터size_t length, // 전송할 메시지의 크기
int flags // 송신 방법);
– flags MSG_OOB 대역외 데이터의 발신 또는 수신 (TCP Only) MSG_DONTROUT routing 을 적용하지 않음
• Return – Success : 전송된 데이터의 길이– Failure : -1
sendto system call• sendto ()
#include <sys/socket.h>ssize_t sendto (
int socket, // 소켓 번호const void *buffer, // 전송할 메시지를 가리키는 포인터size_t length, // 전송할 메시지의 크기int flags, // 송신 방법const struct sockaddr *dest_addr, // 전송할 목적지 주소 구조체
포인터size_t dest_len // 목적지 주소 구조체의 크기);
• Return– Success : 전송된 데이터의 길이– Failure : -1
• recv ()
#include <sys/socket.h>int recv (
int sockfd, // 소켓 번호char *buff, // 데이터를 읽어 들일 버퍼의 포인터int nbytes, // 버퍼의 크기int flags // 읽는 방법);
– flags MSG_OOB 대역외 데이터의 발신 또는 수신 (TCP Only) MSG_PEEK "Peek" at the data present on the socket
• Return– Success : 읽어 들인 바이트 수– Failure : -1
recv( ) system call
• recvfrom ()
#include <sys/socket.h>int recvfrom(
int sockfd, // 소켓 번호char *buff, // 데이터를 읽어 들일 버퍼의 주소int nbytes, // 읽어 들이고자 하는 데이터의 크기int flags, // 읽는 방법struct sockaddr *from, // 데이터를 송신한 호스트의 주소int *addrlen //addr 구조체의 크기);
• Return– Success : 읽어 들인 데이터의 바이트 수– Failure : -1
recvfrom( ) system call
bcopy, bzero, bcmp system call
• bit and byte string operations
#include <strings.h>void bcopy ( const void *s1, // 소스 의 포인터
void *s2, // 복사할 곳의 포인터 size_t n ); // 소스에서 nbyte
void bzero ( void *s, //from 스트링의 시작 위치 size_t n); //n byte 만큼
int bcmp ( const void *s1, // 첫번째 스트링 const void *s2, // 두번째 스트링 size_t n); //n byte 만큼
• Return – Success : 0– Fauile : -1
Socket 을 사용한 Client/Server Example• TCP, UDP 기반 Echo 프로그램• 구성 파일
– 서버 : TCP_srv.c, UDP_srv.c– 클라이언트 : TCP_clnt.c, UDP_clnt.c– 그 외 데이터 입출력 및 데몬을 위해 필요한 파일