엔지니어가 되고 싶은 공돌이
10. Socket 본문
10. 1. TCP/IP
- TCP/IP: 인터넷 표준 프로토콜로 5계층으로 구성됩니다.
- Application – Transport(TCP/UDP) – Network(IP) – Data Link – Physical.
TCP | UDP |
연결지향형 | 비연결형 |
신뢰성 보장 | 신뢰성 보장하지 않음 |
순서 보장 | 순서 보장하지 않음 |
- IP: 인터넷을 이용할 때 사용하는 주소.
- Host Name: IP주소를 대신해서 사용하는 시스템에 부여된 이름.
- DNS: Host Name과 IP를 관리하는 서비스.
- Port: Host에서 동작하고 있는 서비스를 구분하는 번호.
2Byte 정수로 0 ~ 65535까지 사용가능.
10. 2. Socket
- Types of Socket
1) AF_UNIX: 유닉스 도메인 소켓(시스템 내부 프로세스간 통신에 사용).
2) AF_INET: 인터넷 소켓(네트워크를 이용한 통신에 사용).
- Communication Methods of Soket
1) SOCK_STREAM: TCP 사용.
2) SOCK_DGRAM: UDP 사용.
- Socket Function
1) int socket(int domain, int type, int protocol) : 소켓 생성.
domain은 Types of Sokcet, type은 TCP or UDP, protocol은 소켓에서 이용할 protocol.
2) int bind(int s, const struct sockaddr *name, int namelen) : 소켓의 이름 지정하기.
s에 socket int값을, name이 소켓의 이름.
3) int listen(int s, int backlog) : Client 연결 기다리기.
backlog는 최대 허용가능한 클라이언트 수.
4) int accept(int s, struct sockaddr *addr, socklen_t *addrlen) : 클라이언트 연결 요청 수락.
addr: 접속을 요청하는 Client IP Information.
5) int connect(int s, const struct sockaddr *name, int namelen) : 서버와 연결하기.
name: 접속하려는 Server IP Information.
6) ssize_t send(int s, const void *msg, size_t len, int flags) : TCP Data Send.
7) ssize_t recv(int s, void *buf, size_t len, int flags): TCP Data Receive.
8) ssize_t sendto(int s, const void *msg, size_t len, int flags, const struct sockaddr *to, int tolen);
: UDP Data Send 이며, to에 메시지를 받을 Host Address를 넣습니다.
9) ssize_t recvfrom(int s, void *buf, size_t len, int flags, struct sockaddr *from, int *fromlen);
: UDP Data Receive 이며, from에 메시지를 보내는 Host Address를 넣습니다.
- 프로그램에서 port를 사용하고 종료시, 해당 port는 일정시간동안 다시 사용되지 않도록 설정되어 있습니다.
반복적인 작업을 해야할 경우, 아래 코드를 입력하면, 설정을 해제할 수 있습니다.
int optvalue=1;
setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &optvalue, sizeof(optvalue));
'Computer Science > Unix' 카테고리의 다른 글
12. UDP (0) | 2025.03.18 |
---|---|
11. TCP (0) | 2025.03.18 |
09. Thread (0) | 2025.03.16 |
08. Fork and Exec (0) | 2025.03.15 |
07. Process (0) | 2025.03.14 |