Quest 3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 22

Question-3: Implement the following TCP/IP program and show the output.

(i) Client Server chat program?

(ii) Calculation operation on server node, requested from the client node.

(iii) File transfer between the client and server.

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>

void error(const char *msg)

perror(msg);

exit(1);

int main(int argc, char const* argv[])

if (argc<3)

fprintf(stderr, "Port no %s not given. \n", argv[0]);

exit(0);

//**** Socket fd***

int sockfd, portno, newsockfd, n;

char buffer[255];

struct sockaddr_in serv_addr;

struct hostent *server;

portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd<0)

error("Enter opening scoket.");

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);

//**** connection ***

if(connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))<0)

error("Connection Failed ");

//**** read and write***

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);

n = read(sockfd, buffer, 255);

if (n<0)

error("Error in reading");

printf("Server:%s \n", buffer);

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>

void error(const char *msg)


{
perror(msg);
exit(1);
}

int main(int argc, char const* argv[])


{
if (argc<3)
{
fprintf(stderr, "Port no %s not given. \n", argv[0]);
exit(0);
}
//**** Socket fd***
int sockfd, portno, newsockfd, n;
char buffer[255];
struct sockaddr_in serv_addr;
struct hostent *server;
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd<0)
{
error("Enter opening scoket.");
}

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);

//**** connection ***


if(connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))<0)
{
error("Connection Failed ");
}
//**** Calutation ***
int num1, num2, ans, choice;
S : 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", &num1);
write(sockfd,&num1,sizeof(int)); //Send No 1 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", &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>

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");
}

//**** Calutation***
int num1, num2, ans, choice;

S: n = write(newsockfd,"Enter Number 1:",strlen("Enter Number 1:")); //


Asking Number 1
if(n<0) error("Error in writting");
n = read(newsockfd, &num1, sizeof(num1));
printf("Client:Number 1:%d \n", num1);

n = write(newsockfd,"Enter Number 2:",sizeof("Enter Number 1:")); //


Asking Number 2
if(n<0) error("Error in writting");
n = read(newsockfd, &num2, sizeof(num2));
printf("Client:Number 2:%d \n", num2);
n = write(newsockfd,"Enter your choice: \n1:Addition\n2:Substraction\
n3:Multiplication\n4:Division\n5:Exit",sizeof("Enter your choice: \n1:Addition\
n2:Substraction\n3:Multiplication\n4:Division\n5:Exit")); // Asking choice
if(n<0) error("Error in writting");
n = read(newsockfd, &choice, sizeof(int));
printf("Client:choice is:%d \n", choice);
switch(choice)
{
case 1:
ans = num1 + num2;
break;
case 2:
ans = num1 - num2;
break;
case 3:
ans = num1 * num2;
break;
case 4:
ans = num1 / num2;
break;
case 5:
goto Q;
break;
}
write(newsockfd, &ans, sizeof(int));
if(choice !=5)
goto S;
//**** close***
Q: close(newsockfd);
close(sockfd);
return 0;
}

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>

void error(const char *msg)


{
perror(msg);
exit(1);
}

int main(int argc, char const* argv[])


{
if (argc<3)
{
fprintf(stderr, "Port no %s not given. \n", argv[0]);
exit(0);
}

//**** Socket fd***


int sockfd, portno, newsockfd, n;
char buffer[255];
struct sockaddr_in serv_addr;
struct hostent *server;
portno = atoi(argv[2]);

sockfd = socket(AF_INET, SOCK_STREAM, 0);


if (sockfd<0)
{
error("Enter opening scoket.");
}

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);

//**** connection ***


if(connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr))<0)
{
error("Connection Failed ");
}
bzero(buffer, 255);

//**** File Transfer***

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++;
}

write(sockfd, &word, sizeof(int));


rewind(f);

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>

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");
}

//**** File Transfer***

FILE *fp;
char ch =0;
fp = fopen("glad_recive.txt","a");
int word;

read(newsockfd, &word, sizeof (int));

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;
}

You might also like