tcp 소켓 프로그래밍 질문입니다.
TCP connection이 연결되어있는 서버-클라이언트가 있는데요..
클라이언트가 비정상적으로 종료되면
Tcp 서버가 connection이 끊어진것을(close)
모를수도 있나요? 인터넷에서 관련된 내용을 찾았는데요..
어떤 상황에서 발생할수 있는지 조언 좀 부탁 드릴께요
=======================================
2. How can I detect that the other end of a TCP connection has
crashed? Can I use "keepalives" for this?
Detecting crashed systems over TCP/IP is difficult. TCP doesn't
require any transmission over a connection if the application
isn't sending anything, and many of the media over which TCP/IP is
used (e.g. Ethernet) don't provide a reliable way to determine
whether a particular host is up.
If a server doesn't hear from a client,
it could be because it has nothing to say, some network
between the server and client may be down,
the server or client's network interface may be disconnected,
or the client may have crashed.
Network failures are often temporary
(a thin Ethernet will appear down while someone is adding a link to the daisy
chain, and it often takes a few minutes for new routes to stabilize when a router goes down)
and TCP connections shouldn't be dropped as a result.
Keepalives are a feature of the sockets API that requests that an
empty packet be sent periodically over an idle connection; this
should evoke an acknowledgement from the remote system if it is
still up, a reset if it has rebooted, and a timeout if it is down.
These are not normally sent until the connection has been idle for
a few hours. The purpose isn't to detect a crash immediately, but
to keep unnecessary resources from being allocated forever.
If more rapid detection of remote failures is required, this
should be implemented in the application protocol. There is no
standard mechanism for this, but an example is requiring clients
to send a "no-op" message every minute or two. An example protocol
that uses this is X Display Manager Control Protocol (XDMCP), part
of the X Window System, Version 11; the XDM server managing a
session periodically sends a Sync command to the display server,
which should evoke an application-level response, and resets the
session if it doesn't get a response (this is actually an example
of a poor implementation, as a timeout can occur if another client
"grabs" the server for too long).
한쪽이 정상적으로 종료하면 read하는 과정에 -1이 리턴되겠지만,랜
한쪽이 정상적으로 종료하면 read하는 과정에 -1이 리턴되겠지만,
랜선을 확 뽑아버리던가 하면 세션을 종료하는 과정에서 이뤄지는 3웨이 핸드쉐이킹이 이뤄질수 없기때문에 timeout이 될때 까지 쭈욱 멈춰있는것으로 알고 있습니다.
timeout이 되는 시간은 OS마다 또 설정한 값에 따라 다르다고 알고 있는데.. 대충 7~ 8분 정도는 가는듯.. 윈도우의 경우는 더 짧은것 같구..
그래서 링크가 제대로 연결되었는지를 확인해 볼때는 저같은경우 링크테스트 메시지를 주기적으로 보내서 응답이 오는지 확인하는 방법을 쓰고있습니다. 링크가 제대로 연결되었는지 뿐만 아니라 상대편 프로세스가 대충 동작이라도 하고있는지 알수 있는정보가 되니까요..
뭐 방법은 다양하겠죠...
네 모를수도 있습니다...
그래서 윗분 말씀대로
tcp server/client 환경에서는
꼭 주기적으로 connection 이 살아있나를 검사하는
heart beat signal 패킷을 주기적으로 주고 받아 임의로
설정한 시간안에 이 패킷이 안오면 접속이 종료된걸로 판단.
소켓을 close 해주어야 합니다.
댓글 달기