Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment 1: Client-server programming using Berkeley Sockets (I) Implement the

ID: 3629247 • Letter: A

Question

Assignment 1: Client-server programming using Berkeley Sockets
(I) Implement the following rudimentary string processing application using
connection-oriented client-server programming using C or C++. Some guidelines
for the implementation are as follows.
a) The client will send a textual paragraph terminated by ‘ ’ to the server
(assume that in the paragraph, ‘.’ appears only at the end of sentences and
nowhere else). The server will compute the number of characters, number of
words, and number of sentences in the paragraph, and send these numbers
back to the client. The client will print these numbers on the screen.

Explanation / Answer

#include #include #include #include #include #include #include #include #include #define PORT 3456 /* the port client will be connecting to */ #define MAXDATASIZE 100/* max number of bytes we can get at once */ int main(int argc, char *argv[]) { int sockfd, numbytes; char buf[MAXDATASIZE]; struct hostent *he; struct sockaddr_in their_addr; /* client's address information */ if (argc != 2) { fprintf(stderr,"usage: client hostname "); exit(1); } if ((he=gethostbyname(argv[1])) == NULL) /* get the host info */ { herror("gethostbyname"); exit(1); } if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } their_addr.sin_family = AF_INET; their_addr.sin_port = htons(PORT); their_addr.sin_addr = *((struct in_addr *)he->h_addr); bzero(&(their_addr.sin_zero), 8); if (connect(sockfd,(struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) { perror("connect"); exit(1); } if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1) { perror("recv"); exit(1); } int nxt; int word, sentence; char str[100]; while(strcmp(str,"q")) { printf(" Enter Your Paragraph:"); fgets ( str, MAXDATASIZE, stdin ); send(sockfd,str,MAXDATASIZE,0); recv(sockfd,&word,MAXDATASIZE,0); recv(sockfd,&nxt,MAXDATASIZE,0); recv(sockfd,&sentence,MAXDATASIZE,0); printf("Number of words:%d Number of characters:%d Number of sentences:%d",word+1,nxt+1,sentence); } //buf[numbytes] = ''; //printf("Received: %s",buf); close(sockfd); return 0; }