Module 4 Sockets Objectives Socket TCP UDP ClientServer
Module 4 – Sockets
Objectives • • Socket의 정의 및 개요에 대해 이해한다. • TCP와 UDP를 사용하는 Client-Server program을 설계하고 작 성한다. Communication Domain의 개념과 Socket type에 대해 알아본다. Internet name과 address conversion에 대해 알아본다. Connection-oriented/Connectionless Socket System Call에 대해 알아본다.
Socket introduction • 정의 – TCP/IP를 이용하는 응용 프로그래밍 인터페이스(Application Programming Interface: API) – 소프트웨어로 만들어진 통신의 접속 점 • UNIX system을 위하여 가장 많이 보급되고 있는 두 가지 통신 API – Berkeley socket – System V TLI(Transport Layer Interface)
Sockets as a Form of IPC Host 1 Host 2 Socket Proc 1 Proc 2 UNIX domain Internet domain
Descriptor 파일 기술자 또는 소켓 기술자 (파일 또는) 소켓 데이터 구조체 0 pointer 1 pointer Family : PF_INET Service : SOCK_STREAM 2 pointer 3 pointer Local IP : Local port : . . . Descriptor Table (파일 또는 소켓) Remote IP : Remote port : . . .
Application, Socket and TCP/IP 연결형 서비스 비연결형 서비스 응용 1 응용 2 응용 3 응용 4 (fd=3, sd=4) (sd=3) 소켓 인터페이스 소켓 1 소켓 2 3000 포 트 소켓 2 3001 3002 TCP fd : File Descriptor IP : Internet Protocol sd : Socket Descriptor TCP : Transmission Control Protocol UDP : User Datagram Protocol 소켓 3003 UDP IP TCP/IP 소켓 3 트랜스포트 계층 인터넷 계층 203. 252. 134. 170 Network
Socket types and Domains • Socket type – SOCK_DGRAM – SOCK_STREAM – SOCK_RAW • Data. Gram Virtual circuit, byte stream 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 – SOCK_DGRAM • similar to pipes 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 – SOCK_DGRAM – SOCK_RAW • access to TCP protocol access to UDP protocol 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() Client accept() socket() blocks until connection from client read()/recv() connection establish connect() data (request) write()/send() process request write()/send() Shoutdown() And/or Close() data (reply) read()/recv() Shoutdown() And/or Close()
Connectionless protocol에 대한 socket system calls Server (connectionless protocol) socket() Client bind() socket() recvfrom() bind() blocks until data received from a client data (request) sendto() process request recvfrom() data (reply) sendto() Normally block waiting for reply
Sockets system function • socket () #include <sys/socket. h> int socket (int family, int type, int protocol); – family : ü AF_UNIX ü AF_INET ü AF_NS ü AF_IMPLINK UNIX internalprotocols Internet protocols Xerox NS protocols IMP link layer – return value : sock 지정번호, sockfd – socket type ü SOCK_STREAM ü SOCK_DGRAM ü SOCK_RAW ü SOCK_SEQPACKET ü SOCK_RDM stream socket datagram socket raw socket sequenced packet socket reliably delivered message socket (not implemented yet)
Socketpair System call • socketpair() #include <sys/socket. h> int socketpair ( int family, int type, int protocol, int sockvec[2] ); • // domain name // socket type // protocol // 소켓 지정 번호 Return – Success : 0 – Failure : -1 and errno set to indicate error
Elementary Socket System call • 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
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() system call • 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
send system call • send () #include <sys/socket. h> size_t send( int socket, const void *buffer, size_t length, int flags ); – flags ü MSG_OOB ü MSG_DONTROUT • Return – Success : 전송된 데이터의 길이 – Failure : -1 // 소켓 번호 // 전송할 메시지를 가리키는 포인터 // 전송할 메시지의 크기 // 송신 방법 대역외 데이터의 발신 또는 수신 (TCP Only) routing을 적용하지 않음
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( ) system call • recv () #include <sys/socket. h> int recv ( int sockfd, //소켓 번호 char *buff, //데이터를 읽어 들일 버퍼의 포인터 int nbytes, //버퍼의 크기 int flags //읽는 방법 ); • – flags ü MSG_OOB ü MSG_PEEK Return – Success : 읽어 들인 바이트 수 – Failure : -1 대역외 데이터의 발신 또는 수신(TCP Only) "Peek" at the data present on the socket
recvfrom( ) 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
bcopy, bzero, bcmp system call • bit and byte string operations #include <strings. h> void bcopy ( const void *s 1, void *s 2, size_t n ); void bzero ( void *s, size_t n); int bcmp ( const void *s 1, const void *s 2, size_t n); • Return – Success : 0 – Fauile : -1 //소스 의 포인터 //복사할 곳의 포인터 //소스에서 nbyte //from 스트링의 시작 위치 //n byte만큼 //첫번째 스트링 //두번째 스트링 //n byte 만큼
- Slides: 24