Home | | Network Programming and Management | TCP Echo Server : str_echo Function

Chapter: Network Programming and Management : Application Development

TCP Echo Server : str_echo Function

This is shown in the following figure. It shows the server processing for each client: reading the lines from the client and echoing them back to the client.

TCP Echo Server : str_echo Function

This is shown in the following figure. It shows the server processing for each client: reading the lines from the client and echoing them back to the client.

MAXLINE is specified as constant of 4096 characters.

Line 7-11: readline reads the next line from the socket and the line is echoed back to the client by writen If the client closes the connection, the recept of client‘s FIN causes the child‘s readline to return 0. This causes the str_echo function to return which terminates the child.

TCP Echo Client : Main Function :  Following code shows the TCP client  main function.

Line 9 – 13: A TCP socket is created and an Internal socket address structure is filled in with the server‘s IP address and port number. We take the server‘s IP address from the command line argument and the server‘s well known port (SERV_PORT) from the header.


Line 13: The function inet_pton () converts the argument received at the command line from presentation to numeric value and stores the same at the address specified as the third arguments.


Line 14 –1 5: Connection function establishes the connection with the server. The function str_cli () than handles the client processing.


TCP Echo Client : str _ cli function:

Above function handles the client processing loop. That is read a line of text from standard input, write it to the server, read back the server‘s echo of the line and output the echoed line to standard input.


Line 6-7 : fgets reads a line of text and writen     sends the line to the server.

Line 8 – 10: readline reads the line echoed back from the server and fputs writes it to the standard output.


Signals Introduction.


A signal is a message (an integer) sent to a process. The receiving process can try to ignore the signal or call a routine (a so-called signal handler). After returning from the signal handler, the receiving process will resume execution at the point at which it was interrupted. The system-calls to deal with signals vary between one Unix version and another


The following conditions can generate a signal(1):


         When user presses terminal keys, the terminal will generate a signal. For example, when the user breaks a program by CTRL + C key pair.


         Hardware exceptions can generate signals, too: Division by 0, invalid memory reference. Inexperienced programmers often get SIGSEGV (Segmentation Violation signal) because of an invalid address in a pointer.


         Processes can send signals to themselves by using kill(2) system call (If permissions allow).


         Kernel can generate signal to inform processes when something happens. For example, SIGPIPE will be generated when a process writes to a pipe which has been closed by the reader.


Signals can be sent using the kill() routine. The signal() and sigaction() routines are used to control how a process should respond to a signal.

Posix Signal Handling :


         Every signal has a disposition, which is called the action associated with the signal. The disposition is set by calling the sigaction function.


         We have three choices for the disposition:


a)   Whenever a specific signal occurs, a specific function can be provided. This function is called signal handler and the action is called catching the signal.


         The two signal SIGKILL and SIGSTOP cannot be caught – this is an exception.


         The function is called with a single integer argument that is the signal number and the function returns nothing as shown below:


const struct sigaction act;

sigaction (SIGCHLD, &act, NULL)


• Calling sigaction and specifying a function to be called when the signal occurs is all that is required to catch the signal.


o For few signal like SIGIO, SIGPOLL, and SIGURG etc additional actions on the part of the process is required to catch the signal.


b)   A signal a can be ignored by setting its disposition to SIG_IGN. Again the two signals SIGKILL and SIGSTOP are exceptions.


c)    We can set the default disposition for a signal by setting its disposition to SIG_DFL.The


default is normally to terminate a process on the receipt of a signal, with certain signal also generating a core image of the process in its current working directory. The signals whose default disposition is to be ignored are : SIGCHLD AND SIGURG(sent on arrival of out of band data.)



with appropriate settings in the sigactionstructure you can control the current process's response to receiving a SIGCHLDsignal. As well as setting a signal handler, other behaviours can be set. If


         act.sa_handleris SIG_DFLthen the default behaviour will be restored


         act.sa_handleris SIG_IGNthen the signal will be ignored if possible (SIGSTOP and SIGKILLcan't be ignored)


         act.sa_flagsis SA_NOCLDSTOP - SIGCHLDwon't be generated when children stop.


         act.sa_flagsis SA_NOCLDWAIT- child processes of the calling process will not be transformed into zombie processes when they terminate.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Network Programming and Management : Application Development : TCP Echo Server : str_echo Function |

Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.