#include <stdio.h>#include <pthread.h>#include <semaphore.h>#include <stdlib.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <stdio.h>#include <string.h>#define PROTOPORT 4433 /* default protocol port number */#define QLEN 6 /* size of request queue */int visits = 0; char n[1024];sem_t len;void * client1(int sd){struct hostent *ptrh; /* pointer to a host table entry */ struct protoent *ptrp; /* pointer to a protocol table entry */ struct sockaddr_in sad,sad1; /* structure to hold server's address */ struct sockaddr_in cad,cad1; /* structure to hold client's address */ int sd2,sd1,sd3; int alen; /* length of address */ int n,id,id1; /* temporary variable */ char buf[1000]; /* buffer for string the server sends */ char * pstrzWhichProtocol; /*store whether we are using tcp or udp*/ int bIsTCP; /*flag storing whether we are using tcp */ long lRecvd,lEchoed; /*for statistics*/ int iRecvdp, iEchoedp; /*for statistics*/ char * pstrzOutputFname,* pstrzOutputFname1; /*output file file name*/ char *client,*client1,*client2,*client3;FILE *pFileOut,*pFileOut1;pstrzOutputFname="output";pstrzOutputFname1="output1";while (bIsTCP) { /*bIsTCP will not change once defined from the beginning*/ alen = sizeof(cad); if ( (sd1=accept(sd, (struct sockaddr *)&cad, &alen)) < 0) { fprintf(stderr, "accept failed\n"); // exit(1); } iRecvdp=0; iEchoedp=0; visits++; sprintf(buf,"\n\nThis client 1 has been contacted %d time%s with tcp\n", visits,visits==1?".":"s.");printf("Server says: %s", buf); n = recv(sd1, buf, sizeof(buf), 0); /*recieves from client*/ lRecvd=(long)n; lEchoed=0l;client="10.107.12.26";client2="10.107.12.19";client1=inet_ntoa(cad.sin_addr);client3=inet_ntoa(cad.sin_addr);id=strcmp( client, client1);id1=strcmp( client2, client3);printf("id= %d\n",id);printf("id1= %d\n",id1);if (id==0)pFileOut = fopen (pstrzOutputFname,"w+");if (id1==0)pFileOut1 = fopen (pstrzOutputFname1,"w+"); while (n > 0) { iRecvdp++; iEchoedp=0; // write(1,"Client says:\n", 13); /*write it out to screen*/ //write(1,buf,n); if (id==0) fwrite(buf,1,n,pFileOut); //printf("\nfile output 1 \n");} if (id1==0) fwrite(buf,1,n,pFileOut1); // printf("\nfile output \n");} // lEchoed+=(long)send(sd2,buf,n,0); /*echoed back to client*/ n = recv(sd1, buf, sizeof(buf), 0);/*see if there's more*/ // lRecvd+=(long)n; }if (id==0)fclose(pFileOut);if (id1==0)fclose(pFileOut1); /*output statistics*/ printf("peer address is %s %d\n",client1); printf("\n\nNumber of data bits received: %d\n",lRecvd); printf("\n\nNumber of data segment received: %d\n",iRecvdp); } }void * client2(int sd){}main(argc, argv)int argc;char *argv[];{{ int status; pthread_t tr, tw; struct hostent *ptrh; /* pointer to a host table entry */ struct protoent *ptrp; /* pointer to a protocol table entry */ struct sockaddr_in sad,sad1; /* structure to hold server's address */ struct sockaddr_in cad,cad1; /* structure to hold client's address */ int sd, sd2,sd1,sd3; /* socket descriptors */ int port,port1; /* protocol port number */ int alen; /* length of address */ int n; /* temporary variable */ char buf[1000]; /* buffer for string the server sends */ char * pstrzWhichProtocol; /*store whether we are using tcp or udp*/ int bIsTCP; /*flag storing whether we are using tcp */ long lRecvd,lEchoed; /*for statistics*/ int iRecvdp, iEchoedp; /*for statistics*/ char * pstrzOutputFname,* pstrzOutputFname1; /*output file file name*/ iRecvdp=0; iEchoedp=0; memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */ sad.sin_family = AF_INET; /* set family to Internet */ sad.sin_addr.s_addr = INADDR_ANY; /* set the local IP address */ /* Check command-line argument for protocol port and extract */ /* port number if one is specified. Otherwise, use the default */ /* port value given by constant PROTOPORT */ if (argc > 1 && strcmp(argv[1],"-")) {/* if argument specified */ port = atoi(argv[1]); /* convert argument to binary */ } else { port = PROTOPORT; /* use default port number */ }/*............................................................*/ if (port > 0) /* test for illegal value */ sad.sin_port = htons((u_short)port); else { /* print error message and exit */ fprintf(stderr,"bad port number %s\n",argv[1]); exit(1); } printf("Listening on port %d\n",port);/*....................................................*/ /* Check command-line argument for which protocol to use. If none specify then start both tcp and udp */ if (argc >2 ){ pstrzWhichProtocol=argv[2]; bIsTCP=!strcmp(pstrzWhichProtocol,"tcp"); } printf("Listening for %s packets client 1 \n",pstrzWhichProtocol); /* Map protocol name to protocol number */ if ( ((int)(ptrp = getprotobyname(pstrzWhichProtocol))) == 0) { fprintf(stderr, "cannot map protocol to protocol number"); exit(1); } if ( ((int)(ptrp = getprotobyname(pstrzWhichProtocol))) == 0) { fprintf(stderr, "cannot map protocol to protocol number"); exit(1); }/* ........................................program is corrrect upto this point...................*//*if (setsockopt(sd,SOL_SOCKET,SO_REUSEADDR,&sad,sizeof(int))==-1){perror("setsockopt");exit(0);}*/ /* Create a socket, depending on whether we are running as tcp or udp server. Iftcp, then create one that uses sock_stream, otherwise, create one that uses datagram.*/ if (bIsTCP) sd = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);else sd = socket(AF_INET, SOCK_DGRAM,ptrp->p_proto);if (sd < 0) { fprintf(stderr, "socket creation failed\n"); exit(1); } /* Bind a local address to the socket */ /*In the udp and tcp servers running at the same time case, we also bind the twoservers' sockets to listen to the same port. This is ok because tcp connection require both endpoints todefine the connection, and it would not accept a udp connection - and vice versa.*/ if (bind(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) { fprintf(stderr,"bind failed 1\n"); exit(1); } /* Specify size of request queue */ /* we only call the listen function if we're running as a tcp server*/ if (bIsTCP && listen(sd, QLEN) < 0) { fprintf(stderr,"listen failed\n"); exit(1); } pthread_create(&tr,NULL,client1,sd); pthread_create(&tw,NULL,client2,0); pthread_join(tr,NULL); pthread_join(tw,NULL); return 0;}}