Thursday, December 30, 2010
'grep' Command implementation in C.
Sunday, April 18, 2010
Socket Programs (Computer Network Lab)
Q1. Write a program to create a file using create system call with file permissions 777 and then change them to 463. Create another file with permission 463 and copy the first into second file using read and write system call.
Program :
#include
main()
{
int fid1,fid2,i,j;
char* buff;
if((creat("file1.txt",777))==-1);
{
printf("Can't create the file1");
exit(1);
}
exec("chmod 463 file1.txt");
if((creat("file2.txt",463))==-1);
{
printf("Can't create the file2");
exit(1);
}
if((fid1=open("file1.txt",0))==-1)
{
printf("Can't open file1");
exit(1);
}
if((fid2=open("file2.txt",1))==-1);
{
printf("Can't open file2");
exit(1);
}
while((i=read(fid1,buff,512))!=0)
{
if(i==-1);
{
printf("Can't read file1");
close(fid1);
close(fid2);
exit(1);
}
if((j=write(fid2,buff,512))==-1)
{
printf("Can't write file2");
close(fid1);
close(fid2);
exit(1);
}
}
close(fid1);
close(fid2);
}
Q2. Write a program to create a new process using fork system call if
Child Process executing ps command
Parent Process must wait until child dies
Program :
#include
main()
{
int childpid;
if((childpid=fork())==-1)
{
printf("Can't Fork");
exit(1);
}
else if(childpid==0)
{
system("ps -l");
}
else
{
wait();
}
}
Q3. Write a program to display parent pid and grandparent pid
Program :
#include
main()
{
int childpid;
if((childpid=fork())==-1)
{
printf("Can't Fork");
exit(1);
}
else if(childpid==0)
{
printf(“parent pid = %d” , getppid () );
}
else
{
printf(“grandparent pid = %d” , getppid () );
}
}
Q4. Write a program to implement simple UDP one way communication protocol.
UDP Client
#include
#include
#include
#include
#include
#include
#include
#include
int main()
{
int sockfd,bt,l,t;
char messege[100];
struct sockaddr_in cli_addr,serv;
bzero((char *)&serv,sizeof(serv));
serv.sin_family=AF_INET;
serv.sin_addr.s_addr=inet_addr("172.31.104.11");
serv.sin_port=htons(4112);
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)
{
printf("\n\nSOCKET CAN'T BE CREATED");
exit(0);
}
bzero((char *)&cli_addr,sizeof(cli_addr));
cli_addr.sin_family=AF_INET;
cli_addr.sin_addr.s_addr=htonl(INADDR_ANY);
cli_addr.sin_port=htons(0);
t=sizeof(cli_addr);
bt=bind(sockfd,(struct sockaddr *)&cli_addr,t);
if(bt<0)
{
printf("\n\nBIND ERRORS");
exit(0);
}
printf("\n\nENTER THE MESSEGE :");
scanf("%s",messege);
l=strlen(messege);
messege[l]='\0';
printf("\n\n%d",l);
sendto(sockfd,messege,l,0,(struct sockaddr *)&serv,sizeof(serv));
printf("\n\nMESSEGE HAVE BEEN SENT:");
close(sockfd);
}
UDP Server
#include
#include
#include
#include
#include
#include
#include
#define SERV_UDP_PORT 5000
int main()
{
int sockfd,newsockfd,bt,l;
char buff[100]=" ";
struct sockaddr_in serv_addr,cli_addr;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd<0)
{
printf("\n\nSOCKET CAN'T BE CREATED");
exit(0);
}
bzero((char *)&serv_addr,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
serv_addr.sin_port=htons(4112);
bt=bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr));
if(bt<0)
{
printf("\nBIND ERRORS");
exit(0);
}
l=sizeof(cli_addr) ;
recvfrom(sockfd,buff,25,0,(struct sockaddr *)&cli_addr,&l);
buff[25]='\0';
printf("\n\nMESSEGE HAVE BEEN RECEIVED");
printf("\n\n");
fputs( buff, stdout) ;
printf("\n\n");
exit(0);
close(sockfd);
}
Q5. Write a program to implement calculator with client-server communication.
Calculator client
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void writen( int , char [], int );
main()
{
int socfd, l1,l2,l3 ,r;
struct sockaddr_in client ;
char p[20], p1[13];
char opr1[3], opr2[3],opr[3], result[5];
int length;
socfd=socket(AF_INET, SOCK_STREAM, 0) ;
if ( socfd <>
printf("\n client socket failed ");
exit(0) ;
}
bzero( ( char *)&client, sizeof(client) );
client.sin_family=AF_INET ;
client.sin_addr.s_addr=inet_addr("127.0.0.1");
client.sin_port=htons(4888) ;
int conresult;
if(conresult=connect(socfd,( struct sockaddr *)&client, sizeof(client)) <>
{
printf("\n connection errors ");
exit(0) ;
}
printf( "\n enter the operand 1 : ") ;
scanf("\n %s",opr1);
l1=strlen(opr1);
send(socfd, opr1,l1,0 );
printf( "\n enter the operand 2:");
scanf("\n %s", opr2) ;
l2=strlen(opr2) ;
send(socfd, opr2,l2,0 );
printf( "\n enter the operator :");
scanf("\n %s", opr) ;
l3=strlen(opr);
send(socfd, opr,l3,0 );
printf( "\n client side result is ");
recv( socfd, result, 10, 0);
result[10]=0;
printf("\n %s", result);
printf("\n\n\n");}
Calculator Server
#include
#include
#include
#include
#include
#include
#include
#include
void add( int ,int , int);
void sub( int ,int, int );
void mul( int , int, int );
void div( int ,int, int);
void convert( int , char []);
main()
{
int sfd, newsock, length,i, pid;
struct sockaddr_in server, client ;
int rec, r1,r2,len, n;
int res,res1,res2,res3;
char msg[5],opr1 [3],opr2[3],opr[3],ch,buffer[10], buffer1[10], sign[10];
sfd=socket(AF_INET, SOCK_STREAM,0);
if(sfd<>
{ printf("\n server failed ");
exit(0) ;
}
bzero(( char *)&server, sizeof(server) );
server.sin_family=AF_INET ;
server.sin_addr.s_addr=htonl(INADDR_ANY) ;
server.sin_port=htons(4888) ;
int bresult;
bresult=bind( sfd, (struct sockaddr *)&server, sizeof(server) );
if( bresult <>
{
printf("\n bind errors ");
exit(0) ;
}
listen( sfd,5);
while(1)
{
length=sizeof(client) ;
newsock=accept(sfd, (struct sockaddr *)&client, (socklen_t *)&length);
if(newsock <>
{
printf("\n error ");
exit(0) ;
}
pid=fork();
if( pid==0)
{
close(sfd);
rec=recv(newsock, opr1,3,0);
recv(newsock, opr2,3,0);
recv(newsock, opr,2,0 );
opr[2]=0;
opr1[3]=0;
r1=atoi(opr1) ;
r2=atoi(opr2) ;
ch=opr[0];
switch(ch)
{
case '+' :
res=r1+r2;
break;
case '-' : res=r1-r2;
break ;
case '*' : res=r1*r2;
break ;
case '/' : res1=r1/(float)r2 ;
sprintf(msg,"%s");
send( newsock, msg,10,0);
break ;
}
res2=res;
res3=abs(res) ;
i=0;
while( res3 > 0)
{
buffer1[i]=48+res3%10;
i++;
res3=res3/10;
}
len=strlen(buffer1);
for( n=0;n
{
buffer[n]=buffer1[(len-1)-n];
}
buffer[n+1]=0;
if( res2 <>
{
sign[0]='-' ;
strcat( sign, buffer) ;
printf("\n minus");
send( newsock, sign,10,0);
}
send( newsock ,buffer , 10, 0) ;
exit(0);
}
exit(0);
}
}
void add ( int r1, int r2, int sfd)
{
int res;
res=r1+r2;
printf( "\n result is : %d", res) ;
}
void sub( int r1, int r2, int sfd)
{
int res;
res=r1-r2;
printf( "\n result is : %d", res) ;
}
void mul( int r1, int r2, int sfd)
{
int res ;
res=r1*r2 ;
printf("\n rsult is : %d", res) ;
}
void div( int r1, int r2, int sfd)
{
int res;
res=r1/r2;
printf("\n result is :%d", res);
}
void convert(int res, char buffer[] )
{
char p[10],p1[10],sign[2];
int i,n, len, n1,l, res1,res2;
i=0;
res1=abs(res);
while(res1 >0)
{
n=res1%10;
p[i]=n+48;
res1=res1/10;
i++;
}
len=strlen(p);
l=len;
for(n1=0;n1
buffer[n1]=p[l-n1];
p1[n1]=0;
if(res2 <0)
{
sign[0]='-';
sign[1]=0;
strcat(sign, buffer);
}
}
Q6. Write a program to daytime using client-server model.
Daytime Client
#include
#include
#include
#define MAXLINE 1024
int
main(int argc,char *argv[])
{ int sockfd,n;
char line[MAXLINE+1];
struct sockaddr_in servaddr;
if(argc!=2)
printf("\nusage: a.out
if( (sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
printf("\nsocket error");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port =htons(13000);
if(inet_pton(AF_INET,argv[1],&servaddr.sin_addr)<=0)
printf("\ninet_pton error for %s",argv[1]);
if(connect(sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr))<0)
printf("\nconnect error");
while( (n=read(sockfd,line,MAXLINE))>0)
{ line[n]=0;
if(fputs(line,stdout)==EOF)
printf("\nfputs error");
}
if(n<0)
printf("\nread error");
exit(0);
}
Daytime Server
#include
#include
#include
#include
#include
#include
#include
#define MAX 1024
main(int argc,char *argv[])
{
int sockfd,ansockfd,clilen;
char buff[MAX];
struct sockaddr_in ser_addr,cli_addr;
time_t ticks;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
printf("server can't open stream socket..");
bzero((char*) &ser_addr,sizeof(ser_addr));
ser_addr.sin_family=AF_INET;
ser_addr.sin_addr.s_addr=inet_addr("172.31.104.11");
ser_addr.sin_port=htons(13000);
if(bind(sockfd,(struct sockaddr*)&ser_addr,sizeof(ser_addr))<0)
printf("server can't bind local address..");
listen(sockfd,5);
clilen=sizeof(cli_addr);
ansockfd=accept(sockfd,(struct sockaddr *)&cli_addr,&clilen);
if(ansockfd<0)
printf("server accept error..");
else printf("stream echo server connected to the client...\n");
ticks=time(NULL);
snprintf(buff,sizeof(buff),"%24s\r\n",ctime(&ticks));
write(ansockfd,buff,strlen(buff));
printf("client disconnected from stream echo server....\n");
close(sockfd);
close(ansockfd);
}
Q7.SIMPLE TCP Server Client Program
SERVER:
#include
#include
#include
#include
#include
#include
//#include
#include
#define SERV_TCP_PORT 4000
int main()
{
int sockfd,newsockfd,l,length,bt,lt,at,i,cpid;
char *pname,buff[100];
struct sockaddr_in cli_addr,serv_addr;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
printf("\n\nSOCKET IS NOT CREATED ");
exit(0);
}
bzero((char *)&serv_addr,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
i=serv_addr.sin_port=htons(4119);
if(bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
{
printf("\n\nBIND ERRORS ");
exit(0);
}
listen(sockfd,5);
for( ; ; )
{
length=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr *)&cli_addr,(socklen_t *)&length);
if(newsockfd<0)
{
printf("\n\nACCEPT ERRORS ");
exit(0);
}
cpid=fork();
if(cpid==0)
{
close(sockfd);
printf("\n\nCLIENT IS CONNECTED");
recv(newsockfd,buff,15,0);
buff[20]='\0';
printf("\n\nMESSEGE RECEIVED");
printf("\n\n%s",buff);
exit(0);
}
close(newsockfd);
}
}
CLIENT:
#include
#include
#include
#include
#include
#include
#include
#include
#define SERV_TCP_PORT 4000
int main()
{
int sockfd,l;
char buff[100],messege[50];
struct sockaddr_in serv_addr;
bzero((char *)&serv_addr,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
serv_addr.sin_port=htons(4119);
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("\n\nSOCKET CAN'T BE CREATED");
exit(0);
}
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
{
printf("\n\nNOT CONNECTED SUCCESSFULLY");
exit(0);
}
//printf("\n\nCONNECTED SUCCESSFULLY");
printf("\n\nENTER ANY MESSEGE YOU WANT TO SEND :");
scanf("%s",messege);
l=strlen(messege);
messege[l]='\0';
printf("\n%d",l);
send(sockfd,messege,20,0);
printf("\n\nMESSEGE HAVE BEEN SENT\n\n");
}
Q8 ECHO SERVER CLIENT PROGRAM USING TCP.
SERVER:
#include
#include
#include
#include
#include
#include
//#include
#include
#define SERV_TCP_PORT 4000
int main()
{
int sockfd,newsockfd,l,length,bt,lt,at,i,cpid;
char *pname,buff[100];
struct sockaddr_in cli_addr,serv_addr;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
printf("\n\nSOCKET IS NOT CREATED ");
exit(0);
}
bzero((char *)&serv_addr,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
i=serv_addr.sin_port=htons(4119);
if(bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
{
printf("\n\nBIND ERRORS ");
exit(0);
}
listen(sockfd,5);
for( ; ; )
{
length=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr *)&cli_addr,(socklen_t *)&length);
if(newsockfd<0)
{
printf("\n\nACCEPT ERRORS ");
exit(0);
}
cpid=fork();
if(cpid==0)
{
close(sockfd);
printf("\n\nCLIENT IS CONNECTED");
recv(newsockfd,buff,15,0);
buff[20]='\0';
printf("\n\nMESSEGE RECEIVED");
printf("\n\n%s",buff);
send(newsockfd,buff,15,0);
printf("\n Message Echoed back");
exit(0);
}
close(newsockfd);
}}
CLIENT:
#include
#include
#include
#include
#include
#include
#include
#include
#define SERV_TCP_PORT 4000
int main()
{
int sockfd,l;
char buff[100],messege[50];
struct sockaddr_in serv_addr;
bzero((char *)&serv_addr,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
serv_addr.sin_port=htons(4119);
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("\n\nSOCKET CAN'T BE CREATED");
exit(0);
}
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
{
printf("\n\nNOT CONNECTED SUCCESSFULLY");
exit(0);
}
//printf("\n\nCONNECTED SUCCESSFULLY");
printf("\n\nENTER ANY MESSEGE YOU WANT TO SEND :");
scanf("%s",messege);
l=strlen(messege);
messege[l]='\0';
printf("\n%d",l);
send(sockfd,messege,20,0);
printf("\n\nMESSEGE HAVE BEEN SENT\n\n");
recv(sockfd,buff,20,0);
printf("%s",buff);
}
Q9. CHAT SERVER CLIENT PROGRAM USING TCP
SERVER:
#include
#include
#include
#include
#include
#include
//#include
#include
#define SERV_TCP_PORT 4000
int main()
{
int sockfd,newsockfd,l,length,bt,lt,at,i,cpid;
char *pname,buff[100],buff1[100];
struct sockaddr_in cli_addr,serv_addr;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
{
printf("\n\nSOCKET IS NOT CREATED ");
exit(0);
}
bzero((char *)&serv_addr,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
i=serv_addr.sin_port=htons(4119);
if(bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
{
printf("\n\nBIND ERRORS ");
exit(0);
}
listen(sockfd,5);
for( ; ; )
{
length=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr *)&cli_addr,(socklen_t *)&length);
if(newsockfd<0)
{
printf("\n\nACCEPT ERRORS ");
exit(0);
}
cpid=fork();
if(cpid==0)
{
close(sockfd);
printf("\n\nCLIENT IS CONNECTED");
recv(newsockfd,buff,15,0);
buff[20]='\0';
printf("\n\nMESSEGE RECEIVED");
printf("\n\n%s",buff);
printf("\n Enter Mssg you want to send:");
scanf("%s",buff1);
send(newsockfd,buff1,15,0);
printf("\n Message sent");
}
close(newsockfd);
}
}
CLIENT:
#include
#include
#include
#include
#include
#include
#include
#include
#define SERV_TCP_PORT 4000
int main()
{
int sockfd,l;
char buff[100],messege[50];
struct sockaddr_in serv_addr;
bzero((char *)&serv_addr,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
serv_addr.sin_port=htons(4119);
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
printf("\n\nSOCKET CAN'T BE CREATED");
exit(0);
}
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr))<0)
{
printf("\n\nNOT CONNECTED SUCCESSFULLY");
exit(0);
}
//printf("\n\nCONNECTED SUCCESSFULLY");
for(;;){
printf("\n\nENTER ANY MESSEGE YOU WANT TO SEND :");
scanf("%s",messege);
l=strlen(messege);
messege[l]='\0';
printf("\n%d",l);
send(sockfd,messege,20,0);
printf("\n\nMESSAGE HAVE BEEN SENT\n\n");
recv(sockfd,buff,20,0);
printf("%s",buff);
}
}