Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.

Slides:



Advertisements
Similar presentations
Echo server The client reads a line of text from its standard input and writes the line to the server The server reads the line from its network input.
Advertisements

I/O Multiplexing: select and poll
Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
Lecture 6 TCP Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
Socket Programming Application Programming Interface.
CSCE 515: Computer Network Programming TCP Details Wenyuan Xu Department of Computer Science and Engineering.
UNIX Process Control Bach 7 Operating Systems Course Hebrew University Spring 2007.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
1 TCP Sockets Computer Network Programming. 2 TCP Echo Server We will write a simple echo server and client –client read a line of text from standard.
Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back.
Sockets Programming Introduction © Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid.
CS Lecture 17 Outline Named pipes Signals Lecture 17
TDC561 Network Programming Camelia Zlatea, PhD Week 2 – part II: Socket Application Programming Interface.
I/O Multiplexing Capability of tell the kernel that wants to be notified when one or more I/O conditions are ready. For example, I/O data is available.
1) The server should be concurrent. This implies that it should loop infinitely, listening for clients requests. It should NOT terminate after accepting.
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
CS Lecture 16 Outline Inter-process Communication (IPC) – Pipes – Signals Lecture 161CS Operating Systems 1.
Threads© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer Science Department.
Protocol Programs that communicate across a network must agree on a protocol on how they will communicate High-level decisions must be made on which program.
Chapter 5. TCP Client-Server Example. Contents –Introduction –TCP Echo Server –TCP Echo Client –Normal Startup and Termination –Posix Signal Handling.
Elementary UDP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
1 TCP Client-Server Example TCP echo server: main and str_echo TCP echo client: main and str_cli Normal startup and termination POSIX signal handling Handling.
Chapter 5 TCP Client/Server Example. TCP Client-Server Example TCP echo server: main and str_echo TCP echo client: main and str_cli Normal startup and.
Elementary TCP Sockets
UNIX Network Programming1 UNIX Network Programming 2nd Edition.
Chapter 8 Elementary UDP Socket. Contents u recvfrom and sendto Function u UDP Echo Server( main, de_echo Function) u UDP Echo Client( main, de_cli Function)
Sirak Kaewjamnong Computer Network Systems
Elementary TCP Sockets
Programming with TCP – III 1. Zombie Processes 2. Cleaning Zombie Processes 3. Concurrent Servers Using Threads  pthread Library Functions 4. TCP Socket.
Concurrency. Readings r Tanenbaum and van Steen: r Coulouris: Chapter 6 r cs402 web page links r UNIX Network Programming by W. Richard Stevens.
Elementary TCP Sockets –The presentation will provide sufficient information to build a COMPLETE TCP client and server. –In addition the topic of concurrency.
TELE 402 Lecture 3: Elementary … 1 Overview Last Lecture –TCP/UDP and Sockets introduction This Lecture –Elementary TCP sockets –Source: Chapter 4 of Stevens’
Ports Port - A 16-bit number that identifies the application process that receives an incoming message. Reserved ports or well-known ports (0 to 1023)
TELE 402 Lecture 4: I/O multi … 1 Overview Last Lecture –TCP socket and Client-Server example –Source: Chapters 4&5 of Stevens’ book This Lecture –I/O.
Chapter 14 Unix domain protocol. contents Introduction unix domain socket address structure socketpair socket function unix domain stream client-server.
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
CSCE 515: Computer Network Programming Select Wenyuan Xu Department of Computer Science and Engineering.
CSCE 515: Computer Network Programming UDP Socket Wenyuan Xu Department of Computer Science and Engineering.
Threads and Locking Ioctl operations. Threads Lightweight processes What’s wrong with processes? –fork() is expensive – 10 to 100 times slower –Inter.
Operating Systems Process Creation
UNIX Signals * POSIX-Defined Signals * Signaling Processes * Signal Mask * sigaction * kill and sigaction * alarm * Interval Timers * POSIX.1b Timers *
Introduction A Simple Daytime Client A Simple Daytime Server
Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.
TCP Client-Server Example
Concurrent Servers. Idea Behind Concurrent Servers Server Client 1 Server 1 X.
Fork(), Concurrent Server, Normal Termination ( 금 ) 김 희 준
S -1 Processes. S -2 wait and waitpid (11.2) Recall from a previous slide: pid_t wait( int *status ) wait() can: (a) block; (b) return with status; (c)
1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level connection.Establishing.
Lecture 3 TCP and UDP Sockets CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
ECE 297 Concurrent Servers Process, fork & threads ECE 297.
Netprog: TCP Sockets1 TCP Sockets Programming Creating a passive mode (server) socket.Creating a passive mode (server) socket. Establishing an application-level.
I/O Multiplexing.
Chapter 5. TCP Client-Server Example
Read: Chapters 1,2, 3, 4 Communications Client Server Ex: TCP/IP
Elementary UDP Sockets
Threads Threads.
CH5 TCP Client - Server Example:
UNIX PROCESSES.
CSCD433 Advanced Networks Spring 2016 Lecture 16a
Chapter 8 Elementary UDP Socket
Recitation 11 – 4/29/01 Outline Sockets Interface
UNIX Domain sockets The Linux Programming Interface (ch 57)
SCTP Client / Server example
UDP Sockets Programming
TCP Sockets Programming
Advanced Network Programming spring 2007
TCP/IP Socket Programming in C
Elementary UDP Sockets connectionless, unreliable, datagram
TCP Client-Server Example
Presentation transcript:

Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui

Outline Introduction TCP Echo Server TCP Echo Client Normal Startup Normal Termination POSIX Signal Handling Handling SIGCHLD Signals

Simple Echo Client / Server fgets writen read TCP Client TCP Server stdin stdout fputs readline writen 1. Client: - Reads a line of text - Writes the line to the server 2. Server: - Reads the line from its network input - Echoes the line back to the client 3. Client: - Reads the echoed line - Prints the line on its standard output

TCP Echo Server: main Function 1 #include "unp.h" 2 int 3 main(int argc, char ** argv) 4 { 5 int listenfd, connfd; 6 pid_t childpid; 7 socklen_t clilen; 8 struct sockaddr_in cliaddr, servaddr; 9 listenfd = Socket (AF_INET, SOCK_STREAM, 0); 10 bzero(&servaddr, sizeof(servaddr)); 11 servaddr.sin_family = AF_INET; 12 servaddr.sin_addr.s_addr = htonl (INADDR_ANY); 13 servaddr.sin_port = htons (SERV_PORT); //9877 14 Bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); // Assign local protocol address to a socket 15 Listen(listenfd, LISTENQ); 16 for ( ; ; ) { 17 clilen = sizeof(cliaddr); connfd = Accept(listenfd, (SA *) &cliaddr, &clilen); 19 if ( (childpid = Fork()) == 0) { /* child process */ 20 Close(listenfd); /* close listening socket */ 21 str_echo(connfd); /* process the request */ 22 exit (0); 23 } 24 Close(connfd); /* parent closes connected socket */ 25 } fork spawns child child handles new client child closes listening socket parent closes connected socket

TCP Echo Server: str_echo Function 1 #include "unp.h“ 2 void 3 str_echo (int argc, char ** argv) 4 { 5 ssize_t n; 6 char buf[MAXLINE]; 7 again: 8 while ((n = read(sockfd, buf, MAXLINE)) > 0) 9 Writen (sockfd, buf, n); 10 If (n < 0 && errno == EINTR) 11 goto again; else if (n < 0) err_sys (“str_echo: read error”); 14 } Function: Reads data from the client & echoes it back to the client. If client FIN is received, a) child’s read return 0. b) str_echo function returns. c) child is terminated.

TCP Echo Client: main Function 1 #include "unp.h" 2 int 3 main(int argc, char **argv) 4 { 5 int sockfd; struct sockaddr_in servaddr; 7 if (argc != 2) err_quit("usage: tcpcli <IPaddress>"); sockfd = Socket(AF_INET, SOCK_STREAM, 0); 10 bzero(&servaddr, sizeof(servaddr)); 11 servaddr.sin_family = AF_INET; 12 servaddr.sin_port = htons(SERV_PORT); Inet_pton(AF_INET, argv[1], &servaddr.sin_addr); Connect(sockfd, (SA *) &servaddr, sizeof(servaddr)); 15 str_cli(stdin, sockfd); /* do it all */ 16 exit(0); 17 }

TCP Echo Client: str_cli Function 1 #include "unp.h" 2 void 3 str_cli (FILE *fp, int sockfd) 4 { char sendline[MAXLINE], recvline[MAXLINE]; while (Fgets(sendline, MAXLINE, fp) != NULL) { 7 Writen (sockfd, sendline, strlen (sendline); if (Readline(sockfd, sendline, strlen (sendline)) err_quit (“str_cli: server terminated prematurely”); 10 Fputs (recvline, stdout); 11 } 12} fgets: read a line of text. writen: sends the line to the server. readline: reads the line echoed back from the server. fputs: writes it to standard output. When EOF/error is encountered, a) fgets returns a null pointer b) loop terminates

Normal Startup (1) Client calls socket and connect. Start the server in the background of linux host. When server starts, it calls socket, bind, listen, and accept, blocking in the call to accept. Run the netstat program to verify the state of the server’s listening socket. Client calls socket and connect. When three-way handshake completes, connect returns in the client and accept returns in the server.

Normal Startup (2) After connection is established, a) Client calls str_cli, block in the call to fgets (wait for input). 1 #include "unp.h" 2 void 3 str_cli (FILE *fp, int sockfd) 4 { char sendline[MAXLINE], recvline[MAXLINE]; while (Fgets(sendline, MAXLINE, fp) != NULL) { 7 Writen (sockfd, sendline, strlen (sendline); if (Readline(sockfd, sendline, strlen (sendline)) err_quit (“str_cli: server terminated prematurely”); 10 Fputs (recvline, stdout); 11 }

Normal Startup (3) After connection is established, b) When accept returns in the server, it calls fork and the child calls str_echo. This function read, which blocks while waiting for a line to be sent from the client. 1 #include "unp.h“ 2 void 3 str_echo (int argc, char ** argv) 4 { 5 ssize_t n; 6 char buf[MAXLINE]; 7 again: 8 while ((n = read(sockfd, buf, MAXLINE)) > 0) 9 Writen (sockfd, buf, n); 10 If (n < 0 && errno == EINTR) 11 goto again; else if (n < 0) err_sys (“str_echo: read error”); 14 }

Normal Startup (4) After connection is established, 1 #include "unp.h" 2 int 3 main(int argc, char ** argv) 4 { 5 int listenfd, connfd; 6 pid_t childpid; : 16 for ( ; ; ) { 17 clilen = sizeof(cliaddr); connfd = Accept(listenfd, (SA *) &cliaddr, &clilen); 19 if ( (childpid = Fork()) == 0) { /* child process */ 20 Close(listenfd); /* close listening socket */ 21 str_echo(connfd); /* process the request */ 22 exit (0); 23 } 24 Close(connfd); /* parent closes connected socket */ 25 } After connection is established, c) Again, the server parent calls accept again, and blocks while waiting for the next client connection.

Normal Startup (5) When the three-way handshake completes, connect returns when the second segment of the handshake is received by the client accept does not return until the third segment of the handshake is received by the server.

Normal Termination (1)

Normal Termination (2) Normal termination of client and server: Type in EOF character, fgets returns null pointer and the function str_cli returns. When str_cli returns to the client main function, the latter terminates by calling exit. - Kernel closes client socket. - This sends a FIN to the server. - The server TCP responds with an ACK. d) - TCP receives the FIN. - The server child is blocked in a call to readline and readline then returns 0. - str_echo function return to the server child main.

Normal Termination (3) e) The server child terminates by calling exit. f) - All open descriptors in the server child are closed. - The closing of the connected socket by the child causes : i) a FIN from the server to the client ii) an ACK from the client g) Finally, the SIGCHLD signal is sent to the parent when the server child terminates. If ignored, the child enters the zombie state and remains forever. Zombie state: When a child process exits, it is not immediately cleared off the process table. Instead, a signal is sent to its parent process, which needs to acknowledge it's child's death, and only then the child process is completely removed from the system. In the duration before the parent's acknowledgment and after the child's exit, the child process is in a state called "zombie".

POSIX Signal Handling (1) Signal : a notification to a process that an event has occurred. : known as software interrupts. : usually occur asynchronously. Signals can be sent by: one process to another the kernel to a process Disposition : the action associated with the signal : set by sigaction function 3 choices for the disposition: - provide a function (signal handler) that is called whenever a specific signal occurs. Function Prototype: void handler (int signo); - ignore a signal by setting its disposition to SIG_IGN. - set the default disposition for a signal by setting its disposition to SIG_DFL

POSIX Signal Handling (2) The POSIX way to establish the disposition of a signal is to call the sigaction function. An easier way to set the disposition of a signal is to call the signal function.

POSIX Signal Handling (3) Set handler: - The sa_handler member of the sigaction structure is set to the func argument. Set signal mask for handler: When our signal handler is called, a set of signals will be blocked (cannot be delivered to a process). the sa_mask member is set to the empty set so that no additional signals will be blocked while our signal handler is running. Set SA_RESTART flag: - Optional flag - When SA_RESTART flag is set, a system call interrupted by this signal will be automatically restarted by the kernel.

POSIX Signal Handling (4) POSIX Signal Semantics: Signal handling on a POSIX-compliant system: Once a signal handler is installed, it remains installed. While a signal handler is executing, the signal being delivered is blocked. Furthermore, any additional signals that were specified in the sa_mask signal set passed to sigaction when the handler was installed are also blocked. If a signal is generated one or more times while it is blocked, it is normally delivered only one time after the signal is unblocked. It is possible to selectively block and unblock a set of signals using the sigprocmask function in order to protect a critical region of code by preventing certain signals from being caught while that region of code is executing.

Handling sigchld signals (1) The purpose of the zombie state: - to maintain information about the child for the parent to fetch at some later time. - This information includes the process ID of the child, its termination status, and information on the resource utilization of the child (CPU time, memory, etc.). If a process terminates, and that process has children in the zombie state, the parent process ID of all the zombie children is set to 1 (the init process), which will inherit the children and clean them up.

Handling sigchld signals (2)

Handling sigchld signals (3) The sequence of steps is as follows: We terminate the client by typing our EOF character. The client TCP sends a FIN to the server and the server responds with an ACK. The receipt of the FIN delivers an EOF to the child's pending readline. The child terminates. The parent is blocked in its call to accept when the SIGCHLD signal is delivered. The sig_chld function executes (our signal handler), wait fetches the child's PID and termination status, and printf is called from the signal handler. The signal handler returns. Since the signal was caught by the parent while the parent was blocked in a slow system call (accept), the kernel causes the accept to return an error of EINTR (interrupted system call). The parent does not handle this error so it aborts.

To be continued… Thank You!