0% found this document useful (0 votes)
5 views8 pages

Server Student

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

Server Student

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

#include<string.

h>
#include<unistd.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<stdio.h>

struct student
{
char name[20],dept[10];
int roln, year,per;
};

main()
{
int s,r,recb,sntb,x,ns;
printf("INPUT port number: ");
scanf("%d", &x);
socklen_t len;
struct sockaddr_in server,client;
struct student A[10],B;
char buff[50];

s=socket(AF_INET,SOCK_STREAM,0);
if(s==-1)
{
printf("\nSocket creation
error.");
exit(0);
}
printf("\nSocket created.");

server.sin_family=AF_INET;
server.sin_port=htons(x);
server.sin_addr.s_addr=htonl(INADDR_A
NY);

r=bind(s,(struct
sockaddr*)&server,sizeof(server));
if(r==-1)
{
printf("\nBinding error.");
exit(0);
}
printf("\nSocket binded.");

r=listen(s,1);
if(r==-1)
{
close(s);
exit(0);
}
printf("\nSocket listening.");

len=sizeof(client);
ns=accept(s,(struct
sockaddr*)&client, &len);
if(ns==-1)
{
close(s);
exit(0);
}
printf("\nSocket accepting.");
int c,reg,y=2,i;
A[0].roln=2;
strcpy(A[0].name,"Pink Panther");
strcpy(A[0].dept,"CSE");
A[0].per=50;
A[0].year=2009;
A[1].roln=15;
strcpy(A[1].name,"Dare Devils");
strcpy(A[1].dept,"Mech");
A[1].per=90;
A[1].year=2013;

while(1){

recb=recv(ns,&c,sizeof(c),0);
if(recb==-1)
{
printf("\nMessage Recieving
Failed");
close(s);
close(ns);
exit(0);
}

if(c==5)
break;
if(c==1)
{
printf("\n\n\tInserting a
record");
recb=recv(ns,&B,sizeof(B),0);
if(recb==-1)
{
printf("\nMessage Recieving
Failed");
close(s);
close(ns);
exit(0);
}
strcpy(buff, "Record Inserted
Successfully");
A[y++]=B;
printf("\n\n\tRoll No\tName\
tYear\tDept.\tPercentage");
for(i=0;i<y;i++)
printf("\n\t%d\t%s\t%d\t%s\t
%d", A[i].roln, A[i].name, A[i].year,
A[i].dept, A[i].per);
}
if(c==2)
{
printf("\n\n\tDeleting a
record");
recb=recv(ns,&reg,sizeof(reg),0);
if(recb==-1)
{
printf("\nMessage Recieving
Failed");
close(s);
close(ns);
exit(0);
}
for(i=0;i<y; i++)
if(reg==A[i].roln)
break;
if(i<y)
{
for(; i<y; i++)
{
A[i].roln=A[i+1].roln;
strcpy(A[i].name,A[i+1].n
ame);
strcpy(A[i].dept,A[i+1].d
ept);
A[i].per=A[i+1].per;
A[i].year=A[i+1].year;
}
strcpy(buff, "Record Deleted
Successfully");
y--;
}
else
strcpy(buff, "Record Not
Found");
printf("\n\n\tRoll No\tName\
tYear\tDept.\tPercentage");
for(i=0;i<y;i++)
printf("\n\t%d\t%s\t%d\t%s\t
%d", A[i].roln, A[i].name, A[i].year,
A[i].dept, A[i].per);

if(c==3)
{
printf("\n\n\tDisplaying all
records");
printf("\n\n\tRoll No\tName\
tYear\tDept.\tPercentage");
for(i=0;i<y;i++)
printf("\n\t%d\t%s\t%d\t%s\t
%d", A[i].roln, A[i].name, A[i].year,
A[i].dept, A[i].per);
strcpy(buff, "Records
Displayed");
}
if(c==4)
{
printf("\n\n\tSearching a
record");
recb=recv(ns,&reg,sizeof(reg),0);
if(recb==-1)
{
printf("\nMessage Recieving
Failed");
close(s);
close(ns);
exit(0);
}
for(i=0;i<y; i++)
if(reg==A[i].roln)
break;
if(i<y)
{
printf("\n\t%d\t%s\t%d\t%s\t
%d", A[i].roln, A[i].name, A[i].year,
A[i].dept, A[i].per);
strcpy(buff, "Record Found");
}
else
strcpy(buff, "Record Not
Found");
}
printf("\n\n");
sntb=send(ns,buff,sizeof(buff),0);
if(sntb==-1)
{
printf("\nMessage Sending
Failed");
close(s);
close(ns);
exit(0);
}
}
close(ns);
close(s);
}

You might also like