Presentation is loading. Please wait.

Presentation is loading. Please wait.

TCP Client-Server Example

Similar presentations


Presentation on theme: "TCP Client-Server Example"— Presentation transcript:

1 TCP Client-Server Example
Chap 1 Foundation TCP Client-Server Example Chap 5

2 TCP Echo Client tcpcliserv/tcpcli01.c: lib/str_cli.c: TCP Client
Server stdin stdout fgets fputs writen readline

3 TCP Echo Server tcpcliserv/tcpserv01.c: lib/str_echo.c: (Parent) TCP
Client Server (Child) writen readline (Parent) fork connect accept tcpcliserv/tcpserv01.c: lib/str_echo.c:

4 Problems in Echo Server
Normal Termination후에 zombie process로 남아 있음 Zombie state of process in UNIX To maintain information about the child for the parent fetch at some later time such as child process ID, termination status, and information on the resource utilization Solution When a child process is terminated, signal SIGCHLD is delivered to the parent Abnormal Termination 경우도 처리해야 Client host crashes Client process crashes Network connectivity is lost, and so on

5 Posix Signal Handling Signal(software interrupt) can be sent asynchronously by one process to another process(or to itself) by the kernel to a process Signal disposition (or actions) catch: call a signal handler ignore: SIG_IGN SIGKILL and SIGSTOP cannot be caught nor ignored default: SIG_DFL Normally, the process receiving a signal is terminated Ignored for SIGCHLD and SIGURG signal() vs Posix sigaction() Posix Signal Semantics Once a signal handler is installed, it remains installed Classical UNIX signal is not… While a signal handler is executing, the signal being delivered is blocked Signals are not queued(delivered only one time)  may lost

6 Handling Zombies using signal
When the parent process captures SIGCHLD signals, wait until termination status is delivered In main program of the server signal(SIGCHLD, sig_child); /* Signal handler */ void sig_chld(int signo) { pid_t pid; int stat; pid = wait(&stat); printf("child %d terminated\n", pid); return; }

7 Problems still remain Problem-1: Problem-2:
Parent가 slow system call(accept)에서 blocked 됐을 때 signal이 들어오면 accept는 EINTR error로 return하게 됨 Parent aborted ! Problem-2: 5개의 client가 거의 동시에 terminate 5개의 client가 동시에 SIGCHLD를 server의 child에게 보냄 하나의 SIGCHLD가 도착한 후 나머지는 저장(queueing)되지 않을 수 있음 여전히 zombie가 남아 있음

8 Correct Solution to Problem-1
Handling interrupted system calls

9 Correct Solution to Problem-2 using waitpid()
pid: PID that we want to wait -1 to wait for the first of children to terminate WNOHANG option: not to block if there are no terminated children

10 TCP Echo Server handling Zombie Problem

11 Handling Abnormal Termination: Connection aborted before call accept
due to the fact that client process killed or client host crashed ??? When accept is called, SVR4 returns error EPROTO Posix.1g returns error ECONNABORTED Berkeley-derived implementation handle completely within kernel Solution: call accept again See lib/wrapsock.c lib/Wrapsock.c:

12 Handling Abnormal Termination: Termination of Server Process
Case 1: client blocked for reading from socket Server: process killed  close socket send FIN Client: readline() returns 0  normal termination Case 2: client blocked for reading from stdin Client: User input  writen() Server: send RST Client: readline() returns error (ECONNRESET) Socket과 Stdin 두개의 descriptor가 ready될 조건을 알아내어 별개로 처리 가능해야  use I/O Multiplexing (select(), poll()) When a process is terminated or killed Calls close() TCP sends FIN When TCP receives a segment towards terminated process (i.e. invalid destination port #)  TCP responds RST

13 Abnormal Termination: Crashing or Shutdown of Server Host
Crashing of Server Host ETIMEOUT error return after retransmitting 12 times(9 min) or EHOSTUNREACH (or ENETUNREACH) error returns with ICMP message 데이터를 보내지 않고도 connection이 끊어졌음을 알 수는 없는가 ? 초 단위로 더 빨리 알아낼 방법은 없는가? Use SO_KEEPALIVE socket option Construct heartbeat mechanism using OOB data (TCP URG flag) Crashing and rebooting of server host loose connection information responds RST to the request form client  readlline() in client returns error ECONNRESET Shutdown of server host SIGKILL  socket closed  send FIN  same as in “Termination of Server Process”

14 Client’s and Server’s Perspective

15 Data Format Potential problems in exchanging data structure Solutions
different format of binary numbers: big endian, little endian different implementation of C data type: e.g) long, int, short different implementation packing and alignment of structures Solutions pass all the numeric data as text string define and use common exchange format - e.g) RPC TCP 상의 application protocol은 통상 PDU를 사용하지 않고 textual syntax를 사용함. Why?


Download ppt "TCP Client-Server Example"

Similar presentations


Ads by Google