[CN] Socket Programming
Computer Network aspect
What is Socket?
An internal endpoint of a connection: 결국 socket도 자료구조이다!
- socket은 internal, port가 external
Categories of Socket
- socket address: IP address + 16-bit integer port
- socket mode: SOCK_STREAM(TCP), SOCK_DGRAM(UDP), SOCK_RAW
- socket API: bind(), listen(), accpet()
Server API
socket() –> bind() –> listen() –> accept() –> R/W –> … –> close()
socket으로 socket descriptor 받고, bind로 ip address + port number를 엮어 descriptor에 bind.
listen으로 TCP request 듣게 하는데 int backlog 만큼만 듣기
accept으로 client 기다림
Client API
socket() –> connect() –> R/W –> … –> close()
socket으로 socket descriptor 받고, connect로 server-client간의 connection 지정
read, write는 buffer 하나 끼고 진행