Download presentation
Presentation is loading. Please wait.
1
Pertemuan 7 I/O Multiplexing
Matakuliah : H0483 / Network Programming Tahun : 2008 Versi : 2.0 Pertemuan 7 I/O Multiplexing
2
Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Mendemonstrasikan program dengan menggunakan fungsi-fungsi I/O Multiplexing
3
I/O Multiplexing Model Signal driven I/O Model Asynchronous I/O Model
Outline Materi I/O Model Blocking I/O Model NonBlocking I/O Model I/O Multiplexing Model Signal driven I/O Model Asynchronous I/O Model Select function
4
Blocking I/O Model
5
Multiplexing I/O Model
6
Signal Driven
7
Select () The select() system call allows us to use blocking I/O on a set of descriptors (file, socket, etc). For example, we can ask select to notify us when data is available for reading on either STDIN or a TCP socket.
8
Select () int select( int maxfd, fd_set *readset, fd_set *writeset,
fd_set *excepset, const struct timeval *timeout); maxfd : highest number assigned to a descriptor. readset : set of descriptors we want to read from. writeset : set of descriptors we want to write to. excepset : set of descriptors to watch for exceptions. timeout : maximum time select should wait
9
Server dgn I/O Multiplex
#include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #define PORT 6789
10
Server dgn I/O Multiplex
int main(void) { fd_set master; fd_set read_fds; struct sockaddr_in myaddr; struct sockaddr_in remoteaddr; int fdmax; int listener; int newfd; int nbytes; int yes=1; int addrlen; int i,j; char buf[256]; FD_ZERO(&master); FD_ZERO(&read_fds);
11
Server dgn I/O Multiplex
//get the listener if((listener=socket(AF_INET,SOCK_STREAM,0))==-1){ perror("socket"); exit(1); } //bind myaddr.sin_family = AF_INET; myaddr.sin_addr.s_addr = INADDR_ANY; myaddr.sin_port = htons(PORT); memset(&(myaddr.sin_zero),'\0',8); if(bind(listener, (struct sockaddr *)&myaddr, sizeof(myaddr)) == -1){ perror("bind");
12
Server dgn I/O Multiplex
if(listen(listener, 10) == -1){ perror("listen"); exit(1); } //add the listener to the master set FD_SET(listener, &master); //keep track of the biggest file descriptor fdmax=listener; //main loop for(;;){ read_fds=master; //copy it if(select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1){ perror("select"); exit(1);
13
Server dgn I/O Multiplex
for(i=0; i<=fdmax; i++){ if(FD_ISSET(i, &read_fds)){ if(i==listener){ addrlen = sizeof(remoteaddr); if((newfd=accept(listener,(struct sockaddr*) &remoteaddr, &addrlen)) == -1){ perror("accept"); } else{ FD_SET(newfd, &master); if(newfd > fdmax) fdmax = newfd; printf("new connection from %s on socket %d\n", inet_ntoa(remoteaddr.sin_addr),newfd);
14
Server dgn I/O Multiplex
else{ if((nbytes=recv(i,buf,sizeof(buf),0)) <= 0){ if(nbytes =0)printf("socket %d hung up\n",i); else perror("recv"); close(i); //bye FD_CLR(i,&master); } for(j=0; j<=fdmax;j++){ if(FD_ISSET(j, &master)){ if(j != listener && j != i){ if(send(j,buf,nbytes,0)==-1) perror("send");
15
Server dgn I/O Multiplex
}//if } //for } return 0; }//main
16
Jelaskan perbedaan antara :
Latihan Jelaskan perbedaan antara : Blocking I/O Model NonBlocking I/O Model I/O Multiplexing Model Signal driven I/O Model Asynchronous I/O Model
17
Latihan Dengan menggunakan contoh server diatas, buat aplikasi untuk chatting !, dengan membuat program clientnya yang cocok untuk server diatas.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.