Académique Documents
Professionnel Documents
Culture Documents
III-Unit
Contents
Introduction TCP Echo Server TCP Echo Client Normal Startup and Termination Posix Signal Handling Handling SIGCHLD Signals Data Format and so on...
5.1 Introductioon
stdin stdout
fgets writen readline readline writen
fputs
TCP client
TCP server
1. The Client reads a line of text from its standard input and writes the line to the server. 2. The server reads the line from its network input and echoes the line back to the client. 3. The client reads the echoed line and prints it on its standard output.
char
line[MAXLINE];
for ( ; ; ) { if ( (n = Readline(sockfd, line, MAXLINE)) == 0) return; /* connection closed by other end */ Writen(sockfd, line, n); } }
while (Fgets(sendline, MAXLINE, fp) != NULL) { Writen(sockfd, sendline, strlen(sendline)); if (Readline(sockfd, recvline, MAXLINE) == 0) err_quit("str_cli: server terminated prematurely");
Fputs(recvline, stdout);
} }
Normal Startup
How the client and server start, how they end What happens when something goes wrong
the client host crashes, the client process crashes, network connectivity is lost
Test method We first start the server in the background on the host linux
tcpserv & When the server starts, it calls socket, bind,listen and accept .
Before starting the client , we run the netstat program to verify the state of the servers listening sockets netstat a tcpcli 127.0.0.1 (local test) netstat a ps -l
Normal Termination
At this point, the connection is established and whatever we type to the client is echoed back. tcpcli 127.0.0.1
hello, world hello, world good bye good bye ^D We type Control-D,which terminates the client., if we immediately execute netstat, we have netstat -a | grep procID
Problem: The client should be aware of server process crash when FIN is received.
SA_RESTART
a system call interrupted by this signal will be automatically restarted by the kernel
Handling Zombies
space waste of the kernel, out of process wait for the children to prevent them from becoming zombies
pit_t: the process ID of the terminated child statloc : the termination status of the child(an integer) is returned through the statloc pointer. pid : specify the process ID that we want to wait for. A value of -1 say to wait for the first of our children to terminate. option : specify additional option. The most common option is WNOHANG.
waitpid
we must specify the WNOHANG option: this tells waitpid not to block if there exist running children that have not yet terminated.
void sig_chld(int signo) { pid_t pid; int stat; while((pid = waitpid(-1,&stat,WNOHANG)) > 0) printf("child %d terminated\n", pid); return; }
Implementation BSD : kernel SVR4 : return an errno of EPROTO Posix.1g : return an errno of ECONNABORTED EPROTO : returned when some fatal protocol-related events occur on the streams subsystem. In the case of the ECONNABORTED error, the server can ignore the error and just call accept again.
Our client is not expecting to receive an end-offile at this point so it quits with the error message server terminated prematurely.
SIGPIPE Signal
What happens if the client the error return from readline and writes more data to the server ?
When a process writes to a socket that has received an RST, the SIGPIPE signal is sent to the process
Default action of SIGPIPE: terminate the process
Nothing is echoed for bye data Reason: the default action of SIGPIPE is terminate the process.