KEMBAR78
Sem Unp Lab Programs | PDF | Pointer (Computer Programming) | Computer Programming
0% found this document useful (0 votes)
38 views39 pages

Sem Unp Lab Programs

The document describes code for TCP and UDP client-server programs to perform various tasks like echo, reverse string, and file transfer. The TCP programs include an echo server and client that send a message back and forth, a reverse string server and client that reverse the input string, and a file transfer server and client that send a file between the two. The UDP program includes a reverse string server that takes a string from the client, reverses it, and sends it back using UDP sockets.

Uploaded by

Enduku Meeku
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views39 pages

Sem Unp Lab Programs

The document describes code for TCP and UDP client-server programs to perform various tasks like echo, reverse string, and file transfer. The TCP programs include an echo server and client that send a message back and forth, a reverse string server and client that reverse the input string, and a file transfer server and client that send a file between the two. The UDP program includes a reverse string server that takes a string from the client, reverses it, and sends it back using UDP sockets.

Uploaded by

Enduku Meeku
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

1.

TCP ECHO
SERVER
// Including header files
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>

// Error function declaration


void error(char *msg)
{
perror(msg);
exit(1);
}

//Main function
int main(int argc,char *argv[2])
{
//Variables declaration
int sockfd,newsockfd,portno,clilen;
char buffer[256],rbuffer[256];

//Structure declaration
struct sockaddr_in serv_addr, cli_addr;
int n,i,j;

//Checking of portnumber
if(argc < 2)
{
fprintf(stderr,"error,no port provided\n");
exit(1);
}

//Declaration of socket
sockfd = socket(AF_INET,SOCK_STREAM,0);

//Checking of socket created or not


if(sockfd < 0)
error("error opening socket");
bzero((char *)&serv_addr,sizeof(serv_addr));
portno=atoi(argv[1]);

//Assiging family, address and port


serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port =htons(portno);

//Binding the socket


if(bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
error("error on binding");

//Listening the socket


listen(sockfd,5);
clilen = sizeof(cli_addr);

//Accepting Connection
newsockfd = accept(sockfd,(struct sockaddr*)&cli_addr,&clilen);

if(newsockfd < 0)
error("error onaccept");
printf("Conection establishment competed\n");
bzero(buffer,256);
n = read(newsockfd,buffer,255);

if(n<0)
error("error reading from socket");

printf("Here is the message:%s",buffer);


n = write(newsockfd,buffer,sizeof(buffer));
if(n < 0)
error("error writing tosocket");

return 0;
}

CLIENT
/*Required header files */
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<arpa/inet.h>

// Error function declaration


void error(char *msg)
{
perror(msg);
exit(1);
}

// Main function declaration


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

int sockfd,portno,n;
struct sockaddr_in serv_addr;
char buffer[256];

/* checking passing ip addres and portnumber */


if(argc< 3)
{
fprintf(stderr,"usage %s hostname port \n",argv[0]);
exit(0);
}

/* assigning portnumber */
portno = atoi(argv[2]);

/* socket creation */
sockfd = socket(AF_INET,SOCK_STREAM,0);

/* checking socket created or not */


if(sockfd < 0)
error("error opening socket");

/* initially setting server addres is zero */


bzero((char *)&serv_addr,sizeof(serv_addr));

//Assiging family, address and port


serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(portno);

/* connecting to server */
if(connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) < 0)
error("error connecting");

printf("please enter the message: ");


bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if(n<0)
error("error writing to socket");

bzero(buffer,256);
n=read(sockfd,buffer,255);
if(n < 0)
error("error reading from socket");
printf("%s\n",buffer);

close(sockfd);

return 0;

2.TCP REVERSE A STRING


SERVER
// Including header files
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>

// Error function declaration


void error(char *msg)
{
perror(msg);
exit(1);
}

//Main function
int main(int argc,char *argv[2])
{
//Variables declaration
int sockfd,newsockfd,portno,clilen;
char buffer[256],rbuffer[256];

//Structure declaration
struct sockaddr_in serv_addr, cli_addr;
int n,i,j;

//Checking of portnumber
if(argc < 2)
{
fprintf(stderr,"error,no port provided\n");
exit(1);
}

//Declaration of socket
sockfd = socket(AF_INET,SOCK_STREAM,0);

//Checking of socket created or not


if(sockfd < 0)
error("error opening socket");
bzero((char *)&serv_addr,sizeof(serv_addr));
portno=atoi(argv[1]);

//Assiging family, address and port


serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port =htons(portno);

//Binding the socket


if(bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
error("error on binding");

//Listening the socket


listen(sockfd,5);
clilen = sizeof(cli_addr);

//Accepting Connection
newsockfd = accept(sockfd,(struct sockaddr*)&cli_addr,&clilen);

if(newsockfd < 0)
error("error onaccept");
printf("Conection establishment competed\n");
bzero(buffer,256);
n = read(newsockfd,buffer,255);

if(n<0)
error("error reading from socket");
j=n-1;
printf("%d",n);
for(i = 0;i<n;i++)
{
rbuffer[i]=buffer[j];
printf("%c",buffer[j]);
j--;
}
printf(" is sent");
rbuffer[n]='\0';
n = write(newsockfd,rbuffer,sizeof(rbuffer));
if(n < 0)
error("error writing tosocket");

return 0;
}

CLIENT
/*Required header files */
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

// Error function declaration


void error(char *msg) {
perror(msg);
exit(1);
}
// Main function declaration
int main(int argc, char *argv[]) {
int sockfd, portno, n;
struct sockaddr_in serv_addr;
char buffer[256];

/* checking passing ip addres and portnumber */


if (argc < 3) {
fprintf(stderr, "usage %s hostname port \n", argv[0]);
exit(0);
}

/* assigning portnumber */
portno = atoi(argv[2]);

/* socket creation */
sockfd = socket(AF_INET, SOCK_STREAM, 0);

/* checking socket created or not */


if (sockfd < 0) error("error opening socket");

/* initially setting server addres is zero */


bzero((char *)&serv_addr, sizeof(serv_addr));

// Assiging family, address and port


serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(portno);

/* connecting to server */
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
error("error connecting");

printf("please enter the message: ");


bzero(buffer, 256);
fgets(buffer, 255, stdin);
n = write(sockfd, buffer, strlen(buffer));
if (n < 0) error("error writing to socket");

bzero(buffer, 256);
n = read(sockfd, buffer, 255);
if (n < 0) error("error reading from socket");
printf("%s\n", buffer);

close(sockfd);
return 0;
}

3.TCP FILE TRANSFER


SERVER
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<stdlib.h>
#include<string.h>
#include<netinet/in.h>
#include<unistd.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc,char *argv[])
{
FILE *fp1;
int sockfd,newsockfd,portno,clilen;
char buffer[256],filename[256],c;
struct sockaddr_in serv_addr,cli_addr;
int n,i,j;
if(argc<2)
{
fprintf(stderr,"ERROR ,no port provided");
exit(1);
}
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
error("ERROR opening 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 binding");
listen(sockfd,5);
clilen=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr *)&cli_addr,&clilen);
if(newsockfd<0)
error("ERROR on accept");
bzero(buffer,256);
n=read(newsockfd,buffer,255);
if(n<0)
error("ERROR reading from socket");
printf("here is the file:%s\n",buffer);
n=strlen(buffer)-1;
//printf("here is the file:%d\n",n);
for(i=0;i<n;i++)
{
filename[i]=buffer[i];
//printf("here is the file:%c\n",filename[i]);
}
fp1=fopen(filename,"r");
//printf("here is the file:%s\n",filename);
if(fp1==NULL)
{
printf("no such file to read");
exit(0);
}
else
{
j = 0;
while ((c = getc(fp1)) != EOF)
{
printf("%c", c);
buffer[j]=c;
j++;
}
}
n = write(newsockfd,buffer,strlen(buffer));
if(n<0)
error("ERROR writing to socket");
return 0;
}

CLIENT
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<arpa/inet.h>

// Error function declaration


void error(char *msg)
{
perror(msg);
exit(1);
}

// Main function declaration


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

int sockfd,portno,n;
struct sockaddr_in serv_addr;
char buffer[256];
FILE *fp;
/* checking passing ip addres and portnumber */
if(argc< 3)
{
fprintf(stderr,"usage %s hostname port \n",argv[0]);
exit(0);
}

/* assigning portnumber */
portno = atoi(argv[2]);

/* socket creation */
sockfd = socket(AF_INET,SOCK_STREAM,0);

/* checking socket created or not */


if(sockfd < 0)
error("error opening socket");

/* initially setting server addres is zero */


bzero((char *)&serv_addr,sizeof(serv_addr));

//Assiging family, address and port


serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(portno);

/* connecting to server */
if(connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) < 0)
error("error connecting");

printf("please enter file name: ");


bzero(buffer,256);
fgets(buffer,255,stdin);
n=write(sockfd,buffer,strlen(buffer));
if(n<0)
error("ERROR writing to socket");
bzero(buffer,256);
n=read(sockfd,buffer,255);
if(n<0)
error("error reading from socket");
fp=fopen("bbb.txt","w");
fwrite(&buffer,sizeof(buffer),1,fp);
return 0;
}

4.UDP REVERSE
SERVER
#include<stdio.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<string.h>

#include<stdlib.h>

#include<unistd.h>

//Error function declaration

void error(char *msg)

perror(msg);

exit(1);
}

//Main function

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

//Variable declaration

int sockfd,newsockfd,portno,clien,serlen,clilen,bn;

char buffer[256],rbuffer[256];

//Structure declaration

struct sockaddr_in serv_addr,cli_addr; int n,i,j;

//Checking port number

if(argc<2)

fprintf(stderr,"error no port\n");

exit(1);

//Declaration of socket

sockfd=socket(AF_INET,SOCK_DGRAM,0);

//Checking of socket created or not

if(sockfd<0)

error("error opening socket");

bzero((char *)&serv_addr,sizeof(serv_addr));

portno=atoi(argv[1]);

//Assigning family, address, port

serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(portno);

//Binding the socket


if(bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
error("error on binding");

clilen = sizeof(cli_addr);

bzero(buffer,256);

while(1)

n=recvfrom(sockfd,buffer,256,0,(struct sockaddr *)&cli_addr,&clilen);

if(n<0)

error("error reading from socket");

j=n-1;

printf("%d",n);

for(i = 0;i<n;i++)

rbuffer[i]=buffer[j];

j--;

rbuffer[n]='\0';

write(1,"\nreceived a datagram:",21);

write(1,buffer,n);

write(1,"\n datagram sent:",21);


write(1,rbuffer,n);

bn=strlen(rbuffer);

n=sendto(sockfd,rbuffer,bn,0,(struct sockaddr *)&cli_addr,sizeof(cli_addr));

if(n<0)

{
error("error reading from socket");

}}

return 0;

CLIENT

#include<stdio.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

//#include<netdb.h>

#include<string.h>

#include<stdlib.h>

#include<unistd.h>
#include<arpa/inet.h> void
error(char *msg)

perror(msg);

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

int sockfd,portno,n,x,servlen,bn;

struct sockaddr_in serv_addr, cli_addr;

struct hostent *server;

char buffer[256];

if(argc<3)
{

fprintf(stderr,"usage %s hostname port\n",argv[1]);

exit(0);

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

if(sockfd<0)

error("error opening socket");

bzero((char *)&serv_addr,sizeof(serv_addr));
serv_addr.sin_family = AF_INET;

serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(portno);

while(1)

printf("\n please enter the message:");

bzero(buffer,256);

fgets(buffer,255,stdin);

servlen = sizeof(serv_addr);

bn=strlen(buffer);

n=sendto(sockfd,buffer,bn-1,0,(struct sockaddr *)&serv_addr,sizeof(serv_addr));


if(n<0)

error("error writing to socket");

n=recvfrom(sockfd,buffer,strlen(buffer),0,(struct sockaddr *)&serv_addr,&servlen);

if(n<0)

error("error reading from socket");


}

write(1,buffer,n);

return 0;
}

5.UDP FILE TRANSFER


server
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
//Error function declaration
void error(char *msg)
{
perror(msg);
exit(1);
}
//Main function
int main(int argc,char *argv[])
{
//Variable declaration
FILE *fp1;
int sockfd,newsockfd,portno,clien,serlen,clilen,bn;
char buffer[256],filename[256],c;
//Structure declaration
struct sockaddr_in serv_addr,cli_addr;
int n,i,j;
//Checking port number
if(argc<2)
{
fprintf(stderr,"error no port\n");
exit(1);
}
//Declaration of socket
sockfd=socket(AF_INET,SOCK_DGRAM,0);
//Checking of socket created or not
if(sockfd<0)
error("error opening socket");
bzero((char *)&serv_addr,sizeof(serv_addr));
portno=atoi(argv[1]);
//Assigning family, address, port
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(portno);
//Binding the socket
if(bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
error("error on binding");
clilen = sizeof(cli_addr);
bzero(buffer,256);

n=recvfrom(sockfd,buffer,256,0,(struct sockaddr *)&cli_addr,&clilen);


if(n<0)
{
error("error reading from socket");
}
//write(1,"\nreceived file:",21);
write(1,buffer,n);
bn=strlen(buffer);
for(i=0;i<bn;i++)
{
filename[i]=buffer[i];
}
fp1=fopen(filename,"r");
if(fp1==NULL)
{
printf("\n no such file to read");
exit(0);
}
else
{
j=0;
printf("\n");
while((c=getc(fp1))!= EOF)
{
printf("%c",c);
buffer[j]=c;
j++;
}
//write(1,buffer,bn);
}
n=sendto(sockfd,buffer,bn,0,(struct sockaddr *)&cli_addr,sizeof(cli_addr));
if(n<0)
{
error("error reading from socket");
}

return 0;
}

client
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
//#include<netdb.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<arpa/inet.h>
void error(char *msg)
{
perror(msg);
}
int main(int argc,char *argv[])
{
int sockfd,portno,n,x,servlen,bn;
struct sockaddr_in serv_addr, cli_addr;
struct hostent *server;
char buffer[256];
FILE *fp;
if(argc<3)
{
fprintf(stderr,"usage %s hostname port\n",argv[1]);
exit(0);
}
portno=atoi(argv[2]);
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd<0)
error("error opening socket");
bzero((char *)&serv_addr,sizeof(serv_addr));

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(portno);

printf("\n please enter the file name:");


bzero(buffer,256);
fgets(buffer,255,stdin);
servlen = sizeof(serv_addr);
bn=strlen(buffer);

n=sendto(sockfd,buffer,bn-1,0,(struct sockaddr *)&serv_addr,sizeof(serv_addr));


if(n<0)
{
error("error writing to socket");
}
n=recvfrom(sockfd,buffer,strlen(buffer),0,(struct sockaddr *)&serv_addr,&servlen);
if(n<0)
{
error("error reading from socket");
}
fp=fopen("bbb.txt","w");
fwrite(&buffer,sizeof(buffer),1,fp);
//write(1,buffer,n);
return 0;
}

6.UDP SPECIAL CHARACTER

SERVER

#include<stdio.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<string.h>

#include<stdlib.h>

#include<unistd.h>

//Error function declaration

void error(char *msg)


{

perror(msg);

exit(1);

//Main function

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

//Variable declaration

int sockfd,newsockfd,portno,clien,serlen,clilen,bn;

char buffer[256],input_string[256];

//Structure declaration

struct sockaddr_in serv_addr,cli_addr;

int n,i,j,k,count=0;

//Checking port number

if(argc<2)

fprintf(stderr,"error no port\n");

exit(1);

//Declaration of socket

sockfd=socket(AF_INET,SOCK_DGRAM,0);

//Checking of socket created or not

if(sockfd<0)

error("error opening socket");

bzero((char *)&serv_addr,sizeof(serv_addr));

portno=atoi(argv[1]);
//Assigning family, address, port

serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(portno);

//Binding the socket

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


error("error on binding");

clilen = sizeof(cli_addr);

bzero(buffer,256);

n=recvfrom(sockfd,buffer,256,0,(struct sockaddr *)&cli_addr,&clilen);

if(n<0)

error("error reading from socket");

write(1,"\nreceived a datagram:",21);
write(1,buffer,n);

bn=strlen(buffer);

for(i=0;i<bn;i++)

input_string[i]=buffer[i];

bzero(buffer,256);

for(i=0;i<bn;i++)

if((input_string[i]>='A' && input_string[i]<='Z') || (input_string[i]>='a' && input_string[i]<='z') ||


(input_string[i]>='0' && input_string[i]<='9'))

continue;

else

count++;

}
sprintf(buffer,"%d",count);

printf("\n special characters");

write(1,buffer,n);

n=sendto(sockfd,buffer,bn,0,(struct sockaddr *)&cli_addr,sizeof(cli_addr));

if(n<0)

error("error reading from socket");

return 0;

CLIENT

#include<stdio.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

//#include<netdb.h>

#include<string.h>

#include<stdlib.h>

#include<unistd.h>
#include<arpa/inet.h> void
error(char *msg)

perror(msg);

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


{

int sockfd,portno,n,x,servlen,bn,count;

struct sockaddr_in serv_addr, cli_addr;

struct hostent *server;

char buffer[256];

if(argc<3)

fprintf(stderr,"usage %s hostname port\n",argv[1]);

exit(0);

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

if(sockfd<0)

error("error opening socket");

bzero((char *)&serv_addr,sizeof(serv_addr));

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);

serv_addr.sin_port = htons(portno);

printf("\n please enter the message:");

bzero(buffer,256);

fgets(buffer,255,stdin);

servlen = sizeof(serv_addr);

bn=strlen(buffer);

n=sendto(sockfd,buffer,bn-1,0,(struct sockaddr *)&serv_addr,sizeof(serv_addr));


if(n<0)
{

error("error writing to socket");


}

n=recvfrom(sockfd,buffer,strlen(buffer),0,(struct sockaddr

*)&serv_addr,&servlen);

if(n<0)

error(" error reading from socket");

write(1,buffer,n);

printf(" special characters");


return 0;
}

7.UDP ADDITION

SERVER

#include<stdio.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<string.h>

#include<stdlib.h>

#include<unistd.h>

//Error function declaration

void error(char *msg)


{

perror(msg);

exit(1);

//Main function

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

//Variable declaration

int sockfd,newsockfd,portno,clien,serlen,clilen,bn;

char buffer[256],rbuffer[256];

int a,b,sum;

//Structure declaration

struct sockaddr_in serv_addr,cli_addr;

int n,i,j,k,count=0;

//Checking port number

if(argc<2)

fprintf(stderr,"error no port\n");

exit(1);

//Declaration of socket

sockfd=socket(AF_INET,SOCK_DGRAM,0);

//Checking of socket created or not

if(sockfd<0)

error("error opening socket");


bzero((char *)&serv_addr,sizeof(serv_addr));

portno=atoi(argv[1]);

//Assigning family, address, port

serv_addr.sin_family=AF_INET;

serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(portno);

//Binding the socket

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


error("error on binding");

clilen = sizeof(cli_addr);

bzero(buffer,256);

n=recvfrom(sockfd,buffer,256,0,(struct sockaddr *)&cli_addr,&clilen);


if(n<0)
error("error reading from socket");

a=atoi(buffer);

n=recvfrom(sockfd,rbuffer,256,0,(struct sockaddr *)&cli_addr,&clilen);


if(n<0)
error("error reading from socket");
b=atoi(rbuffer);

sum=a+b;

printf("\nSum of given numbers is: %d",sum);


bzero(buffer,256);

sprintf(buffer,"%d",sum);

bn=strlen(buffer);

write(1,buffer,n);

n=sendto(sockfd,buffer,bn,0,(struct sockaddr *)&cli_addr,sizeof(cli_addr));

if(n<0)

error("error reading from socket");


}

return 0;

CLIENT

#include<stdio.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

//#include<netdb.h>

#include<string.h>

#include<stdlib.h>

#include<unistd.h>
#include<arpa/inet.h>

void error(char *msg)

perror(msg);

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

int sockfd,portno,n,x,servlen,bn1,bn2;

struct sockaddr_in serv_addr, cli_addr;

struct hostent *server;

char buffer[256],rbuffer[256];

if(argc<3)
{

fprintf(stderr,"usage %s hostname port\n",argv[1]);

exit(0);

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

if(sockfd<0)

error("error opening socket");

bzero((char *)&serv_addr,sizeof(serv_addr));

serv_addr.sin_family = AF_INET;

serv_addr.sin_addr.s_addr = inet_addr(argv[1]);

serv_addr.sin_port = htons(portno);

printf("\n please enter the value of a:");


bzero(buffer,256);

fgets(buffer,255,stdin);

printf("\n please enter the value of b:");

bzero(rbuffer,256);

fgets(rbuffer,255,stdin);

servlen = sizeof(serv_addr);

bn1=strlen(buffer);

bn2=strlen(rbuffer);

n=sendto(sockfd,buffer,bn1-1,0,(struct sockaddr

*)&serv_addr,sizeof(serv_addr));

if(n<0)

error("error writing to socket");


}

//bzero(buffer,256);

n=sendto(sockfd,rbuffer,bn2-1,0,(struct sockaddr *)&serv_addr,sizeof(serv_addr));

if(n<0)

error("error writing to socket");

n=recvfrom(sockfd,buffer,strlen(buffer),0,(struct sockaddr

*)&serv_addr,&servlen);

if(n<0)

error(" error reading from socket");

write(1,buffer,n);

printf(" is the sum of given numbers\n");

return 0;
}

9.PIPES

WEEK 10(a):
PROBLEM STATEMENT :

To design a application to transfer and receive the data simultaneously using pipes.

Solution Design:
REFERENCE IMPLEMENTATION :-
PIPE .C

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(void) {
int pfds[2];
char buf[30];
pipe(pfds);
if (!fork()) {
printf(" CHILD: writing to the pipe\n");
write(pfds[1], "test", 5);
printf(" CHILD: exiting\n");
exit(0);
} else {
printf("PARENT: reading from pipe\n");
read(pfds[0], buf, 5);
printf("PARENT: read \"%s\"\n", buf);
wait(NULL);
}
return 0;
}

TEST CASES :

TEST CASE 1:
PARENTS WRITES : THIS PROGRAM IS DONE BY USING PIPES

CHILD READS : THIS PROGRAM IS DONE BY USING PIPES

TEST CASE 2:
PARENTS WRITES : USAGE OF PIPES

CHILD READS : USAGE OF PIPES

EXECUTION STEPS :

TO COMPILE THE PROGRAM :


CC PIPES .C

TO RUN THE PROGRAM :


./A.OUT

10.FIFO
WEEK 10(B)
PROBLEM STATEMENT
To design a application to transfer and receive the data simultaneously using
named pipes.

RERFERNCE IMPLEMENTATION :-

PROCESS 1.C

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define FIFO_NAME "me_maid"
int main(void)
{
char s[300];
int num, fd;
mknod(FIFO_NAME, S_IFIFO | 0666, 0);
printf("waiting for readers...\n");
fd = open(FIFO_NAME, O_WRONLY);
printf("got a reader--type some data\n");
while (gets(s), !feof(stdin)) {
if ((num = write(fd, s, strlen(s))) == -1)
perror("write");
else
printf("speak: wrote %d bytes\n", num);
}
return 0;
}

PROCESS 2.C
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define FIFO_NAME "me_maid"
int main(void)
{
char s[300];
int num, fd;
mknod(FIFO_NAME, S_IFIFO | 0666, 0);
printf("waiting for writers...\n");
fd = open(FIFO_NAME, O_RDONLY);
printf("got a writer\n");
do {
if ((num = read(fd, s, 300)) == -1)
perror("read");
else {
s[num] = '\0';
printf("tick: read %d bytes: \"%s\"\n", num, s);
}
} while (num > 0);
return 0;
}

Test Cases :

Test Case1:
Waiting for readers..
Got the reader ..type the data….helloworld
Wrote the 10 bytes..

Waiting for writers


Got the writer
Read the 10 bytes,helloworld

Test Case2:
Waiting for readers..
Got the reader ..type the data….namedpipe
Wrote the 9 bytes..

Waiting for writers


Got the writer
Read the 9 bytes,named pipe

Execution steps:

To compile the process 1 :

cc process1.c

To compile the process 2:

cc process2.c

To start the process 1

./a.out

To start the process 2

./a.out

11.MESSAGE QUEUE
WEEK-11
Problem statement:

Implement file transfer using Message Queue form of IPC


-creating and sending to a simple message queue

Reference Implementation:
Messagequeue.c
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>

main()
{
int q,w,m1,n,i,id;
char msg[50];
m1=msgget("1234",IPC_CREAT | 0666);

if(m1!=-1)
{
printf("created the message queue Id:%d",m1);
}
else
{
printf("The message queue is not created");
exit(1);
}

struct msgbuf
{
long mtype;
char mtext[50];
};

struct msgbuf *wmsg;


struct msgbuf *rmsg;

wmsg=(struct msgbuf *)malloc(sizeof(struct msgbuf)+strlen(msg));


printf("\nEnter the no of messages:");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
wmsg->mtype=i;
printf("\nEnter A msg ::");
scanf("%s",&msg);
strcpy(wmsg->mtext,msg);
q=msgsnd(m1,wmsg,strlen(msg)+1,IPC_NOWAIT);
if(q!=-1)
printf("\nWritten Message:%s",wmsg->mtext);
}
rmsg=(struct msgbuf *)malloc(sizeof(struct msgbuf)+strlen(msg));
id=fork();
if (id>0)
sleep(20);
if(id==0)
{ for(i=1;i<=n;i++)
{
printf("Enter the msg u want to read:");
scanf("%d",&i);
w=msgrcv(m1,rmsg,strlen(msg)+1,i,IPC_NOWAIT);
if(w!=-1)
printf("\n Received Message id: Message:%d %s\n",rmsg->mtype,rmsg->mtext);
}
}
}

Test cases :
Test case 1:
Message queue created:67892
Enter no of message: 3
Enter the message : hello
Message written is :hello
Enter the message: message queue
Message written is: message queue
Enter the message:inter process communication
Message written is:inter process communication
Enter the message do you want to read: 2
Received message is,message:2 ,message queue..

Execution steps:

To compile the program :

cc messagequeue.c

To run the program:

./a.out

12.SHARED MEMORY
WEEK-12A
Problem statement: To implement the shared memory concept…

Reference implementation:
Sharedmemory.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/shm.h>

#define SHM_SIZE 1024 /* creating a 1K shared memory segment */

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


{
key_t key;
int shmid;
char *data;
int mode;

if(argc>2)
{
fprintf(stderr,"usage:shmdemo [data_to_write]\n");
exit(1);
}

/* now create the key */

if((key = ftok("shmdemo.c",'R'))==-1)
{
perror("ftok");
exit(1);
}

// now we need to connect to and possibly create the segment

if((shmid=shmget(key,SHM_SIZE,0644 | IPC_CREAT))==-1)
{
perror("shmget");
exit(1);
}

// attach to the segment to get a pointer to it...

data=shmat(shmid,(void *)0,0);

if(data== (char *) (-1) )


{
perror("shmat");
exit(1);
}

// read or modify the segment based on the command line

if(argc==2)
{
printf("writing to the segment:\"%s\"\n",argv[1]);

strncpy(data,argv[1],SHM_SIZE);
}
else
{
printf("segment contains:\"%s\"\n",data);

//detach from the segment...

if(shmdt(data)==-1)
{
perror("shmdt");
exit(1);
}

return 0;
}

Test Cases :

Test case 1:

Writing to the segment: welcome to shared memory

Segment contains: welcome to shared memory

Test case 2:

Writing to the segment: usage of shared memory

Segment contains: usage of shared memory


Execution steps:

To compile the program :

cc sharedmemory.c

To run the program:

./a.out

WEEK-12B

Problem statement:

Write a program to create an integer variable using shared memory concept and increment the
variable simultaneously by two processes. Use semaphores to avoid race conditions

Reference Implementation:

Sharedmemory.c

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
int main(void) {
pid_t pid;
int *shared; /* pointer to the shm */
int shmid;
shmid = shmget(IPC_PRIVATE, sizeof(int), IPC_CREAT | 0666);
printf("Shared Memory ID=%u",shmid);
if (fork() == 0) { /* Child */
/* Attach to shared memory and print the pointer */
shared = shmat(shmid, (void *) 0, 0);
printf("Child pointer %u\n", shared);
*shared=1;
printf("Child value=%d\n", *shared);
sleep(2);
printf("Child value=%d\n", *shared);
} else { /* Parent */
/* Attach to shared memory and print the pointer */
shared = shmat(shmid, (void *) 0, 0);
printf("Parent pointer %u\n", shared);
printf("Parent value=%d\n", *shared);
sleep(1);
*shared=42;
printf("Parent value=%d\n", *shared);
sleep(5);
shmctl(shmid, IPC_RMID, 0);
}
}

Expected output:
Shared Memory ID=0Parent pointer 1435201536
Parent value=0
Shared Memory ID=0Child pointer 1435201536
Child value=1
Parent value=42
Child value=42

You might also like