[ํ์คํ] TCP Socket programming (1:1)
2023. 4. 12. 23:00ใComputerScience/FullStackProgramming
Network Socket
- Network Socket is an internal endpoint for sending or receiving data within a node on a computer network.
- a representation of this endpoint in networking software (protocol stack)
- such as an entry in a table (listing communication protocol, destination, status, etc.), and
is a form of system resource. - 4๊ณ์ธต์ ๊น๊ณ , Application ์ด์ผ๊ธฐํ๋ค.
- IP ์ฃผ์ + ํฌํธ ๋ฒํธ -> socket address
- 4๊ณ์ธต๊ณผ 3๊ณ์ธต์ ์ด์์ฒด์ ์์ ์๋ค.
- ์์ผ ํ๋ก๊ทธ๋๋ฐ -> ์ด์์ฒด์ ์ ์ด์ผ๊ธฐํ๋ค.
Socket Programming
- The fundamental technology behind communications on TCP/IP networks.
- Socket provides a bidirectional communication endpoint for sending and receiving data with another socket.
- Socket connections normally run between two different computers on a local area network (LAN) or across the internet, but they can also be used for inter-process communication on a single computer.
Client and Server Architecture
- Socket Server
- Any program that listens for other programs to connect to it.
- ์๋ฒ๋ ๋จผ์ ์ด์๋์ listen
- Example: Web Server, Email Server, etc.
- Socket Client
- Any program that is designed to connect to another program.
- Example, Web Browser, Email app on your phone,
most all Internet games on mobile devices, etc.
Berkeley Socket API
- Application Programming Interface (API) for Internet sockets and Unix domain sockets,
used for inter-process communication (IPC). - It is commonly implemented as a library of linkable modules, and originated with the 4.2 BSD Unix operating system, released in 1983.
- Berkeley sockets evolved with little modification from a de facto standard into a component of the POSIX specification.
Berkeley Socket API Functions
- socket()
- creates a new socket of a certain type, identified by an integer number, and allocates system resources to it.
- ์ด์์ฒด์ ์๊ฒ ๋ฆฌ์์ค ์ค๋นํ๋ผ๊ณ ์๋ ค์ฃผ๋ ๊ธฐ๋ฅ
- socket() -> OS, ๋ฒํผ๋ฅผ ๋ง๋ค๊ณ , ์ค๋น ๋ฑ๋ฑ + ์ํฉ์ฒดํฌ, ํ์ด๋จธ
- UDP๋ ๊ฐ๋จํ๊ฒ, ๊ฐ๋ณ๊ฒ ์ค๋น
- bind()
- typically used on the server side, and associates a socket with a socket address structure,
- i.e. a specified local IP address and a port number.
- bind() -> ๋ถ์ด๋ ๊ฒ
๋๋ ์ด ํฌํธ ๋ฒํธ๋ฅผ ์ฌ์ฉํ ๊ฑฐ์ผ ์ ์ธ
ํด๋ผ์ด์ธํธ - ๋์๊ฒ ํฌํธ ๋ฒํธ๋ฅผ ์ค
์ค๋น๋ ์์ผ ์์, ์ ์ฐฉ์ ๋ก ์ดํ๋ฆฌ์ผ์ด์ ์ ๋ถ์ด๋ ๊ฒ
์์ดํผ๋ ํฌํธ ๋ฑ ์ค์ํ ์ ๋ณด๋ฅผ ์ค๋ค.
- listen()
- used on the server side, and causes a bound TCP socket to enter listening state.
- ์์ฒญ์ ๋ค์ ์ค๋น๋ฅผ ํ๋ ๊ฒ
- connect()
- used on the client side, and assigns a free local port number to a socket.
- In case of a TCP socket, it causes an attempt to establish a new TCP connection.
- SYN ๋นํธ 1์ด ๋จ
- TCP๋ ์ฐ๊ฒฐ ์์ฒญ ๊ณผ์ ์์ผ๋,
UDP ๊ฐ์ ๊ฒฝ์ฐ์๋ ์ปค๋ฅํธ ์์
- accept()
- used on the server side.
- It accepts a received incoming attempt to create a new TCP connection from the remote client, and creates a new socket associated with the socket address pair of this connection.
- SYN์ ๋ํ ACK
๋ณธ์ธ์ SYN๋ ๋๊ฐ
์ฐ๊ฒฐ ์์ฒญ์ ํ๋ฝํ๊ฒ ๋ค.
- send(), recv(), sendto(), and recvfrom()
- used for sending and receiving data.
- The standard functions write() and read() may also be used.
- write / read - ํต์ ๊ณผ ํ์ผ ์ฒ๋ฆฌ ๋ฑ๋ฑ
- ์ ๋์ค์์๋ ์ด๋ฅผ ๋ชจ๋ ๋์ผํ๊ฒ ์ทจ๊ธํ๋ค
ํ์ผ์ ์ฐ๋ค๋ ๊ฐ๋ ์ผ๋ก ์ง์ง ํ์ผ๋ ์ด๊ณ , ๋คํธ์ํฌ๋ ์ด๊ณ , ๋ฉ๋ชจ๋ฆฌ๋ ์ด๊ณ ๋ฑ๋ฑ
- close()
- causes the system to release resources allocated to a socket.
- In case of TCP, the connection is terminated.
- gethostbyname() and gethostbyaddr()
- used to resolve host names and addresses. IPv4 only.
- ๋๋ฉ์ธ - ์์ดํผ ์ฃผ์
๋๋ฉ์ธ ๋ค์ ์๋ฒ์ ๊ฐ๋ค์ค๊ฑฐ๋
์บ์ฌ ๊ฐ์ ํตํด , ๋ ์ฌ์ด๋ฅผ ๋ฒ์ญ
- select()
- used to suspend, waiting for one or more of a provided list of sockets to be ready to read, ready to write, or that have errors.
- ์๋ฒ ์ ์ฅ์์ ์ด๋ค ํด๋ผ์ด์ธํธ ์์ผ์ ์ฒ๋ฆฌํ ๊ฒ์ธ์ง ์ ํ
- poll()
- used to check on the state of a socket in a set of sockets.
- The set can be tested to see if any socket can be written to, read from or if an error occurred.
- ์ฝ๊ธฐ ์ ์ ์ ๋ณด๊ฐ ์๋์ง, ์ํ๋ฅผ ์ฒดํฌํ๋ ๊ณผ์
์จ๋ ๋๋ ๊ฑด์ง -> ๋ ์ด์ด ๋ด๋ฆด ๋ ํฐ์ง ์ ์์ผ๋๊น
- getsockopt()
- used to retrieve the current value of a particular socket option for the specified socket.
- ์ถ๊ฐ ์ต์ ์ธํ ๊ณผ์
- setsockopt()
- used to set a particular socket option for the specified socket.
Berkeley Socket API Procedures for TCP
- y์ถ - ์๊ฐ
์๋ฒ๊ฐ ๋จผ์ ์ด์๋๋ค.
socket ํธ์ถ : ์ค๋น์ํด
bind : ์ดํ์ด ์์ผ์ ์ฌ์ฉํ์ฌ,
์์ดํผ ํฌํธ ๋๋ฒ ๋ฑ๋ฑ ์ปจํผ๊ทธ ๋ง์นจ
listen ์ํ (์ฐ๊ฒฐ์ค์ ์๋ฃ)
- ํด๋ผ์ด์ธํธ - connect
accept์ ์ํด์ ๋์ ํต์ ๊ด๊ณ์ ๋ค์ด๊ฐ
๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ๋ ๊ด๋ก
- send + recv : ๋ณ๋ ฌ์ ์ผ๋ก ์ผ์ด๋จ
๋ ์ค ํ๋๊ฐ ๋๊ธฐ๋ฅผ ๋ฐ๋ผ๋ฉด
ํด๋ผ์ด์ธํธ๊ฐ close - ๋๊ธฐ๋ฅผ ๋ฐ๋
: TCP Session์ด ๋๋ ๋ฟ, ์๋ฒ๋ ๋ฉ์ฉกํ ์ด์์๋ค.
- UDP ๋ผ๋ฉด ์ฐ๊ฒฐ ํด์ ๊ณผ์ X
๊ทธ๋ฅ ๋ณด๋ผ ๊ฒ๋ค ๋ณด๋ด๊ณ , ๋๊ฐ๋ฉด ๋จ
- ํจ์๋ค์ด ์์ฐจ์ ์ผ๋ก ํธ์ถ๋๋ค.
์ด์์ฒด์ , ์ธ์ด๊ฐ ๋ค๋ฅด๋๋ผ๋ ๋๋์์ด
TCP UDP sample program ๋์ถฉ ๋ค ๋น์ทํจ
TCP Echo Server
- ์๋ฌ ์ฒ๋ฆฌ ์ ์ฉํ ์ฝ๋
- ์ด๋ ๊ฒ ํต์ ํ๋ก๊ทธ๋จ ์ง์ผ ํ๋ค. (๊ฐํ ์ฝ๋)
- ์๋๋ฐฉ๊ณผ ๋์ ๊ด๊ณ + ๋คํธ์ํฌ์ ์ํฉ + OS ์ํฉ
-> ์๋ฌด๊ฒ๋ ๋ณด์ฅํ ์ ์์ผ๋๊น
- try except -1
- ์์ ์ ์ผ๋ก ๋ณ๊ฒฝํ ์ฝ๋
- ํต์ ํ๋ก๊ทธ๋จ์ ์งค ๋
- if -> ๋ฌด์กฐ๊ฑด else ๋ฅผ ์ - ๋ญ๋ผ๋ ํ ์ค ์
- ๋ถ์ํ ํต์ ํจ์ -> ๋ค try except๋ก ๊ฐ์ผ๋ค
- ์๋ฒ ํ๋ก๊ทธ๋จ์ ์์ ์ ์ผ๋ก ์ง๋ ๋ฐฉ์
'ComputerScience > FullStackProgramming' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ์คํ] Application Layer messaging protocol (0) | 2023.04.17 |
---|---|
[ํ์คํ] TCP ๋น๋๊ธฐ์ socket server programming (1) | 2023.04.16 |
[ํ์คํ] TCP - connection-oriented protocol (1) | 2023.04.12 |
[ํ์คํ] UDP - User Datagram Protocol (0) | 2023.04.11 |
[ํ์คํ] Transport Layer 4 / Process-to-Process Delivery (0) | 2023.04.11 |