> For the complete documentation index, see [llms.txt](https://toothlessdev.gitbook.io/main/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://toothlessdev.gitbook.io/main/computer-science/network-programming/ch07-tcp-half-close.md).

# Ch07 TCP 기반의 Half Close

## 일방적인 연결 종료의 문제점

`close()` 함수는 소켓의 완전한 소멸을 의미합니다\
소켓이 완전히 닫히므로 더 이상의 입출력은 불가능합니다

상대 호스트의 상태에 상관없이 일방적인 종료를 하게 되는데,\
이때문에 상대 호스트의 데이터 송수신이 아직 완료되지 않은 상황이라면, 문제가 발생 할 수 있습니다

## Half Close

Half Close 란 입력 또는 출력 스트림 중 하나만 종료시키는 방법입니다

```c
#include <sys/socket.h>

int shutdown(int sock, int howto);
// SHUT_RD : 입력 스트림 종료
// SHUT_WR : 출력 스트림 종료
// SHUT_RDWR : 입출력 스트림 종료
```

`shutdown()` 을 활용하면 입출력 스트림 중 하나를 종료시킬 수 있습니다\
출력스트림만 종료함으로써 EOF 가 전달되고, 상대 호스트에서의 `read()` `recv()` 는 0을 리턴하고,\
종료를 기다릴 수 있습니다
