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.

Slides:



Advertisements
Similar presentations
Network Programming Week #1 J.P. Yoo
Advertisements

Socket Programming Computer Networks, Spring 2008 Your TAs.
Advanced Piloting Cruise Plot.
UDP Sockets UDP: unreliable delivery, no connection DNS, NFS, SNMP.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Chapter 1 The Study of Body Function Image PowerPoint
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Title Subtitle.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Year 6 mental test 10 second questions
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
Socket Programming 101 Vivek Ramachandran.
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Socket Programming CS3320 Fall 2010.
ABC Technology Project
2 |SharePoint Saturday New York City
IP Multicast Information management 2 Groep T Leuven – Information department 2/14 Agenda •Why IP Multicast ? •Multicast fundamentals •Intradomain.
VOORBLAD.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
© 2012 National Heart Foundation of Australia. Slide 2.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
25 seconds left…...
Januar MDMDFSSMDMDFSSS
Week 1.
Analyzing Genes and Genomes
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Intracellular Compartments and Transport
PSSA Preparation.
Essential Cell Biology
CS 4700 / CS 5700 Network Fundamentals
Computer Net Lab/Praktikum Datenverarbeitung 2 1 Overview Sockets Sockets in C Sockets in Delphi.
TCP/IP Protocol Suite 1 Chapter 18 Upon completion you will be able to: Remote Login: Telnet Understand how TELNET works Understand the role of NVT in.
MERANCANG Program Transfer File (PTF) PTFServer dan PTFClient I Made Astawa
Ipv4 Socket Address Structure struct in_addr { in_addr_t s_addr; /* 32-bit IPv4 address */ /* network byte ordered */ }; struct sockaddr_in { uint8_t sin_len;
CSE 333 – SECTION 8 Networking and sockets. Overview Network Sockets IP addresses and IP address structures in C/C++ DNS – Resolving DNS names Demos.
Programming with UDP – I Covered Subjects: IPv4 Socket Address Structure Byte Ordering Functions Address Access/Conversion Functions Functions: 1.socket()
CS252: Systems Programming Ninghui Li Based on slides by Prof. Gustavo Rodriguez-Rivera Topic 16: Socket Programming & Project 5.
Elementary TCP Sockets© Dr. Ayman Abdel-Hamid, CS4254 Spring CS4254 Computer Network Architecture and Programming Dr. Ayman A. Abdel-Hamid Computer.
1 Elementary TCP Sockets socket function connect function bind function listen function accept function fork and exec functions Concurrent servers close.
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.
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)
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 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)
Elementary TCP Sockets
Socket Programming Lec 2 Rishi Kant. Review of Socket programming Decide which type of socket – stream or datagram. Based on type create socket using.
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.
Chapter 5. TCP Client-Server Example
Elementary UDP Sockets
Chapter4 Elementary TCP Socket
UNIX Domain sockets The Linux Programming Interface (ch 57)
Chapter 5 (part 1) TCP Client /Server Example By: Lim Meng Hui.
TCP Sockets Programming
Elementary UDP Sockets connectionless, unreliable, datagram
TCP Client-Server Example
Presentation transcript:

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 and echoes the line back to the client

Echo server 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); 14 Bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); 15 Listen(listenfd, LISTENQ); 16 for ( ; ; ) { 17 clilen = sizeof(cliaddr); 18 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 } 26 }

Echo server 1 #include "unp.h" 2 void 3 str_echo(int sockfd) 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; 12 else if (n < 0) 13 err_sys("str_echo: read error"); 14 }

Echo client 1 #include "unp.h" 2 int 3 main(int argc, char **argv) 4 { 5 int sockfd; 6 struct sockaddr_in servaddr; 7 if (argc != 2) 8 err_quit("usage: tcpcli "); 9 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); 13 Inet_pton(AF_INET, argv[1], &servaddr.sin_addr); 14 Connect(sockfd, (SA *) &servaddr, sizeof(servaddr)); 15 str_cli(stdin, sockfd); /* do it all */ 16 exit(0); 17 }

Echo client 1 #include "unp.h" 2 void 3 str_cli(FILE *fp, int sockfd) 4 { 5 char sendline[MAXLINE], recvline[MAXLINE]; 6 while (Fgets(sendline, MAXLINE, fp) != NULL) { 7 Writen(sockfd, sendline, strlen (sendline)); 8 if (Readline(sockfd, recvline, MAXLINE) == 0) 9 rr_quit("str_cli: server terminated prematurely"); 10 Fputs(recvline, stdout); 11 } 12 }

Netstat linux % tcpserv01 & [1] linux % netstat -a Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 *:9877 *:* LISTEN linux % tcpcli linux % netstat -a Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 local host:9877 localhost:42758 ESTABLISHED tcp 0 0 local host:42758 localhost:9877 ESTABLISHED tcp 0 0 *:9877 *:* LISTEN linux % ps -t pts/6 -o pid,ppid,tty,stat,args,wchan PID PPID TT STAT COMMAND WCHAN pts/6 S -bash wait pts/6 S./tcpserv01 wait_for_connect pts/6 S./tcpserv01 tcp_data_wait pts/6 S./tcpcli read_chan

Netstat (termination) linux % netstat -a | grep 9877 tcp 0 0 *:9877 *:* LISTEN tcp 0 0 localhost:42758 localhost:9877 TIME_WAIT linux % ps -t pts/6 -o pid,ppid,tty,stat,args,wchan PID PPID TT STAT COMMAND WCHAN pts/6 S -bash read_chan pts/6 S./tcpserv01 wait_for_connect pts/6 Z [tcpserv01 <defu do_exit

Normal termination 1. When EOF is typed fgets returns the Ao digitar EOF, fget returns a null pointer and str_cli returns 2. The client exits 3. Clients descriptors are closed and a FYN segment is sent to the server 4. Server in state CLOSE_WAIT and client in state FIN_WAIT-2

Normal termination The server child was blocked at the readline command ( str_echo ) and it returns to main. Server child ends by executing exit All server child descriptors are closed A FIN segment is sent to the client Connection ended A SIGCHLD signal is sent

Signal Software interrupt Process to process or kernel to process SIGACTION specifies disposition (or action). Three options: Specify the handler: void handler (int signo) – a function that is called whenever a specific signal occurs Ignore the signal by setting its disposition to SIG_IGN Sets default SIG_DFL. The default is to terminate the process SIGNAL – two arguments: name of the signal and either a pointer to a function or a constant SIG_IGN or SIG_DFL

Signal 1 #include "unp.h" 2 Sigfunc * 3 signal (int signo, Sigfunc *func) 4 { 5 struct sigaction act, oact; 6 act.sa_handler = func; 7 sigemptyset (&act.sa_mask); 8 act.sa_flags = 0; 9 if (signo == SIGALRM) { 10 #ifdef SA_INTERRUPT 11 act.sa_flags |= SA_INTERRUPT; /* SunOS 4.x */ 12 #endif 13 } else { 14 #ifdef SA_RESTART 15 act.sa_flags |= SA_RESTART; /* SVR4, 4.4BSD */ 16 #endif 17 } 18 if (sigaction (signo, &act, &oact) < 0) 19 return (SIG_ERR); 20 return (oact.sa_handler); 21 }

Signal Once handler is installed, it remaisn installed When handler is executed, the signal being delivered is blocked. Any additional signal that were specifeid in the mask as_mask signal set passed by sigaction when handler was installed are also blocked. Signals are not queued – delivered once Selectively blocking by using sigprocmask

Signal Zombie state mantains information about child for the parent to fetch at later time. Information includes process ID of the child, its termination status and on resource utilization Zombieo occupies space and can run out of process space Must wait child terminate to avoid them to become zombies The handler mus be executed after a listen Signal (SIGCHLD, sig_chld)

Signal Wait and waitpid returns the process ID and the termination status Macros can be used to check this information Wait blocks the calling process and waitpid has the option to block or not #include pid_t wait (int *statloc); pid_t waitpid (pid_t pid, int *statloc, int options); Both return: process ID if OK, 0 or–1 on error

Signal Cuncurrent wait – does not enqueue zombie processes

Signal Waitpid waits for temination of any children 1 #include "unp.h" 2 void 3 sig_chld(int signo) 4 { 5 pid_t pid; 6 int stat; 7 while ( (pid = waitpid(-1, &stat, WNOHANG)) > 0) 8 printf("child %d terminated\n", pid); 9 return; 10 } Option WNOHANGdoes not block

Signal 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 void sig_chld(int); 10 listenfd = Socket (AF_INET, SOCK_STREAM, 0); 11 bzero (&servaddr, sizeof(servaddr)); 12 servaddr.sin_family = AF_INET; 13 servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 14 servaddr.sin_port = htons(SERV_PORT); 15 Bind(listenfd, (SA *) &servaddr, sizeof(servaddr)); 16 Listen(listenfd, LISTENQ); 17 Signal (SIGCHLD, sig_chld); /* must call waitpid() */ 18 for ( ; ; ) { 19 clilen = sizeof(cliaddr); 20 if ( (connfd = accept (listenfd, (SA *) &cliaddr, &clilen)) < 0) { 21 if (errno == EINTR) 22 continue; /* back to for() */ 23 else 24 err_sys("accept error"); 25 } 26 if ( (childpid = Fork()) == 0) { /* child process */ 27 Close(listenfd); /* close listening socket */ 28 str_echo(connfd); /* process the request */ 29 exit(0); 30 } 31 Close (connfd); /*parent closes connected socket*/ 32 } 33 }

Connection abortion Connection abortion before returning accept solution is SO dependent

SIGPIPE Process performs a write operation to a socket that received a RST Defaultis to terminate the process

Server crash After a default number of attempts, client receives either the error code EPTIMEDOUT or EHOSTUNREACH (in case a router sends na ICMP message) Option SO_KEEPALIVE to verify if server is alive

Server crash If server crashes and reboots before client send data, upon receiving data from client server send na RST Client returns error code ECONRESET

TCP Example