Quest 3
Quest 3
Quest 3
(ii) Calculation operation on server node, requested from the client node.
ANSWER-
Client-
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<unistd.h>
#include<string.h>
perror(msg);
exit(1);
if (argc<3)
exit(0);
char buffer[255];
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd<0)
server= gethostbyname(argv[1]);
if (server==NULL)
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
while(1)
bzero(buffer,255);
fgets(buffer,255,stdin);
n = write(sockfd, buffer,strlen(buffer));
if (n<0)
error("Error in writting");
}
bzero(buffer,255);
if (n<0)
error("Error in reading");
int i = strncmp("Bye",buffer,3);
if (i==0)
break;
close(sockfd);
return 0;
Server
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<string.h>
void error(const char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
if (argc<2)
{ fprintf(stderr, "Port no not given. \n");
exit(1);
}
//**** Socket fd***
int sockfd, portno, newsockfd, n;
char buffer[255];
struct sockaddr_in serv_addr, cli_addr;
socklen_t clilen;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd<0)
{
error("Enter opening scoket.");
}
portno = atoi(argv[1]);
bzero((char *) &serv_addr, sizeof(serv_addr));
//**** Bind***
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("Binding fails");
}
//**** listen and accept***
listen(sockfd, 5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd<0)
{
error("Acceptance fails");
}
//**** read and write***
while(1)
{
bzero(buffer,256);
n = read(newsockfd, buffer, 255);
if (n<0)
{
error("Error in reading");
}
printf("Client:%s \n", buffer);
bzero(buffer,255);
fgets(buffer,255,stdin);
n = write(newsockfd, buffer,strlen(buffer));
if (n<0)
{
error("Error in writting");
}
int i = strncmp("Bye",buffer,3);
if (i==0)
{
break;
}
}
//**** close***
close(newsockfd);
close(sockfd);
return 0;
}
B).
Client-
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<unistd.h>
#include<string.h>
server= gethostbyname(argv[1]);
if (server==NULL)
{
fprintf(stderr, "No such server");
}
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);
bzero(buffer,256);
n = read(sockfd, buffer, 255); // Read server string
if (n<0) error("Error in READING");
printf("Server - %s \n", buffer);
scanf("%d", &num2);
write(sockfd,&num2,sizeof(int)); //Send No 2 server
bzero(buffer,256);
n = read(sockfd, buffer, 255); // Read server string
if (n<0) error("Error in READING");
printf("Server - %s \n", buffer);
scanf("%d", &choice);
write(sockfd,&choice,sizeof(int)); //Send choice server
if(choice == 5)
goto Q;
read(sockfd, &ans, sizeof(int));
printf("Server:The answers is :%d \n", ans);
if(choice != 5)
goto S;
Q: printf("You have selected to exit. Exit successfull");
close(sockfd);
return 0;
}
Server-
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<string.h>
if (sockfd<0)
{
error("Enter opening scoket.");
}
portno = atoi(argv[1]);
//**** Bind***
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
listen(sockfd, 5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd<0)
{
error("Acceptance fails");
}
//**** Calutation***
int num1, num2, ans, choice;
C)-
Client
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<unistd.h>
#include<string.h>
#include<ctype.h>
server= gethostbyname(argv[1]);
if (server==NULL)
{
fprintf(stderr, "No such server");
}
FILE *f;
int word = 0;
char c;
f = fopen("glad.txt","r");
while((c=getc(f)) != EOF)
{
fscanf(f, "%s", buffer);
if(isspace(c) || c=='\t')
word++;
}
char ch;
while(ch != EOF)
{
fscanf(f, "%s", buffer);
write(sockfd, buffer, 255);
ch = fgetc(f);
}
printf("File send successfully.");
close(sockfd);
return 0;
}
Server-
#include<stdio.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<string.h>
#include<ctype.h>
if (sockfd<0)
{
error("Enter opening scoket.");
}
portno = atoi(argv[1]);
//**** Bind***
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
listen(sockfd, 5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd<0)
{
error("Acceptance fails");
}
FILE *fp;
char ch =0;
fp = fopen("glad_recive.txt","a");
int word;
while(ch != word)
{
read(newsockfd, buffer, 255);
fprintf(fp, "%s ", buffer);
ch++;
}
printf("File has been reciveed successfully.");
//**** close***
close(newsockfd);
close(sockfd);
return 0;
}