[ํ’€์Šคํƒ] 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 ๋Œ€์ถฉ ๋‹ค ๋น„์Šทํ•จ

 

 

 

https://www.keil.com/pack/doc/mw6/Network/html/using_network_sockets_bsd.html

 

 

 

TCP Echo Server 

  • ์—๋Ÿฌ ์ฒ˜๋ฆฌ ์ ์šฉํ•œ ์ฝ”๋“œ
  • ์ด๋ ‡๊ฒŒ ํ†ต์‹  ํ”„๋กœ๊ทธ๋žจ ์งœ์•ผ ํ•œ๋‹ค. (๊ฐ•ํ•œ ์ฝ”๋“œ)
  • ์ƒ๋Œ€๋ฐฉ๊ณผ ๋‚˜์˜ ๊ด€๊ณ„ + ๋„คํŠธ์›Œํฌ์˜ ์ƒํ™ฉ + OS ์ƒํ™ฉ
    -> ์•„๋ฌด๊ฒƒ๋„ ๋ณด์žฅํ•  ์ˆ˜ ์—†์œผ๋‹ˆ๊นŒ

 

  • try except -1
  • ์•ˆ์ •์ ์œผ๋กœ ๋ณ€๊ฒฝํ•œ ์ฝ”๋“œ 
  • ํ†ต์‹  ํ”„๋กœ๊ทธ๋žจ์„ ์งค ๋•Œ 
  • if -> ๋ฌด์กฐ๊ฑด else ๋ฅผ ์”€ - ๋ญ๋ผ๋„ ํ•œ ์ค„ ์”€
  • ๋ถˆ์•ˆํ•œ ํ†ต์‹  ํ•จ์ˆ˜ -> ๋‹ค try except๋กœ ๊ฐ์‹ผ๋‹ค
  • ์„œ๋ฒ„ ํ”„๋กœ๊ทธ๋žจ์„ ์•ˆ์ •์ ์œผ๋กœ ์งœ๋Š” ๋ฐฉ์‹