Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro...
Transcript of Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro...
![Page 1: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/1.jpg)
Prof. Edmundo R. M. MadeiraCarlos R. Senna
MC823MC823Laboratório de TeleprocessamentoLaboratório de Teleprocessamento
e Redese Redes Primeiro Semestre 2009
![Page 2: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/2.jpg)
2Programa da DisciplinaA
pre
senta
ção
• Tecnologias de Comunicação:
1. Sockets
2. RMI
3. Web Services
![Page 3: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/3.jpg)
3Critério de AvaliaçãoA
pre
senta
ção
• Quatro projetos com relatórios de comparação (pesos iguais).
• Todos os projetos devem ter notas superiores ou iguais a 5. Se não, a média final é o menor valor entre 4,9 e a média dos quatro projetos.
![Page 4: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/4.jpg)
4Bibliografia
• Stevens, R. W. “Unix Network Programming – Networking APIs: Sockets and XTI” – Vol. 1, Second Edition, Prentice-Hall, 1998. • BIMECC 005.43St47u
• Tutoriais sobre RMI e Web Services.
Ap
rese
nta
ção
![Page 5: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/5.jpg)
5Tecnologias de Comunicação
Sockets
![Page 6: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/6.jpg)
6OutlineS
ock
ets
• Socket basics• TCP sockets• Socket details and options• Socket functions• Concurrent server• Example: Echo Server and Echo Client• Signals and Zumbies• I/O Multiplexing• UDP Sockets
![Page 7: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/7.jpg)
7BasicsS
ock
ets
Copyright ©2000 The McGraw Hill Companies Leon-Garcia & Widjaja: Communication Networks Fig. 2.20
![Page 8: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/8.jpg)
8BasicsS
ock
ets
• An end-point for an IP network connection• what the application layer “plugs into”• programmer cares about Application Programming Interface (API)
• End point determined by two things:• Host address: IP address is Network Layer• Port number: is Transport Layer
• Two end-points determine a connection: socket pair
• ex: 206.62.226.35,p21 + 198.69.10.2,p1500• ex: 206.62.226.35,p21 + 198.69.10.2,p1499
![Page 9: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/9.jpg)
9PortsS
ock
ets
• Numbers (vary in BSD, Solaris):
• 0-1023 “reserved”, must be root
• 1024 – 49151 (registered with IANA)
• 49152 – 65535 “ephemeral”
• /etc/services:
• ftp 21/tcp
• telnet 23/tcp
• finger 79/tcp
• snmp 161/udp
![Page 10: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/10.jpg)
10Sockets and the OSS
ock
ets
• User Socket Operating System (Transport Layer)
• User sees “descriptor”, integer index
• like: FILE *, or file index
• returned by socket() call (more later)
![Page 11: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/11.jpg)
11Transport LayerS
ock
ets
• UDP: User Datagram Protocol• no acknowledgements• no retransmissions• out of order, duplicate possible• connectionless
• TCP: Transmission Control Protocol• reliable (in order, all arrive, no
duplicates)• flow control• connection• duplex
![Page 12: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/12.jpg)
12Socket DetailsS
ock
ets
Unix Network Programming, W. Richard Stevens, Second Edition, 1998, Prentice Hall, BIMECC 005.43St47u
• Socket address structure (Chap. 3 )• TCP client-server (Chap. 4-5 )• UDP client server (Chap. 8 )• Misc stuff (Chap. 7 )
• setsockopt(), getsockopt()
![Page 13: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/13.jpg)
13Socket Address Structure
#include <sys/types.h>#include <sys/socket.h>struct in_addr {
/* 32-bit IPv4 addresses */
in_addr_t s_addr; };struct sock_addr_in { unit8_t sin_len; /* length of structure */
sa_family_t sin_family; /* AF_INET */
in_port_t sin_port; /* TCP/UDP Port num */
struct in_addr sin_addr; /* IPv4 address */
char sin_zero[8]; /* unused */
}
Sock
ets
![Page 14: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/14.jpg)
14Tamanhos e LimitaçõesS
ock
ets • Tamanho máximo IPv4: 65535 bytes incluindo
cabeçalho• Tamanho máximo IPv6• IPv6: opção Jumbo Payload – 32 bits• MTU mínimo de um enlace para:
• IPv4: 68 bytes; IPv6: 576 bytes• Path MTU• Tamanho mínimo do buffer de remontagem – tamanho
mínimo do datagrama que qualquer implementação deve suportar:• IPv4: 576 bytes
![Page 15: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/15.jpg)
15TCP Client-ServerS
ock
ets
socket( )
bind( )
listen( )
accept( )
socket( )
connect( )
write( )
read( )
write( )
read( )
close( )
Blocks until conection
from client
Process request
read( )
close( )
TCP Server
TCP Client
Connection establishment
(TCP) three-way handshake
data (request)
data (reply)
End-of-file notification
well-known
port
![Page 16: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/16.jpg)
16socket FunctionS
ock
ets
• family is one of• AF_INET (IPv4), AF_INET6 (IPv6), AF_LOCAL (local Unix),• AF_ROUTE (access to routing tables), AF_KEY (new, for
encryption)
• type is one of• SOCK_STREAM (TCP), SOCK_DGRAM (UDP)• SOCK_RAW (for special IP packets, PING, etc. Must be root)
• protocol is 0 (used for some raw socket options)• upon success returns socket descriptor
• like file descriptor => -1 if failure
int socket(int family, int type, int protocol);Create a socket, giving access to transport layer service.
Example:if (( sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
err_sys (“socket call error”);
![Page 17: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/17.jpg)
17socket OptionsS
ock
ets • Many socket( ) options
• Set/lookup using• setsockopt(), getsockopt()
• Examples:• SO_LINGER• SO_RCVBUF, SO_SNDBUF (modify buffer sizes)• SO_RCVLOWAT, SO_SNDLOWAT• SO_RCVTIMEO, SO_SNDTIMEO (Timeouts)• TCP_KEEPALIVE (idle time before close (2 hours,
default))• TCP_MAXRT (set timeout value)• TCP_NODELAY (disable Nagle Algorithm)
• See man pages for details• man socket on any Unix machine
![Page 18: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/18.jpg)
18connect FunctionS
ock
ets
• sockfd is socket descriptor from socket()• servaddr is a pointer to a structure with:
• Server port number and IP address• must be specified (unlike bind())
• addrlen is length of structure• client doesn’t need bind()
• OS will pick ephemeral port• returns socket descriptor if ok, -1 on error
int connect( int sockfd, const struct sockaddr *servaddr,
socklen_t addrlen );Connect to server.
Example:if (connect (sockfd, (struct sockaddr *) &servaddr, sizeof
(servaddr)) != 0)err_sys(“connect call error”);
![Page 19: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/19.jpg)
19bind FunctionS
ock
ets
• sockfd is socket descriptor from socket()• myaddr is a pointer to address struct with:
• port number and IP address• addrlen is length of structure• returns 0 if ok, -1 on error
• EADDRINUSE (“Address already in use”)
int bind( int sockfd, const struct sockaddr *myaddr, socklen_t
addrlen);Assign a local protocol address (“name”) to a socket.
Example:if (bind (sd, (struct sockaddr *) &servaddr, sizeof (servaddr))
!= 0)errsys (“bind call error”);
![Page 20: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/20.jpg)
20listen FunctionS
ock
ets
• sockfd is socket descriptor from socket()• backlog is maximum number of incomplete connections
• historically 5• rarely above 15 on a even moderate web server!
• Sockets default to active (for client)
int listen(int sockfd, int backlog);Announce willingness to accept connections, give queue size,
change socket state for TCP server.
Example:if (listen (sd, 2) != 0)
errsys (“listen call error”);
![Page 21: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/21.jpg)
21listen FunctionS
ock
ets
![Page 22: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/22.jpg)
22accept FunctionS
ock
ets
• sockfd is socket descriptor from socket()• cliaddr and addrlen return protocol address from client• returns brand new descriptor, created by OS• if used with fork(), can create concurrent server (more later)
int accept( int sockfd, struct sockaddr cliaddr,
socklen_t *addrlen );Return next completed connection.
Example:sfd = accept (s, NULL, NULL);if (sfd == -1) err_sys (“accept error”);
![Page 23: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/23.jpg)
23close FunctionS
ock
ets
• sockfd is socket descriptor from socket()• closes socket for reading/writing
• returns (doesn’t block)• attempts to send any unsent data• -1 if error
int close(int sockfd);Close socket for use.
![Page 24: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/24.jpg)
24read and write FunctionsS
ock
ets • Pode ler/escrever menos do que necessita devido ao
tamanho restrito do buffer.• É necessário chamar a função novamente.• Somente o write é nonblocking
• Tamanho máximo IPv4: 65535 bytes incluindo cabeçalho
• Tamanho máximo IPv6• IPv6: opção Jumbo Payload – 32 bits• MTU mínimo de um enlace para:
• IPv4: 68 bytes; IPv6: 576 bytes• Path MTU• Tamanho mínimo do buffer de remontagem – tamanho
mínimo do datagrama que qualquer implementação deve suportar:• IPv4: 576 bytes
![Page 25: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/25.jpg)
25write FunctionS
ock
ets Aplicação
TCP
IP
Fila saída
Enlace
Buffer da aplicação (qualquer tamanho)
write
Buffer de envio do socket (SO_SNDBUF)
Processo do usuário
Kernel
Segmentos TCP do tamanho do MSS
MSS normalmente ≤ MTU – 40 (IPv4) ou MTU – 60 (IPv6)
Pacotes IPv4 ou IPv6 do tamanho da MTU
• Kernel copia dados do buffer da aplicação para buffer do socket.
• Se não há espaço, o processo é bloqueado até a transferência do último byte.
• Sucesso da operação write não significa que o outro lado do TCP recebeu os dados.
![Page 26: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/26.jpg)
26write Function (lib/writen.c)S
ock
ets 1 #include "unp.h"
2 ssize_t /* Write "n" bytes to a descriptor. */ 3 writen(int fd, const void *vptr, size_t n) 4 { 5 size_t nleft; 6 ssize_t nwritten; 7 const char *ptr; 8 ptr = vptr; 9 nleft = n; 10 while (nleft > 0) { 11 if ( (nwritten = write(fd, ptr, nleft)) <= 0) { 12 if (nwritten < 0 && errno == EINTR) 13 nwritten = 0; /* and call write() again */ 14 else 15 return (-1); /* error */ 16 } 17 nleft -= nwritten; 18 ptr += nwritten; 19 } 20 return (n); 21 }
![Page 27: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/27.jpg)
27read Function (lib/readn.c)S
ock
ets 1 #include "unp.h"
2 ssize_t /* Write "n" bytes to a descriptor. */ 3 readn(int fd, void *vptr, size_t n) 4 { 5 size_t nleft; 6 ssize_t nread; 7 char *ptr; 8 ptr = vptr; 9 nleft = n; 10 while (nleft > 0) { 11 if ( (nread = read(fd, ptr, nleft)) < 0) { 12 if (errno == EINTR) 13 nread = 0; /* and call read() again */ 14 else 15 return (-1); 16 } else if (nread == 0) 17 break; /* EOF */ 18 nleft -= nread; 19 ptr += nread; 20 } 21 return (n - nleft); /* return >= 0 */ 22 }
![Page 28: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/28.jpg)
28Sending and ReceivingS
ock
ets
• Same as read() and write() but for flags
• flags for I/O functions (see man pages)• MSG_DONTWAIT (this send non-blocking)• MSG_OOB (out of band data, 1 byte sent ahead)• MSG_PEEK • MSG_WAITALL (don’t give me less than max)• MSG_DONTROUTE (bypass routing table)
int recv(int sockfd, void *buff, size_t mbytes, int flags);
int send(int sockfd, void *buff, size_t mbytes, int flags);
![Page 29: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/29.jpg)
29Servidor ConcorrenteS
ock
ets
- T
CP • Após accept e fork, o processo filho executa
no CONNFD e o pai continua a escutar no LISTENFD.
• close decrementa contador de referências.
• FYN é enviado somente quando contador de referências possui valor zero.
![Page 30: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/30.jpg)
30Servidor Concorrente – forkS
ock
ets
- T
CP • fork executa uma cópia idêntica do processo.
• Retorna o identificador do processo filho ao pai (process ID) e o valor 0 (process ID) ao filho.
• Os descritores abertos antes do fork são compartilhados com os filhos.
• o “connected socked” após um accept seguido de fork é compartilhado com o filho.
# include <unistd.h>pid_t fork(void);
Returns: 0 in child, process ID of child in parent, -1 on error
![Page 31: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/31.jpg)
31Servidor Concorrente – execS
ock
ets
- T
CP • A função exec executa outro programa.
#include <unistd.h>
int execl ( const char *pathname, const char *arg0,... /* (char *) 0
*/ );int execv ( const char *pathname, char *const argv[ ] );int execle ( const char *pathname, const char *arg0, ...
/* (char *) 0, char *const envp[ ] */ );int execve ( const char *pathname, char *const argv[ ],
char *const envp[ ] );int execlp ( const char *filename, const char *arg0, ... /* (char *) 0
*/ );int execvp ( const char *filename, char *const argv[ ] );
All six return: -1 on error, no return on success
![Page 32: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/32.jpg)
32Servidor ConcorrenteS
ock
ets
- T
CP pid_t pid;
int listenfd, connfd; /* fill in sockaddr_in{ } with server's well-known port */ listenfd = socket( ... ); bind(listenfd, ... ); listen(listenfd, LISTENQ);
for ( ; ; ) { connfd = accept (listenfd, ... ); /* probably blocks */
if( (pid = fork() ) == 0) {
close(listenfd); /* child closes listening socket */ doit(connfd); /* process the request */ close(connfd); /* done with this client */ exit(0); /* child terminates */
}
close(connfd); /* parent closes connected socket */ }
![Page 33: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/33.jpg)
33Servidor ConcorrenteS
ock
ets
- T
CP
connect( )
Client
listenfd
Serverconnection
request
connect( ) listenfd
connfd
connection
connect( ) listenfd
connfd
connection
listenfd
connfd
fork
Server
(parent)
Server
(child)
connect( )listenfd
connection
connfd
Server
(parent)
Server
(child)
1
2
3
4
Stevens Vol. 1 Fig. 4.14 a 4.17
![Page 34: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/34.jpg)
34Servidor Concorrente - ExemploS
ock
ets
- T
CP • Questões: o que acontece...
• Quando o cliente e o servidor iniciam?• Se o servidor “terminar” antes do cliente finalizar a
sessão?
• Servidor Echo
• Cliente Echo
![Page 35: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/35.jpg)
35Servidor Echo (main)S
ock
ets
- T
CP 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 }
Stevens Vol. 1 Fig. 5.2 TCP echo server
![Page 36: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/36.jpg)
36Servidor Echo (str_echo)S
ock
ets
- T
CP 1 #include "unp.h"
2 3 void 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 }
![Page 37: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/37.jpg)
37Cliente Echo (main)S
ock
ets
- T
CP 1 #include "unp.h"
2 3 int main(int argc, char **argv) 4 { 5 int sockfd; 6 struct sockaddr_in servaddr; 7 if (argc != 2) 8 err_quit("usage: tcpcli <IPaddress>"); 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 }
Stevens Vol. 1 Fig. 5.4 TCP echo client
![Page 38: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/38.jpg)
38Cliente Echo (str_cli)S
ock
ets
- T
CP 1 #include "unp.h"
2 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 }
Stevens Vol. 1 Fig. 5.5 str_cli function: client processing loop
![Page 39: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/39.jpg)
39Netstat (início normal)S
ock
ets
- T
CP linux % tcpserv01 &
[1] 17870
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 % tcpcli01 127.0.0.1
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
22038 22036 pts/6 S -bash wait4
17870 22038 pts/6 S ./tcpserv01 wait_for_connect
19315 17870 pts/6 S ./tcpserv01 tcp_data_wait
19314 22038 pts/6 S ./tcpcli01 127.0 read_chan
![Page 40: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/40.jpg)
40Netstat (finalização normal)S
ock
ets
- T
CP 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
22038 22036 pts/6 S -bash read_chan
17870 22038 pts/6 S ./tcpserv01 wait_for_connect
19315 17870 pts/6 Z [tcpserv01 <defu do_exit
![Page 41: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/41.jpg)
41Finalização NormalS
ock
ets
- T
CP • Ao digitar EOF, fget retorna um ponteiro nulo e
a função str_cli retorna;• O cliente executa exit;• Descriptors no cliente são fechados e um FYN
é enviado ao servidor. Servidor no estado CLOSE_WAIT e cliente FIN_WAIT-2;
• O filho servidor estava bloqueado em readline (str_echo) e retorna para main servidor. Filho servidor termina executando exit;
• Todos os descritores do filho servidor são fechados, um FIN do servidor é enviado e um ACK do cliente;
• Conexão terminada; e• Sinal SIGCHLD é enviado.
![Page 42: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/42.jpg)
42signal FunctionS
ock
ets
- T
CP Sigfunc *signal(int signo, Sigfunc *func);
• signo is a signal number.• *func is a pointer to a signal handling function, as well
as the return value from the function.
• A signal is a notification to a process that ana event has ocurred.
• Are sometimes called software interrupts.• Usually occur asynchronously.• Signals can be sent:
• By one process to another process (or to itself), or• By the Kernel to a process.
![Page 43: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/43.jpg)
43signal FunctionS
ock
ets
- T
CP 1 #include "unp.h"
2 3 Sigfunc *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 }
Stevens Vol. 1 Fig. 5.6 signal function
![Page 44: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/44.jpg)
44Handling SIGCHLD Signals and Zombies
Sock
ets
- T
CP • The purpose of the zombies state is to maintain
information about the child for the parent to fetch at some later time.
• The 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 the 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.
• Whenever we fork children, we must wait for them to prevent them from becoming zombies.
![Page 45: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/45.jpg)
45Handling SIGCHLD Signals and Zombies
Sock
ets
- T
CP Signal (SIGCHLD, sig_chld);
...1 #include “unp.h”23 void sig_chld( int signo )4 {5 pid_t pid;6 int stat;7 pid = wait(&stat);8 printf(“child %d terminated\n”, pid);9 return;10 }
![Page 46: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/46.jpg)
46wait and waitpid Functions#include <sys/wait.h>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
Sock
ets
- T
CP
Stevens Vol. 1 Fig. 5.10
Client terminates, closing all five connections, terminating all five children.
Server
parent
Server
child #1
Server
child #2
Server
child #3
Server
child #4
Server
child #5
4 3 2 1 0
client exit
FIN
FIN
FIN
FIN
FIN
SIGCHLD
SIGCHLD
SIGCHLD
SIGCHLD
SIGCHLD
![Page 47: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/47.jpg)
47wait and waitpid Functions
• waitpid fetching the status of any of our children that have terminated.
• WNOHANG option specify waitpid not to block.
Sock
ets
- T
CP
Stevens Vol. 1 Fig. 5.11
1 #include "unp.h" 2 3 void 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 }
![Page 48: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/48.jpg)
48TCP server that handles na error of EINTRS
ock
ets
- T
CP
Stevens Vol. 1 Fig. 5.11
1 #include "unp.h" 3 int 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 }
![Page 49: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/49.jpg)
49Tecnologias de Comunicação
Sockets
Multiplexação de E/S
![Page 50: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/50.jpg)
50IntroduçãoM
ult
iple
xaçã
o d
e E
/S • Capacidade de avisar Núcleo que se deseja ser notificado quando condições de E/S estejam válidas.• Ex: dados para leitura estão disponíveis.
• Funções relacionadas:• select• poll• shutdown
![Page 51: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/51.jpg)
51Quando usarM
ult
iple
xaçã
o d
e E
/S • Quando cliente está manipulando vários descritores.• Ex: descritor do socket é de entrada interativa.
• Quando o cliente manipula vários sockets ao mesmo tempo.
• Quando TCP manipula listening sockets e outros sockets conectados
• Quando o servidor trabalha com TCP e com UDP simultaneamente.
• Quando o servidor manipula vários protocolos e serviços simultaneamente.
![Page 52: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/52.jpg)
52Tipos e OperaçõesM
ult
iple
xaçã
o d
e E
/S • Tipos:• E/S bloqueante;• E/S não bloqueante ;• Multiplexação de E/S;• E/S orientada a sinal; e• E/S assíncrona.
• Operações em duas fases:• Espera para dados estarem disponíveis e• Cópia dos dados do Kernel para o processo.
![Page 53: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/53.jpg)
53ModeloM
ult
iple
xaçã
o d
e E
/S
Stevens Vol. 1 Fig. 6.3
![Page 54: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/54.jpg)
54select FunctionM
ult
iple
xaçã
o d
e E
/S #include <sys/select.h>#include <sys/time.h>
struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */
};
int select ( int maxfdp1, fd_set *readset, fd_set *writeset,
fd_set *exceptset, const struct timeval *timeout);
Return: positive count of ready descriptors, 0 on timeout and –1 on error.
![Page 55: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/55.jpg)
55select FunctionM
ult
iple
xaçã
o d
e E
/S • Instrui o núcleo para acordar o processo quando:• Um conjunto de eventos ocorrer ou • Um certo intervalo de tempo tiver ocorrido.
• Pode especificar o descritor:• Reading, writing ou exception.
• Três possibilidades de espera:• Espera até condição ser verdadeira – null pointer;• Espera por um valor máximo de intervalo; e• Não espera. Retorna após verificar os descritores
(polling).
• Os valores de temporização em timeout devem ser zero.
![Page 56: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/56.jpg)
56poll FunctionM
ult
iple
xaçã
o d
e E
/S #include <poll.h>
struct pollfd { int fd; /* descriptor to check */ short events; /* events of interest on fd */ short revents; /* events that occurred on fd */
};
int poll ( struct pollfd *fdarray, unsigned long nfds, int timeout);
Return: -1 if an error occurred, 0 if no descriptors are ready before the timer
expires, Otherwise it is the number of descriptors that
have a nonzero revents member.
![Page 57: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/57.jpg)
57shutdown FunctionM
ult
iple
xaçã
o d
e E
/S #include <sys/socket.h>
int shutdown(int sockfd, int howto);
Return: 0 if OK or -1 on error.
Argumento howto:
• SHUT_RD – fecha para leitura (read-halt). Conteúdo do buffer e futuras recepções são
descartadas.
• SHUT_WR – qualquer dado no buffer de escrita será enviado. Não pode mais escrever.
• SHUT_RDWR – efeito de fazer SHUT_WR após o SHUT_RD.
![Page 58: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/58.jpg)
58shutdown FunctionM
ult
iple
xaçã
o d
e E
/S
Stevens Vol. 1 Fig. 6.12
![Page 59: Prof. Edmundo R. M. Madeira Carlos R. Senna MC823 Laboratório de Teleprocessamento e Redes Primeiro Semestre 2009.](https://reader036.fdocumentos.com/reader036/viewer/2022062512/552fc11b497959413d8c998f/html5/thumbnails/59.jpg)
59Tecnologias de ComunicaçãoM
C82
3
Sockets
UDP