SlideShare ist ein Scribd-Unternehmen logo
1 von 17
LAB #19
Ques1:Discuss the concepts of I/O Multiplexing?
Ans1: There were problemswith previous echo client
example,like:
1. Client was blocked in fgets.
2. It was able to see FIN when sent by the server.
3. Only detected server close when called read.
Problems were resolved by I/O multiplexingusing Select
and Poll functions.
Ques2: What are the I/O Models. Discuss it.
Ans2: There are five I/O models that we are going to talk
about:
a.) Blocking i/o:
 All example we have seen so far.
 This is default for sockets.
b.) Non blocking i/o:
 Don’t put process to sleep
 return error instead.
 Need to check periodically.
 Waste of CPU.
c.) I/O Multiplexing:
With i/o multiplexing , we call select or poll and
block in one of these two system calls, instead of
blocking in the actual I/O system call.
d.) Signal driven i/o:
The signal driven i/o model uses signals, telling the
kernel to notify us with the SIGIO signal when the
descriptor is ready.
e.) Asynchronous i/o:
Asynchronous i/o is defined by the POSIX specification,
and various differences in the real-time functions that
appeared in the various standards which came together to
form the current POSIX specification have been
reconciled.
Ques3.Comparedifferent types of I/O Models.
Ans3:
Second phase in allof the first four models is same the
difference is in first phase.First four are synchronousi/o and
the last one is asynchronous.Difference between both is that
synchronousi/o operation causes the requesting process to be
blocked util that i/o operation completes whereas in
asynchronousi/o operationdoes not cause the requesting
process to be blocked.
The diagram below show the differences between all five:
Ques4-What are the concepts of select and poll
function.
Ans4-
Select():-
This function allowsthe process to instruct the kernel to wait
for any one of multiple events to occur and to wake up the
process only when one or more of these events occurs or when
a specified amount of time has passed.
Poll():- This function was originated with SVR3 and was
originallylimited to stream devices.it is same as select() except
it provides additionfunctionalitiesin case of stream devices.
Ques5:Designa TCP concurrent server to convert a given
text into upper case using multiplexing system call
“select”.
Ans5.
Ans-> #include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#define MAXBUFFER 1024
void sendstring(int , char *);
int main( int C, char *V[] ) {
int sd,fd; char c;
struct sockaddr_in serveraddress;
char text[100] ; int i=0;
sd = socket( AF_INET, SOCK_STREAM, 0 );
if( sd < 0 ) {
perror( "socket" );
exit( 1 ); }
if (V[1] == NULL ) {
printf ("PL specfiy the server's IP Address n");
exit(0); }
if (V[2] == NULL ) {
printf ("PL specify the server's Port No n");
exit(0); }
memset( &serveraddress, 0, sizeof(serveraddress) );
serveraddress.sin_family = AF_INET;
serveraddress.sin_port = htons(atoi(V[2]));//PORT NO
serveraddress.sin_addr.s_addr = inet_addr(V[1]);//ADDRESS
if (connect(sd,(struct sockaddr*)&serveraddress,
sizeof(serveraddress))<0) {
printf("Cannot Connect to server");
exit(1); }
printf("enter sentence to end enter #");
while(1) {
c=getchar();
if(c=='#')
break;
text[i++]=c; }
text[i]='0';
sendstring(sd,text);
close(sd);
return 0; }
void sendstring(
int sd, /*Socket Descriptor*/
char *fname) /*Array Containing the string
{ int n , byteswritten=0 , written ;
char buffer[MAXBUFFER];
strcpy(buffer , fname);
n=strlen(buffer);
while (byteswritten<n)
{ written=write(sd , buffer+byteswritten,(n-byteswritten));
byteswritten+=written; }
printf("String : %s sent to server n",buffer);
/*TCP SERVER_SELECT*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/time.h>>
#include <fcntl.h>
#define MAXLINE 100
#define SERV_PORT 13153
int main(int argc, char **argv)
{ int k, i, maxi, maxfd, listenfd, connfd, sockfd;
int nready, client[FD_SETSIZE];
ssize_t n;
fd_set rset, allset;
char line[MAXLINE],buf[100];
socklen_t clilen;
struct sockaddr_in cliaddr, servaddr;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd < 0 ) {
perror("socket" );
exit(1); }
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);-
listen(listenfd,5);
maxfd = listenfd; /* initialize */
maxi = -1; /* index into client[] array */
for (i = 0; i < FD_SETSIZE; i++)
client[i] = -1; /* -1 indicates available entry */
FD_ZERO(&allset);
FD_SET(listenfd, &allset);
for ( ; ; ) {
printf("Server:I am waiting-----Start of Main Loopn");
rset = allset; /* structure assignment */
nready = select(maxfd+1, &rset, NULL, NULL, NULL);
if (FD_ISSET(listenfd, &rset)) {
clilen = sizeof(cliaddr);
connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);
#ifdef NOTDEF
printf("new client: %s, port %dn",inet_ntop(AF_INET,
&cliaddr.sin_addr, buf, NULL),ntohs(cliaddr.sin_port));
#endif
for (i = 0; i < FD_SETSIZE; i++)
if (client[i] < 0) {
client[i] = connfd; /* save descriptor */
break;}
if (i == FD_SETSIZE) {
printf("too many clients");
exit(0); }
FD_SET(connfd, &allset);
if (connfd > maxfd)
maxfd = connfd;
if (i > maxi)
maxi = i;
if (--nready <= 0)
continue;}
for (i = 0; i <= maxi; i++) {
if ( (sockfd = client[i]) < 0)
continue;
if (FD_ISSET(sockfd, &rset)) {
if ( (n = read(sockfd, line, MAXLINE)) == 0) {
close(sockfd);
FD_CLR(sockfd, &allset);
client[i] = -1;
} else{
printf("n output at servern");
for(k=0;line[k]!='0';k++)
printf("%c",toupper(line[k]));
write(sockfd, line, n);}
if (--nready <= 0)
break; } }}}
Ques6:esign using poll client server application to
multiplex TCP and UDP requests for converting a given
text into upper case.
Ans6:
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
#define MAXLINE 20
#define SERV_PORT 8114
main(int argc,char **argv){
int maxfdp1; fd_set rset;
char sendline[MAXLINE],recvline[MAXLINE];
int sockfd;
struct sockaddr_in servaddr;
if(argc!=2){
printf("usage tcpcli <ipaddress>"); return;}
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(SERV_PORT);
inet_pton(AF_INET,argv[1],&servaddr.sin_addr);
connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
printf("nenter data to be send:");
while(fgets(sendline,MAXLINE,stdin)!=NULL){
write(sockfd,sendline,MAXLINE);
printf("nline send to server :%s ",sendline);
read(sockfd,recvline,MAXLINE);
printf("line received from the server : %s",recvline);}
exit(0);}
#include<stdio.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/select.h>
#include<unistd.h>
#define MAXLINE 20
#define SERV_PORT 8114
main(int argc,char **argv){
int i,j,maxi,maxfd,listenfd,connfd,sockfd;
int nready,client[FD_SETSIZE]; ssize_t n;
fd_set rset,allset; char line[MAXLINE];
socklen_t clilen; struct sockaddr_in cliaddr,servaddr;
listenfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY );
servaddr.sin_port=htons(SERV_PORT);
bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
listen(listenfd,1);
maxfd=listenfd;
maxi=-1;
for(i=0;i<FD_SETSIZE;i++)
client[i]=-1;
FD_ZERO(&allset);
FD_SET(listenfd,&allset);
for(;;){
rset=allset;
nready=select(maxfd+1,&rset,NULL,NULL,NULL);
if(FD_ISSET(listenfd,&rset)){
clilen=sizeof(cliaddr);
connfd=accept(listenfd,(struct sockaddr *)&cliaddr,&clilen);
for(i=0;i<FD_SETSIZE;i++)
if(client[i]<0){
client[i]=connfd;
break;}
if(i==FD_SETSIZE){
printf("too many clients");
exit(0);}
FD_SET(connfd,&allset);
if(connfd>maxfd)
maxfd=connfd;
if(i>maxi)
maxi=i;
if(--nready<=0)
continue;}
for(i=0;i<=maxi;i++) {
if((sockfd=client[i])<0)
continue;
if(FD_ISSET(sockfd,&rset)){
if((n=read(sockfd,line,MAXLINE))==0){
close(sockfd);
FD_CLR(sockfd,&allset);
client[i]=-1;}
else{{
printf("line received from client:%sn",line);
for(j=0;line[j]!='0';j++)
line[j]=toupper(line[j]);
write(sockfd,line,MAXLINE);}
if(--nready<=0) break;} } } }
#define MAXLINE 20
#define SERV_PORT 8114
main(int argc,char **argv){
int maxfdp1; fd_set rset;
char sendline[MAXLINE],recvline[MAXLINE];
int sockfd;
struct sockaddr_in servaddr;
if(argc!=2){
printf("usage tcpcli <ipaddress>"); return;}
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(SERV_PORT);
inet_pton(AF_INET,argv[1],&servaddr.sin_addr);
connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
printf("nenter data to be send:");
while(fgets(sendline,MAXLINE,stdin)!=NULL){
write(sockfd,sendline,MAXLINE);
printf("nline send to server :%s ",sendline);
read(sockfd,recvline,MAXLINE);
printf("line received from the server : %s",recvline);}
exit(0);}
#include<stdio.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/select.h>
#include<unistd.h>
#define MAXLINE 20
#define SERV_PORT 8114
main(int argc,char **argv){
int i,j,maxi,maxfd,listenfd,connfd,sockfd;
int nready,client[FD_SETSIZE]; ssize_t n;
fd_set rset,allset; char line[MAXLINE];
socklen_t clilen; struct sockaddr_in cliaddr,servaddr;
listenfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(SERV_PORT);
bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
listen(listenfd,1);
maxfd=listenfd;
maxi=-1;
for(i=0;i<FD_SETSIZE;i++)
client[i]=-1;
FD_ZERO(&allset);
FD_SET(listenfd,&allset);
for(;;){
rset=allset;
nready=select(maxfd+1,&rset,NULL,NULL,NULL);
if(FD_ISSET(listenfd,&rset)){
clilen=sizeof(cliaddr);
connfd=accept(listenfd,(struct sockaddr *)&cliaddr,&clilen);
for(i=0;i<FD_SETSIZE;i++)
if(client[i]<0){
client[i]=connfd;
break;}
if(i==FD_SETSIZE){
printf("too many clients");
exit(0);}
FD_SET(connfd,&allset);
if(connfd>maxfd)
maxfd=connfd;
if(i>maxi)
maxi=i;
if(--nready<=0)
continue;}
for(i=0;i<=maxi;i++) {
if((sockfd=client[i])<0)
continue;
if(FD_ISSET(sockfd,&rset)){
if((n=read(sockfd,line,MAXLINE))==0){
close(sockfd);
FD_CLR(sockfd,&allset);
client[i]=-1;}
else{{
printf("line received from client:%sn",line);
for(j=0;line[j]!='0';j++)
line[j]=toupper(line[j]);
write(sockfd,line,MAXLINE);}
if(--nready<=0) break;} } } }

Weitere ähnliche Inhalte

Was ist angesagt?

Best Bugs from Games: Fellow Programmers' Mistakes
Best Bugs from Games: Fellow Programmers' MistakesBest Bugs from Games: Fellow Programmers' Mistakes
Best Bugs from Games: Fellow Programmers' MistakesAndrey Karpov
 
Asterisk: PVS-Studio Takes Up Telephony
Asterisk: PVS-Studio Takes Up TelephonyAsterisk: PVS-Studio Takes Up Telephony
Asterisk: PVS-Studio Takes Up TelephonyAndrey Karpov
 
Евгений Крутько, Многопоточные вычисления, современный подход.
Евгений Крутько, Многопоточные вычисления, современный подход.Евгений Крутько, Многопоточные вычисления, современный подход.
Евгений Крутько, Многопоточные вычисления, современный подход.Platonov Sergey
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 FeaturesJan Rüegg
 
100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects 100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects Andrey Karpov
 
C++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical ReviewerC++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical ReviewerAndrey Karpov
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...Francesco Casalegno
 
About Those Python Async Concurrent Frameworks - Fantix @ OSTC 2014
About Those Python Async Concurrent Frameworks - Fantix @ OSTC 2014About Those Python Async Concurrent Frameworks - Fantix @ OSTC 2014
About Those Python Async Concurrent Frameworks - Fantix @ OSTC 2014Fantix King 王川
 
The CppCat Analyzer Checks TortoiseGit
The CppCat Analyzer Checks TortoiseGitThe CppCat Analyzer Checks TortoiseGit
The CppCat Analyzer Checks TortoiseGitAndrey Karpov
 
Devirtualizing FinSpy
Devirtualizing FinSpyDevirtualizing FinSpy
Devirtualizing FinSpyjduart
 

Was ist angesagt? (20)

Npc08
Npc08Npc08
Npc08
 
4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf
 
Best Bugs from Games: Fellow Programmers' Mistakes
Best Bugs from Games: Fellow Programmers' MistakesBest Bugs from Games: Fellow Programmers' Mistakes
Best Bugs from Games: Fellow Programmers' Mistakes
 
Virtual Machine Constructions for Dummies
Virtual Machine Constructions for DummiesVirtual Machine Constructions for Dummies
Virtual Machine Constructions for Dummies
 
Asterisk: PVS-Studio Takes Up Telephony
Asterisk: PVS-Studio Takes Up TelephonyAsterisk: PVS-Studio Takes Up Telephony
Asterisk: PVS-Studio Takes Up Telephony
 
Interpreter, Compiler, JIT from scratch
Interpreter, Compiler, JIT from scratchInterpreter, Compiler, JIT from scratch
Interpreter, Compiler, JIT from scratch
 
Евгений Крутько, Многопоточные вычисления, современный подход.
Евгений Крутько, Многопоточные вычисления, современный подход.Евгений Крутько, Многопоточные вычисления, современный подход.
Евгений Крутько, Многопоточные вычисления, современный подход.
 
C++ L01-Variables
C++ L01-VariablesC++ L01-Variables
C++ L01-Variables
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 Features
 
100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects 100 bugs in Open Source C/C++ projects
100 bugs in Open Source C/C++ projects
 
TensorFlow XLA RPC
TensorFlow XLA RPCTensorFlow XLA RPC
TensorFlow XLA RPC
 
C++11
C++11C++11
C++11
 
C++11
C++11C++11
C++11
 
Ppt of socket
Ppt of socketPpt of socket
Ppt of socket
 
C++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical ReviewerC++ Code as Seen by a Hypercritical Reviewer
C++ Code as Seen by a Hypercritical Reviewer
 
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
 
About Those Python Async Concurrent Frameworks - Fantix @ OSTC 2014
About Those Python Async Concurrent Frameworks - Fantix @ OSTC 2014About Those Python Async Concurrent Frameworks - Fantix @ OSTC 2014
About Those Python Async Concurrent Frameworks - Fantix @ OSTC 2014
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
The CppCat Analyzer Checks TortoiseGit
The CppCat Analyzer Checks TortoiseGitThe CppCat Analyzer Checks TortoiseGit
The CppCat Analyzer Checks TortoiseGit
 
Devirtualizing FinSpy
Devirtualizing FinSpyDevirtualizing FinSpy
Devirtualizing FinSpy
 

Ähnlich wie Lab

Remote Procedure Call
Remote Procedure CallRemote Procedure Call
Remote Procedure CallNadia Nahar
 
CODE FOR echo_client.c A simple echo client using TCP #inc.pdf
CODE FOR echo_client.c A simple echo client using TCP  #inc.pdfCODE FOR echo_client.c A simple echo client using TCP  #inc.pdf
CODE FOR echo_client.c A simple echo client using TCP #inc.pdfsecunderbadtirumalgi
 
Please help with the below 3 questions, the python script is at the.pdf
Please help with the below 3  questions, the python script is at the.pdfPlease help with the below 3  questions, the python script is at the.pdf
Please help with the below 3 questions, the python script is at the.pdfsupport58
 
Build reliable, traceable, distributed systems with ZeroMQ
Build reliable, traceable, distributed systems with ZeroMQBuild reliable, traceable, distributed systems with ZeroMQ
Build reliable, traceable, distributed systems with ZeroMQRobin Xiao
 
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...Vincenzo Iozzo
 
How to Leverage Go for Your Networking Needs
How to Leverage Go for Your Networking NeedsHow to Leverage Go for Your Networking Needs
How to Leverage Go for Your Networking NeedsDigitalOcean
 
maxbox starter72 multilanguage coding
maxbox starter72 multilanguage codingmaxbox starter72 multilanguage coding
maxbox starter72 multilanguage codingMax Kleiner
 
OpenSSL Basic Function Call Flow
OpenSSL Basic Function Call FlowOpenSSL Basic Function Call Flow
OpenSSL Basic Function Call FlowWilliam Lee
 
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptINTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptsenthilnathans25
 
Socket Programming Intro.pptx
Socket  Programming Intro.pptxSocket  Programming Intro.pptx
Socket Programming Intro.pptxssuserc4a497
 
Socket programming
Socket programmingSocket programming
Socket programmingAnurag Tomar
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial javaTpoint s
 
Optimization in Programming languages
Optimization in Programming languagesOptimization in Programming languages
Optimization in Programming languagesAnkit Pandey
 
Scalable Socket Server by Aryo
Scalable Socket Server by AryoScalable Socket Server by Aryo
Scalable Socket Server by AryoAgate Studio
 
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
 Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docxMARRY7
 

Ähnlich wie Lab (20)

Remote Procedure Call
Remote Procedure CallRemote Procedure Call
Remote Procedure Call
 
CODE FOR echo_client.c A simple echo client using TCP #inc.pdf
CODE FOR echo_client.c A simple echo client using TCP  #inc.pdfCODE FOR echo_client.c A simple echo client using TCP  #inc.pdf
CODE FOR echo_client.c A simple echo client using TCP #inc.pdf
 
Please help with the below 3 questions, the python script is at the.pdf
Please help with the below 3  questions, the python script is at the.pdfPlease help with the below 3  questions, the python script is at the.pdf
Please help with the below 3 questions, the python script is at the.pdf
 
Build reliable, traceable, distributed systems with ZeroMQ
Build reliable, traceable, distributed systems with ZeroMQBuild reliable, traceable, distributed systems with ZeroMQ
Build reliable, traceable, distributed systems with ZeroMQ
 
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
Post Exploitation Bliss: Loading Meterpreter on a Factory iPhone, Black Hat U...
 
How to Leverage Go for Your Networking Needs
How to Leverage Go for Your Networking NeedsHow to Leverage Go for Your Networking Needs
How to Leverage Go for Your Networking Needs
 
maxbox starter72 multilanguage coding
maxbox starter72 multilanguage codingmaxbox starter72 multilanguage coding
maxbox starter72 multilanguage coding
 
Socket programming
Socket programming Socket programming
Socket programming
 
OpenSSL Basic Function Call Flow
OpenSSL Basic Function Call FlowOpenSSL Basic Function Call Flow
OpenSSL Basic Function Call Flow
 
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.pptINTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
INTRODUCTION TO SOCKETS IN COMPUTER NETWORKS DEPT OF CSE.ppt
 
Socket Programming Intro.pptx
Socket  Programming Intro.pptxSocket  Programming Intro.pptx
Socket Programming Intro.pptx
 
sockets_intro.ppt
sockets_intro.pptsockets_intro.ppt
sockets_intro.ppt
 
Sockets intro
Sockets introSockets intro
Sockets intro
 
Socket programming
Socket programmingSocket programming
Socket programming
 
CGI.ppt
CGI.pptCGI.ppt
CGI.ppt
 
C programming language tutorial
C programming language tutorial C programming language tutorial
C programming language tutorial
 
Optimization in Programming languages
Optimization in Programming languagesOptimization in Programming languages
Optimization in Programming languages
 
Scalable Socket Server by Aryo
Scalable Socket Server by AryoScalable Socket Server by Aryo
Scalable Socket Server by Aryo
 
Blazing Fast Windows 8 Apps using Visual C++
Blazing Fast Windows 8 Apps using Visual C++Blazing Fast Windows 8 Apps using Visual C++
Blazing Fast Windows 8 Apps using Visual C++
 
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
 Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx Lab Assignment 4 CSE330 Spring 2014  Skeleton Code for ex.docx
Lab Assignment 4 CSE330 Spring 2014 Skeleton Code for ex.docx
 

Kürzlich hochgeladen

ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEaurabinda banchhor
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxruthvilladarez
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 

Kürzlich hochgeladen (20)

ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
Dust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSEDust Of Snow By Robert Frost Class-X English CBSE
Dust Of Snow By Robert Frost Class-X English CBSE
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
TEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docxTEACHER REFLECTION FORM (NEW SET........).docx
TEACHER REFLECTION FORM (NEW SET........).docx
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 

Lab

  • 1. LAB #19 Ques1:Discuss the concepts of I/O Multiplexing? Ans1: There were problemswith previous echo client example,like: 1. Client was blocked in fgets. 2. It was able to see FIN when sent by the server. 3. Only detected server close when called read. Problems were resolved by I/O multiplexingusing Select and Poll functions. Ques2: What are the I/O Models. Discuss it. Ans2: There are five I/O models that we are going to talk about: a.) Blocking i/o:  All example we have seen so far.  This is default for sockets.
  • 2. b.) Non blocking i/o:  Don’t put process to sleep  return error instead.  Need to check periodically.  Waste of CPU. c.) I/O Multiplexing: With i/o multiplexing , we call select or poll and block in one of these two system calls, instead of blocking in the actual I/O system call.
  • 3. d.) Signal driven i/o: The signal driven i/o model uses signals, telling the kernel to notify us with the SIGIO signal when the descriptor is ready. e.) Asynchronous i/o:
  • 4. Asynchronous i/o is defined by the POSIX specification, and various differences in the real-time functions that appeared in the various standards which came together to form the current POSIX specification have been reconciled. Ques3.Comparedifferent types of I/O Models. Ans3: Second phase in allof the first four models is same the difference is in first phase.First four are synchronousi/o and the last one is asynchronous.Difference between both is that synchronousi/o operation causes the requesting process to be blocked util that i/o operation completes whereas in asynchronousi/o operationdoes not cause the requesting process to be blocked.
  • 5. The diagram below show the differences between all five: Ques4-What are the concepts of select and poll function. Ans4- Select():- This function allowsthe process to instruct the kernel to wait for any one of multiple events to occur and to wake up the process only when one or more of these events occurs or when a specified amount of time has passed.
  • 6. Poll():- This function was originated with SVR3 and was originallylimited to stream devices.it is same as select() except it provides additionfunctionalitiesin case of stream devices. Ques5:Designa TCP concurrent server to convert a given text into upper case using multiplexing system call “select”. Ans5. Ans-> #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <fcntl.h> #define MAXBUFFER 1024 void sendstring(int , char *); int main( int C, char *V[] ) { int sd,fd; char c; struct sockaddr_in serveraddress; char text[100] ; int i=0; sd = socket( AF_INET, SOCK_STREAM, 0 ); if( sd < 0 ) { perror( "socket" );
  • 7. exit( 1 ); } if (V[1] == NULL ) { printf ("PL specfiy the server's IP Address n"); exit(0); } if (V[2] == NULL ) { printf ("PL specify the server's Port No n"); exit(0); } memset( &serveraddress, 0, sizeof(serveraddress) ); serveraddress.sin_family = AF_INET; serveraddress.sin_port = htons(atoi(V[2]));//PORT NO serveraddress.sin_addr.s_addr = inet_addr(V[1]);//ADDRESS if (connect(sd,(struct sockaddr*)&serveraddress, sizeof(serveraddress))<0) { printf("Cannot Connect to server"); exit(1); } printf("enter sentence to end enter #"); while(1) { c=getchar(); if(c=='#') break; text[i++]=c; } text[i]='0'; sendstring(sd,text); close(sd); return 0; } void sendstring(
  • 8. int sd, /*Socket Descriptor*/ char *fname) /*Array Containing the string { int n , byteswritten=0 , written ; char buffer[MAXBUFFER]; strcpy(buffer , fname); n=strlen(buffer); while (byteswritten<n) { written=write(sd , buffer+byteswritten,(n-byteswritten)); byteswritten+=written; } printf("String : %s sent to server n",buffer); /*TCP SERVER_SELECT*/ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include <string.h> #include <sys/types.h> #include <sys/stat.h> #include <sys/select.h> #include <sys/time.h>> #include <fcntl.h> #define MAXLINE 100 #define SERV_PORT 13153 int main(int argc, char **argv) { int k, i, maxi, maxfd, listenfd, connfd, sockfd; int nready, client[FD_SETSIZE];
  • 9. ssize_t n; fd_set rset, allset; char line[MAXLINE],buf[100]; socklen_t clilen; struct sockaddr_in cliaddr, servaddr; listenfd = socket(AF_INET, SOCK_STREAM, 0); if (listenfd < 0 ) { perror("socket" ); exit(1); } bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT);- listen(listenfd,5); maxfd = listenfd; /* initialize */ maxi = -1; /* index into client[] array */ for (i = 0; i < FD_SETSIZE; i++) client[i] = -1; /* -1 indicates available entry */ FD_ZERO(&allset); FD_SET(listenfd, &allset); for ( ; ; ) { printf("Server:I am waiting-----Start of Main Loopn"); rset = allset; /* structure assignment */ nready = select(maxfd+1, &rset, NULL, NULL, NULL); if (FD_ISSET(listenfd, &rset)) { clilen = sizeof(cliaddr);
  • 10. connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen); #ifdef NOTDEF printf("new client: %s, port %dn",inet_ntop(AF_INET, &cliaddr.sin_addr, buf, NULL),ntohs(cliaddr.sin_port)); #endif for (i = 0; i < FD_SETSIZE; i++) if (client[i] < 0) { client[i] = connfd; /* save descriptor */ break;} if (i == FD_SETSIZE) { printf("too many clients"); exit(0); } FD_SET(connfd, &allset); if (connfd > maxfd) maxfd = connfd; if (i > maxi) maxi = i; if (--nready <= 0) continue;} for (i = 0; i <= maxi; i++) { if ( (sockfd = client[i]) < 0) continue; if (FD_ISSET(sockfd, &rset)) { if ( (n = read(sockfd, line, MAXLINE)) == 0) { close(sockfd); FD_CLR(sockfd, &allset);
  • 11. client[i] = -1; } else{ printf("n output at servern"); for(k=0;line[k]!='0';k++) printf("%c",toupper(line[k])); write(sockfd, line, n);} if (--nready <= 0) break; } }}} Ques6:esign using poll client server application to multiplex TCP and UDP requests for converting a given text into upper case. Ans6: #include<string.h> #include<stdlib.h> #include<sys/socket.h> #include<netinet/in.h>#include<string.h> #include<stdlib.h> #include<sys/socket.h> #include<netinet/in.h> #define MAXLINE 20 #define SERV_PORT 8114 main(int argc,char **argv){ int maxfdp1; fd_set rset; char sendline[MAXLINE],recvline[MAXLINE]; int sockfd;
  • 12. struct sockaddr_in servaddr; if(argc!=2){ printf("usage tcpcli <ipaddress>"); return;} sockfd=socket(AF_INET,SOCK_STREAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(SERV_PORT); inet_pton(AF_INET,argv[1],&servaddr.sin_addr); connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)); printf("nenter data to be send:"); while(fgets(sendline,MAXLINE,stdin)!=NULL){ write(sockfd,sendline,MAXLINE); printf("nline send to server :%s ",sendline); read(sockfd,recvline,MAXLINE); printf("line received from the server : %s",recvline);} exit(0);} #include<stdio.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> #include<sys/select.h> #include<unistd.h> #define MAXLINE 20 #define SERV_PORT 8114 main(int argc,char **argv){ int i,j,maxi,maxfd,listenfd,connfd,sockfd; int nready,client[FD_SETSIZE]; ssize_t n;
  • 13. fd_set rset,allset; char line[MAXLINE]; socklen_t clilen; struct sockaddr_in cliaddr,servaddr; listenfd=socket(AF_INET,SOCK_STREAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY ); servaddr.sin_port=htons(SERV_PORT); bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr)); listen(listenfd,1); maxfd=listenfd; maxi=-1; for(i=0;i<FD_SETSIZE;i++) client[i]=-1; FD_ZERO(&allset); FD_SET(listenfd,&allset); for(;;){ rset=allset; nready=select(maxfd+1,&rset,NULL,NULL,NULL); if(FD_ISSET(listenfd,&rset)){ clilen=sizeof(cliaddr); connfd=accept(listenfd,(struct sockaddr *)&cliaddr,&clilen); for(i=0;i<FD_SETSIZE;i++) if(client[i]<0){ client[i]=connfd; break;} if(i==FD_SETSIZE){ printf("too many clients"); exit(0);}
  • 14. FD_SET(connfd,&allset); if(connfd>maxfd) maxfd=connfd; if(i>maxi) maxi=i; if(--nready<=0) continue;} for(i=0;i<=maxi;i++) { if((sockfd=client[i])<0) continue; if(FD_ISSET(sockfd,&rset)){ if((n=read(sockfd,line,MAXLINE))==0){ close(sockfd); FD_CLR(sockfd,&allset); client[i]=-1;} else{{ printf("line received from client:%sn",line); for(j=0;line[j]!='0';j++) line[j]=toupper(line[j]); write(sockfd,line,MAXLINE);} if(--nready<=0) break;} } } } #define MAXLINE 20 #define SERV_PORT 8114 main(int argc,char **argv){ int maxfdp1; fd_set rset; char sendline[MAXLINE],recvline[MAXLINE]; int sockfd; struct sockaddr_in servaddr;
  • 15. if(argc!=2){ printf("usage tcpcli <ipaddress>"); return;} sockfd=socket(AF_INET,SOCK_STREAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_port=htons(SERV_PORT); inet_pton(AF_INET,argv[1],&servaddr.sin_addr); connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr)); printf("nenter data to be send:"); while(fgets(sendline,MAXLINE,stdin)!=NULL){ write(sockfd,sendline,MAXLINE); printf("nline send to server :%s ",sendline); read(sockfd,recvline,MAXLINE); printf("line received from the server : %s",recvline);} exit(0);} #include<stdio.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> #include<sys/select.h> #include<unistd.h> #define MAXLINE 20 #define SERV_PORT 8114 main(int argc,char **argv){ int i,j,maxi,maxfd,listenfd,connfd,sockfd;
  • 16. int nready,client[FD_SETSIZE]; ssize_t n; fd_set rset,allset; char line[MAXLINE]; socklen_t clilen; struct sockaddr_in cliaddr,servaddr; listenfd=socket(AF_INET,SOCK_STREAM,0); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(SERV_PORT); bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr)); listen(listenfd,1); maxfd=listenfd; maxi=-1; for(i=0;i<FD_SETSIZE;i++) client[i]=-1; FD_ZERO(&allset); FD_SET(listenfd,&allset); for(;;){ rset=allset; nready=select(maxfd+1,&rset,NULL,NULL,NULL); if(FD_ISSET(listenfd,&rset)){ clilen=sizeof(cliaddr); connfd=accept(listenfd,(struct sockaddr *)&cliaddr,&clilen); for(i=0;i<FD_SETSIZE;i++) if(client[i]<0){ client[i]=connfd; break;}
  • 17. if(i==FD_SETSIZE){ printf("too many clients"); exit(0);} FD_SET(connfd,&allset); if(connfd>maxfd) maxfd=connfd; if(i>maxi) maxi=i; if(--nready<=0) continue;} for(i=0;i<=maxi;i++) { if((sockfd=client[i])<0) continue; if(FD_ISSET(sockfd,&rset)){ if((n=read(sockfd,line,MAXLINE))==0){ close(sockfd); FD_CLR(sockfd,&allset); client[i]=-1;} else{{ printf("line received from client:%sn",line); for(j=0;line[j]!='0';j++) line[j]=toupper(line[j]); write(sockfd,line,MAXLINE);} if(--nready<=0) break;} } } }