Implement 3 basic functions: GET, BOUNCE and EXIT 1. Cient input: GET <Test.txt>
ID: 3816476 • Letter: I
Question
Implement 3 basic functions: GET, BOUNCE and EXIT
1. Cient input: GET <Test.txt> SErver output: THe content of Test.txt
2;Client Input BOUNCE <Hello World> SErver output: Hello World
3. For Exit:
The difference between EXIT and EXIT <code> is:
For EXIT command, you should print out the default value/message.
For EXIT<code> command, you should print out the "code"
For example, for EXIT CHROME_ERROR server end should print out the message "CHROME_ERROR"
For EXIT, server end should print a message like "Normal_Exit"
Explanation / Answer
1)Client.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <stdlib.h>
#include <memory.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <stdarg.h>
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char const *argv[])
{
int sockfd, portno, n;
char buffer[256];
struct sockaddr_in serv_addr;
struct hostent *server;
if (argc < 3)
{
fprintf(stderr, "No proper arguments ");
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd<0)
{
error("Error in creating socket");
}
server = gethostbyname(argv[1]);
if(server == NULL)
{
fprintf(stderr, "ERROR no such host");
exit(0);
}
bzero((char *)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd, (struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
{
error("Error on connect");
}
while(1){
printf("Enter command: ");
bzero(buffer, 256);
fgets(buffer, 255, stdin);
n = send(sockfd, buffer, strlen(buffer),0);
bzero(buffer, 256);
n = read(sockfd, buffer, 255);
if(n < 0)
{
error("No command entered ");
}
else
{
printf("%s ", buffer);
if(strstr(buffer,"Client Disconnecting")!=0)
{
close(sockfd);
return 0;
}
}
}
return 0;
2)Server.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
#include <stdlib.h>
#include <memory.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <stdarg.h>
#include <string.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char const *argv[])
{
FILE * file;
char str[999];
int sockfd, cli, portno, clilen, n, count=0;
char buffer[256];
char temp[256];
char * tmp;
struct sockaddr_in serv_addr, cli_addr;
if (argc<2)
{
fprintf(stderr, "Error, no port info ");
exit(0);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd<0)
{
error("Error in creating socket");
}
bzero((char *)&serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr))<0)
{
error("Error on bind");
exit(0);
}
listen(sockfd,2);
clilen = sizeof(cli_addr);
cli = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (cli == -1)
{
error("Error in accepting client");
exit(0);
}
printf("New Client connected from port number %d and IP %s ",ntohs(cli_addr.sin_port), inet_ntoa(cli_addr.sin_addr));
n = 1;
while(1)
{
bzero(buffer,256);
bzero(temp,256);
count = 0;
n = read(cli, buffer, 255);
printf("Received command: %s ", buffer);
strcpy(temp, buffer);
tmp = strtok (temp," ");
if(tmp==NULL)
{
end(cli, "IMPROPER COMMAND", 16, 0);
continue;
}
if(strcmp("GET",tmp)==0)
{
tmp = strtok(NULL, " ");
printf("%s",tmp );
if(tmp==NULL)
{
bzero(buffer,256);
strcpy(buffer,"No Filename Entered");
n = send(cli, buffer, strlen(buffer),0);
continue;
}
printf(" Trying to open FILE: %s ", tmp);
file = fopen(tmp, "r");
if (file)
{
while (fscanf(file, "%s", str)!=EOF)
printf("%s",str);
printf(" ");
fclose(file);
send(cli, "FILE FOUND", 10, 0);
continue;
}
printf("Invalid Filename ");
send(cli, "INVALID FILENAME", 16, 0);
continue;
}
else
{
if(strcmp("BOUNCE",tmp)==0)
{
tmp = strtok(NULL, " ");
while(tmp!=NULL)
{
printf("%s ",tmp);
tmp = strtok(NULL, " ");
}
printf(" ");
send(cli, "MESSAGE DISPLAYED", 17, 0);
continue;
}
else
{
if (strcmp("EXIT ",tmp)==0 || strcmp("EXIT",tmp)==0)
{
printf("EXITING ");
send(cli, "Client Disconnecting", 21, 0);
tmp = strtok(NULL, " ");
while(tmp!=NULL)
{
printf("%s ",tmp);
tmp = strtok(NULL, " ");
}
printf(" Client Disconnected ");
return 0;
}
else
{
printf("Not proper command ");
send(cli, "IMPROPER COMMAND", 16, 0);
close(cli);
close(sockfd);
continue;
}
}
}
}
close(sockfd);
close(cli);
printf("Client Disconnected ");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.